Vikas Godiyal Vikas Godiyal - 1 month ago 13
Android Question

I have one issue on implementing RecyclerView with horizontal and vertical scrolling

I want to implement RecyclerView with horizontal and vertical scrolling and one button on vertical item so when I clicked on button that text should be changed into save to Saved. In my app when I clicked on button, text is changed but when I scrolling down automatically another button's text is changed. Below is my code.

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();
}
}
}

Answer Source

@Potapov Anton is right but you can also do this way.

Implement this method:

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

Hope it helps