首页 > 解决方案 > 芯片组 OnCheckedChangeListener() 未触发

问题描述

我正在尝试制作基于 ChipGroup 和 Chip 的 recyclerview 过滤器

在此处输入图像描述

我在我的应用程序上使用片段,因此,包含 RecyclerView 的片段包含一个 frameLayout,它膨胀了 ChipGroup 过滤器片段

当用户取消选择 ChipGroup 中的所有芯片时,我试图触发一个侦听器(当用户检查芯片时,我已经将侦听器放在芯片上以触发)

我已经在芯片组上放了一些听众,但没有人被触发

FilterFragment.java

public class FilterFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
public ChipGroup chipGroup;

// TODO: Rename and change types of parameters
public View.OnClickListener chipClickListener;

private OnFragmentInteractionListener mListener;

public FilterFragment() {
    // Required empty public constructor
}


public static FilterFragment newInstance(View.OnClickListener 
param1) {
    CoachFilterFragment fragment = new CoachFilterFragment();
    Bundle args = new Bundle();

    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {

    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup 
container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_filter, 
container, false);
    this.chipGroup = view.findViewById(R.id.chipGroup);
    for(Skill skill : ((MainActivity)getContext()).api.skills){

        Chip chip = new Chip(getContext());
        chip.setId(skill.getId());


        chip.setText(skill.getName());
        chip.setOnClickListener(chipClickListener);
        chip.setCheckable(true);
        chipGroup.addView(chip);

    }

    chipGroup.setOnCheckedChangeListener((chipGroup, id) -> {
      Log.d("test","ok");
    });


    return view;
}

public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}



public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
  }
}

FilterFragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".Fragment.FilterFragment">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

       </com.google.android.material.chip.ChipGroup>
   </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

有人知道为什么我的 ChipGroup 没有触发任何侦听器吗?也许我缺少一些参数或什么?

标签: androidandroid-chips

解决方案


您的代码很好,唯一的问题是setOnCheckedChangeListener()只有在您ChipGroup的适用时才有效singleSelection

阅读本文档ChipGroup

setOnCheckedChangeListener()

另请阅读

处理已检查的筹码

调用 setOnCheckedChangeListener(OnCheckedChangeListener) 注册一个回调,该回调将在该组中被检查的芯片发生变化时调用。此回调仅在单选模式下调用。

如果你想使用setOnCheckedChangeListener()ChipGroup你需要做的app:singleSelection="true"

更新

根据您的以下评论,我添加了示例代码以在选择 ChipGroup 时设法处理

用于保持选择的示例代码ChipGroup

布局.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chipGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            >

        </com.google.android.material.chip.ChipGroup>
    </androidx.constraintlayout.widget.ConstraintLayout>

    <Button
        android:layout_width="match_parent"
        android:text="Get Result"
        android:id="@+id/btnShowResult"
        android:layout_gravity="bottom"
        android:layout_height="wrap_content" />
</LinearLayout>

活动代码

public class Main3Activity extends AppCompatActivity {

    public ChipGroup chipGroup;
    public Button btnShowResult;
    public ArrayList<Boolean> booleanArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        chipGroup = findViewById(R.id.chipGroup);
        btnShowResult = findViewById(R.id.btnShowResult);


        btnShowResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < booleanArrayList.size(); i++) {
                    Log.e("RESULT", i + " :" + booleanArrayList.get(i));
                }
            }
        });

        for (int i = 0; i < 5; i++) {

            Chip chip = new Chip(this);
            chip.setId(i);
            chip.setTag(i);

            booleanArrayList.add(false);
            chip.setText("Chip No : " + i);
            chip.setCheckable(true);

            chip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                    int tag = (int) compoundButton.getTag();
                    booleanArrayList.set(tag, b);

                }
            });
            chipGroup.addView(chip);

        }

        chipGroup.invalidate();


        chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(ChipGroup chipGroup, int i) {

                Chip chip = chipGroup.findViewById(i);


                if (chip != null)
                    Toast.makeText(getApplicationContext(), "Chip is " + chip.getText().toString(), Toast.LENGTH_SHORT).show();

                Log.e("OnCheckedChangeListener", "Called");
            }
        });

    }

    ChipGroup.OnCheckedChangeListener onCheckedChangeListener = new ChipGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(ChipGroup chipGroup, int i) {

        }
    };

}

有关更多信息,请查看以下文章


推荐阅读