我想实现带有水平和垂直滚动的RecyclerView以及垂直项目上的一个按钮,所以当我点击按钮时,应该将文本更改为保存到已保存。在我的应用程序中,当我单击按钮时,文本被更改但是当我自动向下滚动时,另一个按钮的文本被更改。以下是我的代码。
In this screenshot I clicked on VerticalList3's button and text is changed successfully
When I scrolled down automatically VerticalList20 and VerticalList14's button text is changed
public class MainActivity extends AppCompatActivity {
private RecyclerView vertical_recycler_view,horizontal_recycler_view;
private ArrayList<String> horizontalList,verticalList;
private HorizontalAdapter horizontalAdapter;
private VerticalAdapter verticalAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vertical_recycler_view= (RecyclerView) findViewById(R.id.vertical_recycler_view);
horizontal_recycler_view= (RecyclerView) findViewById(R.id.horizontal_recycler_view);
horizontalList=new ArrayList<>();
horizontalList.add("horizontal 1");
horizontalList.add("horizontal 2");
horizontalList.add("horizontal 3");
horizontalList.add("horizontal 4");
horizontalList.add("horizontal 5");
horizontalList.add("horizontal 6");
horizontalList.add("horizontal 7");
horizontalList.add("horizontal 8");
horizontalList.add("horizontal 9");
horizontalList.add("horizontal 10");
horizontalList.add("horizontal 11");
horizontalList.add("horizontal 12");
horizontalList.add("horizontal 13");
horizontalList.add("horizontal 14");
horizontalList.add("horizontal 15");
horizontalList.add("horizontal 16");
horizontalList.add("horizontal 17");
horizontalList.add("horizontal 18");
horizontalList.add("horizontal 19");
horizontalList.add("horizontal 20");
verticalList=new ArrayList<>();
verticalList.add("verticallist 1");
verticalList.add("verticallist 2");
verticalList.add("verticallist 3");
verticalList.add("verticallist 4");
verticalList.add("verticallist 5");
verticalList.add("verticallist 6");
verticalList.add("verticallist 7");
verticalList.add("verticallist 8");
verticalList.add("verticallist 9");
verticalList.add("verticallist 10");
verticalList.add("verticallist 11");
verticalList.add("verticallist 12");
verticalList.add("verticallist 13");
verticalList.add("verticallist 14");
verticalList.add("verticallist 15");
verticalList.add("verticallist 16");
verticalList.add("verticallist 17");
verticalList.add("verticallist 18");
verticalList.add("verticallist 19");
verticalList.add("verticallist 20");
horizontalAdapter=new HorizontalAdapter(horizontalList);
verticalAdapter=new VerticalAdapter(verticalList);
LinearLayoutManager verticalLayoutmanager
= new LinearLayoutManager(MainActivity.this,
LinearLayoutManager.VERTICAL, false);
vertical_recycler_view.setLayoutManager(verticalLayoutmanager);
// RecyclerView.LayoutManager mLayoutManager = new
LinearLayoutManager(getApplicationContext());
// vertical_recycler_view.setLayoutManager(mLayoutManager);
LinearLayoutManager horizontalLayoutManagaer
= new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
vertical_recycler_view.setAdapter(verticalAdapter);
horizontal_recycler_view.setAdapter(horizontalAdapter);
}
public class HorizontalAdapter extends
RecyclerView.Adapter<HorizontalAdapter.MyViewHolder> {
private List<String> horizontalList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView txtView;
public MyViewHolder(View view) {
super(view);
txtView = (TextView) view.findViewById(R.id.txtView);
}
}
public HorizontalAdapter(List<String> horizontalList) {
this.horizontalList = horizontalList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.horizontal_item_view, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.txtView.setText(horizontalList.get(position));
holder.txtView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,holder.txtView.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return horizontalList.size();
}
}
public class VerticalAdapter extends RecyclerView.Adapter<VerticalAdapter.MyViewHolder> {
private List<String> verticalList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView txtView;
public Button button;
public MyViewHolder(View view) {
super(view);
txtView = (TextView) view.findViewById(R.id.txtView);
button = (Button) view.findViewById(R.id.verticalButton);
}
}
public VerticalAdapter(List<String> verticalList) {
this.verticalList = verticalList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.vertical_item_view, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.button.setText("Saved");
}
});
holder.txtView.setText(verticalList.get(position));
holder.txtView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,holder.txtView.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return verticalList.size();
}
}
}
答案 0 :(得分:1)
@Potapov Anton是对的,但你也可以这样做。
实施此方法:
@Override
public int getItemViewType(int position) {
return position;
}
希望有所帮助
答案 1 :(得分:0)
RecyclerView将viewHolder重用于其他项目。因此,仅在viewHolder中将文本直接设置为按钮(如此holder.button.setText("Saved");
)是错误的。您需要在列表中为此位置的项目设置文本。
答案 2 :(得分:0)
您还必须存储按钮的状态。您可以创建另一个列表:
List<Boolean> buttonsStatus = new ArrayList<>(numOfElements);
和填写清单:
for(int i = 0; i<numOfElements; i++)
{
buttonsStatus.add(false)
}
或者您可以修改当前列表以存储Item
个对象,如下所示:
public class Item{
public String text
public boolean saved;
public Item(String text, boolean saved)
{
this.text = text;
this.saved= saved;
}
}
并在您的代码中使用它:
List<Item> verticalList = new ArrayList<>();
verticalList.add(new Item("verticallist 1", false));
...
并在onBindViewHolder
方法中:
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.button.setText("Saved");
verticalList.get(position).saved = true;
}
});
Item item = verticalList.get(position);
holder.button.setText(item.saved ? "Saved" : "Save");
holder.txtView.setText(item.text);
holder.txtView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,holder.txtView.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
}