android– 芯片组OnCheckedChangeListener()未触发

android– 芯片组OnCheckedChangeListener()未触发,第1张

概述我正在尝试制作基于ChipGroup&;的回收视图过滤器.芯片(参见:https://storage.googleapis.com/spec-host-backup/mio-design%2Fassets%2F13XlEcbFJxRBSlK18nPZsZ-qqbUE1hFYq%2Fchips-types-filter.png)我在我的应用程序上使用片段,因此,包含RecyclerView的片段包含一个frameLay

我正在尝试制作基于ChipGroup&amp ;;的回收视图过滤器.芯片(参见:https://storage.googleapis.com/spec-host-backup/mio-design%2Fassets%2F13XlEcbFJxRBSlK18nPZsZ-qqbUE1hFYq%2Fchips-types-filter.png)

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

当用户取消选择ChipGroup内部的所有芯片时,我正试图触发一个监听器(当用户检查芯片时,我已经将监听器置于芯片上以触发)

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

FilterFragment.java

public class FilterFragment extends Fragment {// Todo: Re@R_404_6889@ parameter arguments, choose @R_404_6889@s that match// the fragment initialization parameters, e.g. ARG_ITEM_NUMBERpublic ChipGroup chipGroup;// Todo: Re@R_404_6889@ and change types of parameterspublic 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;}@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    if (getArguments() != null) {    }}@OverrIDepublic 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.get@R_404_6889@());        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);    }}@OverrIDepublic voID onAttach(Context context) {    super.onAttach(context);}@OverrIDepublic voID onDetach() {    super.onDetach();    mListener = null;}public interface OnFragmentInteractionListener {    // Todo: Update argument type and @R_404_6889@    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没有触发任何监听器?也许我错过了一些参数或什么?

解决方法:

您的代码很好唯一的问题是setonCheckedchangelistener()仅在您的ChipGroup用于singleSelection时才有效

阅读ChipGroup的这个文档

setOnCheckedChangeListener()

>当已检查的芯片在此组中发生更改时,注册要调用的回调.
>此回调仅在single selection mode.中调用

还读了

07002

调用setonCheckedchangelistener(OnCheckedchangelistener)来注册当被检查的芯片在该组中发生变化时要调用的回调.仅在单选模式下调用此回调.

如果你想使用ChipGroup的setonCheckedchangelistener(),你需要使用app:singleSelection =“true”

UPDATE

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

用于在ChipGroup中维护选择的SAMPLE CODE

Layout.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>

Activity code

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) {        }    };}

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

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

总结

以上是内存溢出为你收集整理的android – 芯片组OnCheckedChangeListener()未触发全部内容,希望文章能够帮你解决android – 芯片组OnCheckedChangeListener()未触发所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1112500.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存