【问题标题】:Android checkbox with custom selector inside recyclerview在 recyclerview 中带有自定义选择器的 Android 复选框
【发布时间】:2016-07-31 12:20:12
【问题描述】:

我使用复选框制作了 recyclerview,因此您可以在按下时选择屏幕底部的每个项目和按钮从列表中返回所选项目。问题是无论我做什么按钮总是返回选择了 0 个项目。我发现当我单击复选框时,即使复选框中的图标发生变化,复选框状态也总是错误的。我使用自定义选择器更改复选框内的图标,但我找不到更改程序内复选框状态的方法。

这是包含recyclerview的片段

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    Resources res = getResources();
    titles = res.getStringArray(R.array.muscle_titles);

    recyclerMain = (RecyclerView) rootView.findViewById(R.id.MainList);
    adapter = new MainAdapter(getActivity(), getData(getActivity()));
    recyclerMain.setAdapter(adapter);
    recyclerMain.setLayoutManager(new LinearLayoutManager(getActivity()));
    return rootView;
}

public static List<MainRowInformation> getData(Context context){
    List<MainRowInformation> data = new ArrayList<>();


    for(int i=0;i<titles.length;i++){

        final MainRowInformation current = new MainRowInformation();
        current.title = titles[i];
        current.checkBox = new CheckBox(context);
        current.checkBox.setChecked(false);


        data.add(current);
    }

    return data;
}

这是recyclerview适配器

    private LayoutInflater inflater;
private List<MainRowInformation> data = Collections.emptyList();

public MainAdapter(Context context, List<MainRowInformation> data) {
    this.data = data;
    inflater = LayoutInflater.from(context);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.row_main, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    MainRowInformation current = data.get(position);
    holder.title.setText(current.title);
    //mozda je krivo sljedeca linija
    holder.checkbox = current.checkBox;

}

@Override
public int getItemCount() {
    return data.size();
}

class MyViewHolder extends RecyclerView.ViewHolder{

    TextView title;
    CheckBox checkbox;

    public MyViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.MainListText);
        checkbox = (CheckBox) itemView.findViewById(R.id.MainListCheckBox);

    }
}

复选框

    <CheckBox
    android:id="@+id/MainListCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/main_list_button_icon"/>

复选框选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/ic_list_uncheck"
    android:state_checked="false" />
<item android:drawable="@mipmap/ic_list_check"
    android:state_checked="true"/>

the button return 0 but 3 items were selected

【问题讨论】:

    标签: java android xml checkbox android-recyclerview


    【解决方案1】:

    找到解决方案我所要做的就是在 MyViewHolder 中制作 onClickListener 复选框。我以前这样做过,但由于某种原因它不起作用。

        class MyViewHolder extends RecyclerView.ViewHolder{
    
        TextView title;
        CheckBox checkbox;
    
        public MyViewHolder(final View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.MainListText);
            checkbox = (CheckBox) itemView.findViewById(R.id.MainListCheckBox);
            checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                if(checkbox.isChecked()){
                    checkbox.setChecked(false);
                }else{
                    checkbox.setChecked(true);
                }
                }
            });
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多