当recycleview滚动父活动按钮时,禁用

时间:2017-11-22 10:13:21

标签: java android checkbox android-recyclerview recycler-adapter

我有一个RecyclerView网格适配器包含复选框和父活动中的显示按钮。如果我们选中复选框父活动显示按钮启用,如果我们取消选中复选框显示按钮将禁用,这是我的要求,它的工作正常,但当我滚动循环视图我的显示按钮禁用。当我滚动时我得到item.isChecked()是假的..任何人都可以帮我实现我的要求,请解释

 @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    try {
        final GridViewHolder viewHolder = (GridViewHolder) holder;

        final MyList item = items.get(position);

        viewHolder.myCheckBox.setTag(position);
        viewHolder.myCheckBox.setOnCheckedChangeListener(new 
   CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, 
    boolean b) {

                item.setChecked(b);

                if (item.isChecked())
                   Count++;
                else
                  Count--;

                if (Count > 0) {

                    ((MyActivity)context).showButton().setEnabled(true);

                } else {
                    ((MyActivity)context).showButton().setEnabled(false);
                }


        }
        });

        viewHolder.myCheckBox.setChecked(item.isChecked());


    } catch (Exception e) {
        e.printStackTrace();
    }
}

在我的pojo列表类中,我为identy复选框返回值写了两个函数

  boolean isChecked;
  public boolean isChecked() {
    return isChecked;
  }

public void setChecked(boolean isChecked) {
    this.isChecked = isChecked;
}

适配器中的ViewHolder内部类

   public class GridViewHolder extends RecyclerView.ViewHolder {


    private CheckBox myCheckBox;


     public GridViewHolder(final View parent) {
        super(parent);

        myCheckBox = (CheckBox) parent.findViewById(myCheckBox);

       ((ProductListingActivity)context).showButton().setEnabled(false);

        parent.setOnClickListener(GridAdapterMy.this);
    }
  }

5 个答案:

答案 0 :(得分:2)

我遇到了和你一样的问题,这就是我修复它的方法。

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    try {
        final GridViewHolder viewHolder = (GridViewHolder) holder;

        viewHolder.myCheckBox.setTag(position);

        //add this two lines 
        viewHolder.myCheckBox.setOnCheckedChangeListener(null);
        viewHolder.myCheckBox.setChecked(items.get(position).isChecked());

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


                items.get(position).setChecked(b);

                if (items.get(position).isChecked())
                   Count++;
                else
                  Count--;

                if (Count > 0) {
                   ((MyActivity)context).showButton().setEnabled(true);
                } else {
                   ((MyActivity)context).showButton().setEnabled(false);
                }}
        });

    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:1)

在重复调用onBindViewHolder时,这就是为什么复选框的状态会自动更改的原因。因此,您必须在滚动时保存复选框的状态。 看看。

    public class MyListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

       private final boolean[] mCheckedState;
       private final Context mContext;
       private CheckBox result;

    public MyListAdapter(Context context, int resource, int textViewResourceId, List<Object> objects) {
        super(context, resource, textViewResourceId, objects);
        mCheckedState = new boolean[objects.size()];
        mContext = context;
    }

  @Override 
 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
   try {
    final GridViewHolder viewHolder = (GridViewHolder) holder;
    viewHolder.myCheckBox.setTag(position);

    //add this two lines 
    viewHolder.myCheckBox.setOnCheckedChangeListener(null);       
    result =  viewHolder.myCheckBox;
    if (result == null) {
        result = new CheckBox(mContext);
    }
    result.setChecked(mCheckedState[position]);
    viewHolder.myCheckBox.setOnCheckedChangeListener(new 
        CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton,boolean b) {

             if(b){
                result.setChecked(mCheckedState[position]);
                  }
    });

} catch (Exception e) {
    e.printStackTrace();
}

}     }

快乐的编码!!

答案 2 :(得分:1)

POJO类中创建一个额外的变量:

public class DataItems {   

    int id, price, qty;
    String title;

    boolean isChecked; // create boolean variable like this

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

}

然后,在适配器中添加如下代码:

@Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        // check here the state of your check from your arraylist of POJO
        if (dataItemsArrayList.get(position).isChecked) {
            holder.chkBox.setChecked(true);               
        } else {
            holder.chkBox.setChecked(false);
        }

        holder.tvTitle.setText(dataItemsArrayList.get(position).getTitle() + "");
        holder.tvPrice.setText(dataItemsArrayList.get(position).getPrice() + "");
        holder.tvQTY.setText(dataItemsArrayList.get(position).getQty() + "");
        holder.chkBox.setTag(position);

        holder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // save here the state of your check box when your select or unSelect the Checkbox
                if (isChecked) {

                    ((MainActivity)context).isChecked(true);
                    holder.chkBox.setChecked(true);
                    dataItemsArrayList.get(position).setChecked(true);


                } else {
                    ((MainActivity)context).isChecked(false);
                    holder.chkBox.setChecked(false);
                    dataItemsArrayList.get(position).setChecked(false);
                }

            }
        });

}

修改

创建一个新的Interface,如下所示

public interface test {

    public void isChecked(boolean isChecked);
}

现在在您的活动中实现,就像这样

public class MainActivity extends AppCompatActivity implements test

将此活动上下文作为参数传递给您的适配器

而不是在您的活动中添加此代码

 @Override
    public void isChecked(boolean isChecked) {

        if(isChecked){
            // enable YOur Button here
        }else {
            // disable YOur Button here
        }
    }

答案 3 :(得分:1)

尝试使用此代码

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    try {
        final GridViewHolder viewHolder = (GridViewHolder) holder;

        final MyList item = items.get(position);

        viewHolder.myCheckBox.setTag(position);

        //add this two lines 
//        viewHolder.myCheckBox.setOnCheckedChangeListener(null);
        viewHolder.myCheckBox.setChecked(item.isChecked());
        viewHolder.myCheckBox.setTag(position);
        viewHolder.myCheckBox.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                int pos = Integer.parseInt(v.getTag().toString().trim());

                items.get(pos).setChecked(b);
                if(items.get(pos).isChecked()){
                    items.get(pos).setChecked(false);
                }else{
                    items.get(pos).setChecked(true);
                }

                if (items.get(pos).isChecked())
                   Count++;
                else
                  Count--;

                if (Count > 0) {
                   ((MyActivity)context).showButton().setEnabled(true);
                } else {
                   ((MyActivity)context).showButton().setEnabled(false);
                }
                });

    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 4 :(得分:0)

感谢所有帮助我的人,我发现了我的错误,每当我滚动时,我的setOnCheckedChangeListener正在调用,这就是我的按钮禁用的原因,我只添加了一行&#34; viewHolder.myCheckBox.setOnCheckedChangeListener(null); &#34;它完美地工作......谢谢你的建议和帮助