Chip Group OnCheckedChangeListener() not triggered

后端 未结 6 886
遥遥无期
遥遥无期 2020-12-10 05:13

I\'m trying to make a recyclerview filter based ChipGroup & Chip

I\'m use fragment on my app, so, the fragment who contain the RecyclerView contain a fr

6条回答
  •  天命终不由人
    2020-12-10 05:59

    Your code is fine the only issue is that setOnCheckedChangeListener() only work when your ChipGroup is for singleSelection

    Read this documentation of ChipGroup

    setOnCheckedChangeListener()

    • Register a callback to be invoked when the checked chip changes in this group.
    • This callback is only invoked in single selection mode.

    ALSO READ

    Handling Checked Chips

    Call setOnCheckedChangeListener(OnCheckedChangeListener) to register a callback to be invoked when the checked chip changes in this group. This callback is only invoked in single selection mode.

    if you want use setOnCheckedChangeListener() of ChipGroup than you need to make app:singleSelection="true"

    UPDATE

    based on your below comment i have added sample code to manage to handle when ChipGroup selection

    SAMPLE CODE for maintain selection in ChipGroup

    Layout.xml

    
    
    
        
    
            
    
            
        
    
        

    Activity code

    public class Main3Activity extends AppCompatActivity {
    
        public ChipGroup chipGroup;
        public Button btnShowResult;
        public ArrayList 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) {
    
            }
        };
    
    }
    

    For more information please check below articles

    • Chips: Material Components for Android
    • Android P: Chips and ChipGroup
    • Exploring the v28 Android Design Support Library Additions

提交回复
热议问题