Reputation: 5667
I have a custom RecyclerView adapter that I instantiate from my onCreate method that contains 9 fixed data points:
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.
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. For example, if itemViews with positions 1, 2 and 3 have a TextView equal to "X", then show an AlertDialog. I imagine I should be able to modify the String[] I passed in the constructor with each click.
Upvotes: 1
Views: 1036
Reputation: 31458
setOnClickListener
from your ViewHolder
and move it to your Adapter's
onBindViewHolder
.X
on one of your Views
see if the change in the checked positions is valid for showing your 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).
Upvotes: 2