gpt4 book ai didi

android - 我在使用水平和垂直滚动实现 RecyclerView 时遇到一个问题

转载 作者:行者123 更新时间:2023-11-30 00:23:57 32 4
gpt4 key购买 nike

我想实现具有水平和垂直滚动以及垂直项目上的一个按钮的 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();
}
}
}

最佳答案

@Potapov Anton 是对的,但您也可以这样做。

实现这个方法:

@Override
public int getItemViewType(int position) {
return position;
}

希望对你有帮助

关于android - 我在使用水平和垂直滚动实现 RecyclerView 时遇到一个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45755581/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com