在recycleler视图中使用onBindViewHolder保持项目值

时间:2018-04-20 10:56:17

标签: android android-recyclerview textview

我正在RecyclerView工作,我正在获取字符串列表。如果我点击它,我想保留特定textView的值,问题是当我向下滚动并再次出现时,它没有向我显示该TextView上的点击值。我正在显示"已选择",但是当我滚动并再次出现时,它会显示该字符串列表中的默认值。

我将我的代码和截图放在下面。

我的onBindViewHolder代码:

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

    final String parseObject = arrayList.get(position);

    if (holder instanceof MemberViewHolder) {

        if (arrayList.size() > 0) {

            ((MemberViewHolder) holder).txtTicket.setText(parseObject);
            ((MemberViewHolder) holder).txtTicket.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((MemberViewHolder) holder).txtTicket.setTextColor(R.color.red_theme);
                    ((MemberViewHolder) holder).txtTicket.setText("Selected");
                }
            });

        }
}
}

我选择的这些值:

image 1

这些随机选择的值显示我正在滚动,但我没有点击它们。

Image 2

5 个答案:

答案 0 :(得分:1)

像这样创建pojo

public class Test {
    public String value;
    public boolean IsSelected;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public boolean isSelected() {
        return IsSelected;
    }

    public void setSelected(boolean selected) {
        IsSelected = selected;
    }
}

现在创建该pojo类的ArrayList

ArrayList<Test>arrayList= new ArrayList<>();

现在从服务器获取列表而不是像arraylist这样添加数据

 // add item in list like this when you get list from server
    Test test= new Test();
    test.setValue("value");
    arrayList.add(test);

现在传递适配器类中的ArrayList

当您点击viewholder中的任何项目时,只需在列表中设置布尔变量

示例

((MemberViewHolder) holder).txtTicket.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((MemberViewHolder) holder).txtTicket.setTextColor(R.color.red_theme);
                    ((MemberViewHolder) holder).txtTicket.setText("Selected");
                    arrayList.get(position).setSelected(true);
                }
            });

Textview内设置文字时,只需使用此条件

if(arrayList.get(position).isSelected()){
    txtTicket.setText("Selected");
 }else {
    txtTicket.setText(arrayList.get(position).getValue());
 }

答案 1 :(得分:1)

document.getElementById("bindNotification ");的{​​{1}}中添加以下行:

onClick()

修改将以下代码放在txtTicket方法中:

arrayList.set(position,"Selected");

答案 2 :(得分:0)

再次向下滚动时,将再次执行onBindViewHolder方法。您必须保存选定的状态。例如:

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

    final String parseObject = arrayList.get(position);

    if (holder instanceof MemberViewHolder) {

        if (arrayList.size() > 0) {
            if ((Boolean)((MemberViewHolder) holder).txtTicket.getTag())) {
                ((MemberViewHolder) holder).txtTicket.setText("Selected");
            } else{
                ((MemberViewHolder) holder).txtTicket.setText(parseObject);
            }

            ((MemberViewHolder) holder).txtTicket.setText(parseObject);
            ((MemberViewHolder) holder).txtTicket.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {                        
                    ((MemberViewHolder) holder).txtTicket.setTextColor(R.color.red_theme);
                    ((MemberViewHolder) holder).txtTicket.setText("Selected");
                    ((MemberViewHolder) holder).txtTicket.setTag(new Boolean(true));

                }
            });

        }
    }
}

答案 3 :(得分:0)

获取一个具有字符串和布尔值的pojo类,如下面的代码..

public class Data {
private String value;
private boolean selectedFlag;

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public boolean isSelectedFlag() {
    return selectedFlag;
}

public void setSelectedFlag(boolean selectedFlag) {
    this.selectedFlag = selectedFlag;
}

}

并将此pojo类传递给适配器并将接口设置为handel click事件,如下面的代码..

  List<Data> mDataList = new ArrayList<>();// hear you can pass any pojo class object.

onItemClickListner onItemClickListner;


public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

public interface onItemClickListner {
    void onClick(Data str);//pass your object types.
}

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    Data data = mStringList.get(position); // if you pass object of class then create that class object.
    if (data.isSelectedFlag()) {
        holder.textView.setText("Selected");
    }
    // below code handle click event on recycler view item.
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClickListner.onClick(data);
        }
    });
}

在适配器绑定到代码下面使用的回收器视图后的活动中。

        recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
        @Override
        public void onClick(Data str) {
            str.setSelectedFlag(true);
            recyclerViewAdpater.notifyDataSetChanged();
        }
    });

答案 4 :(得分:0)

你需要一个pojo类,

public class Pojo{
    String id;
    private boolean isSelected;

  public Pojo(String id) {
    this.id = id;
  }


  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public boolean isSelected() {
    return isSelected;
  }

  public void setSelected(boolean selected) {
    isSelected = selected;
  }
}

首先定位setTag,

holder.txtTicket.setTag(position);


holder.txtTicket.setSelected(arrayList.get(position).isSelectedFlag());
    final int pos = position;
    holder.txtTicket.setTag(arrayList.get(position));

然后在onClick事件中把这个,

Pojo pojo = (Pojo) txtTicket.getTag();
pojo.setSelected(txtTicket.isSelected());