如何通过单击每个项目中的复选框滚动到下一个项目(recyclerview)
how to scroll to next item by clicking on a checkbox in each item (recyclerview)
请原谅我的英语。
我的列表包含 100 个项目。每个项目都包含两个复选框,每次单击复选框时我都想滚动到下一个项目。
请给我最好的答案link如果这个问题已经有人回答了
这是我的自定义适配器
public class CustomAdapter extends
RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private ArrayList answerFirst;
private ArrayList answerSecond;
private Context context;
public CustomAdapter(Context context, ArrayList questionList, ArrayList
answerFirst, ArrayList answerSecond) {
this.context = context;
this.answerFirst = answerFirst;
this.answerSecond = answerSecond;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.test_items,
parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position)
{
holder.firstanswer.setText(answerFirst.get(position).toString());
holder.secondanswer.setText(answerSecond.get(position).toString());
}
@Override
public int getItemCount() {
return questionList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView question;
CheckBox firstanswer;
CheckBox secondanswer;
public MyViewHolder(View itemView) {
super(itemView);
question = (TextView) itemView.findViewById(R.id.question_text);
firstanswer = (CheckBox) itemView.findViewById(R.id.first_answer);
secondanswer = (CheckBox) itemView.findViewById(R.id.second_answer);
}
}
}
这是主要的activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_test);
final RecyclerView recyclerView = (RecyclerView)
findViewById(R.id.recyclerView_test);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);
final CustomAdapter customAdapter = new CustomAdapter(mainActivity.this, answerFirst,answerSecond);
recyclerView.setAdapter(customAdapter);
}
您可以通过在主 class 中使适配器 class 并在复选框的侦听器方法中滚动到所需位置来做到这一点,因为 RecyclerView 是一个全局变量,您可以访问它并且滚动到所需位置。
rv.scrollToPosition(youPositionInTheAdapter)
否则在构造函数中传递 RecyclerView 的实例并执行相同的操作
并通过 getAdapterPosition()
获取适配器内的当前项目位置
或者您可以像这样使用布局管理器来完成
rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter)
请原谅我的英语。
我的列表包含 100 个项目。每个项目都包含两个复选框,每次单击复选框时我都想滚动到下一个项目。
请给我最好的答案link如果这个问题已经有人回答了
这是我的自定义适配器
public class CustomAdapter extends
RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private ArrayList answerFirst;
private ArrayList answerSecond;
private Context context;
public CustomAdapter(Context context, ArrayList questionList, ArrayList
answerFirst, ArrayList answerSecond) {
this.context = context;
this.answerFirst = answerFirst;
this.answerSecond = answerSecond;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.test_items,
parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position)
{
holder.firstanswer.setText(answerFirst.get(position).toString());
holder.secondanswer.setText(answerSecond.get(position).toString());
}
@Override
public int getItemCount() {
return questionList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView question;
CheckBox firstanswer;
CheckBox secondanswer;
public MyViewHolder(View itemView) {
super(itemView);
question = (TextView) itemView.findViewById(R.id.question_text);
firstanswer = (CheckBox) itemView.findViewById(R.id.first_answer);
secondanswer = (CheckBox) itemView.findViewById(R.id.second_answer);
}
}
}
这是主要的activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_test);
final RecyclerView recyclerView = (RecyclerView)
findViewById(R.id.recyclerView_test);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);
final CustomAdapter customAdapter = new CustomAdapter(mainActivity.this, answerFirst,answerSecond);
recyclerView.setAdapter(customAdapter);
}
您可以通过在主 class 中使适配器 class 并在复选框的侦听器方法中滚动到所需位置来做到这一点,因为 RecyclerView 是一个全局变量,您可以访问它并且滚动到所需位置。
rv.scrollToPosition(youPositionInTheAdapter)
否则在构造函数中传递 RecyclerView 的实例并执行相同的操作
并通过 getAdapterPosition()
或者您可以像这样使用布局管理器来完成
rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter)