[英]How can I check every value in my RecyclerView adapter?
I have a custom RecyclerView adapter that I instantiate from my onCreate method that contains 9 fixed data points: 我有一个自定义的RecyclerView适配器,该适配器从包含9个固定数据点的onCreate方法实例化:
GridRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
/* Dummy cell data */
String[] data = {"", "", "", "", "", "", "", "", ""};
...
/* Init RecyclerView */
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
int numColumns = 3;
recyclerView.setLayoutManager(new GridLayoutManager(this, numColumns));
adapter = new GridRecyclerAdapter(this, data, numColumns, player1, player2);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}
The clickListener in my ViewHolder sets the text to "X" when clicked. 单击后,ViewHolder中的clickListener会将文本设置为“ X”。
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView cellTextView;
ViewHolder(View itemView) {
super(itemView);
cellTextView = (TextView) itemView.findViewById(R.id.textView);
itemView.setOnClickListener(v -> {
/* Set cell TextView to "X" */
cellTextView.setText("X");
});
}
}
On each click, I want to check if a certain combination of itemViews posses a certain value. 每次单击时,我要检查itemViews的特定组合是否具有特定值。 For example, if itemViews with positions 1, 2 and 3 have a TextView equal to "X", then show an AlertDialog.
例如,如果位置1、2和3的itemView的TextView等于“ X”,则显示AlertDialog。 I imagine I should be able to modify the String[] I passed in the constructor with each click.
我想我应该能够通过每次单击修改在构造函数中传递的String []。
setOnClickListener
from your ViewHolder
and move it to your Adapter's
onBindViewHolder
. setOnClickListener
从ViewHolder
并将其移动到您的Adapter's
onBindViewHolder
。 X
on one of your Views
see if the change in the checked positions is valid for showing your AlertDialog
Views
上设置X
时, Views
选中位置的更改是否对显示AlertDialog
有效 public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ViewHolder> {
List<Integer> xPositions = new ArrayList<>();
// ... all the needed stuff.
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.itemView.setOnClickListener(v -> {
holder.showX();
xPositions.add(holder.getAdapterPosition());
checkIfShouldShowAlert();
});
}
void checkIfShouldShowAlert() {
// do your checks on xPositions and show the AlertDialog if needed
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView cellTextView;
ViewHolder(View itemView) {
super(itemView);
cellTextView = (TextView) itemView.findViewById(R.id.textView);
}
void showX(){
cellTextView.setText("X");
}
}
}
Remember to add a mechanism that would prevent consecutive clicks on the same views (otherwise the xPositions
array would grow and grow). 请记住要添加一种机制,以防止连续单击相同的视图(否则
xPositions
数组会越来越大)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.