Click is the most common and important means of interaction. The following describes how to click RecyclerView settings. Here is a common implementation, namely listener mode.
When a user clicks an item, the app can respond accordingly. Here we use the method of adding click listeners to achieve this effect.
Listener mode is widely used in Android development. I believe you are very familiar with the setOnClickListener method. The first thing recognized by beginners should be Button click event monitoring.
Design and add listeners
First, the listener is designed. Create a new OnItemClickListener1 interface.
In actual development, the name of the interface can be defined more meaningfully.
public interface OnItemClickListener { void onItemClick(Character c); void onItemLongClick(Character c); }
We put two methods in this interface. It is used to respond to click and long press events respectively.
Adapter hold listener
First, modify the VH class. We want the entire item to accept clicks.
private class VH extends RecyclerView.ViewHolder { View item; // We want to get the view of the whole item TextView tv1; TextView tv2; public VH(@NonNull View itemView) { super(itemView); item = itemView; tv1 = itemView.findViewById(R.id.tv1); tv2 = itemView.findViewById(R.id.tv2); } }
Next, modify the previous Adapter class. Hold the listener object onItemClickListener in the Adapter. In the onBindViewHolder method, set listening for item.
holder.item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(c); } } }); holder.item.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemLongClick(c); } return true; } });
Add the space judgment processing here. Prevent null pointers. In fact, it is the item View that receives the click event and transmits the event through the listener we designed. setOnLongClickListener returns true here. Consume the long click event.
The complete code of the LetterAdapter is as follows.
private class LetterAdapter extends RecyclerView.Adapter<VH> { private List<Character> dataList; private OnItemClickListener onItemClickListener; public LetterAdapter(List<Character> dataList) { this.dataList = dataList; // It's best to judge the space here } @NonNull @Override public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_letter, parent, false)); } @Override public void onBindViewHolder(@NonNull VH holder, int position) { final Character c = dataList.get(position); holder.tv1.setText(c.toString()); holder.tv2.setText(String.valueOf(Integer.valueOf(c))); holder.item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(c); } } }); holder.item.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemLongClick(c); } return true; } }); } @Override public int getItemCount() { return dataList.size(); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } }
Using listeners
Through the above efforts, our LetterAdapter has the function of listener. Now set listening for the list in the activity.
mLetterAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(Character c) { Toast.makeText(getApplicationContext(), "Click " + c, Toast.LENGTH_SHORT).show(); } @Override public void onItemLongClick(Character c) { Toast.makeText(getApplicationContext(), "Long click " + c, Toast.LENGTH_LONG).show(); } });
It can be seen here that the listener is set by the LetterAdapter, not the RecyclerView.
Run it, click the list to see the effect.
Another way to design a listener
Above, we designed a click listener through the interface. We can try to use abstract class instead of interface to design listener.
Create a new abstract class AbsOnItemClick.java.
public abstract class AbsOnItemClick { public abstract void onClick(char c); public void onLongClick(char c) { } }
There is an abstract method and an ordinary public method.
Follow the example and add this listener to the LetterAdapter class.
private AbsOnItemClick absOnItemClick; // Set listening in the onBindViewHolder method holder.item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (absOnItemClick != null) { absOnItemClick.onClick(c); } } }); holder.item.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if (absOnItemClick != null) { absOnItemClick.onLongClick(c); } return true; } }); // The setter method sets the listener public void setAbsOnItemClick(AbsOnItemClick absOnItemClick) { this.absOnItemClick = absOnItemClick; }
Set the listener in the activity.
mLetterAdapter.setAbsOnItemClick(new AbsOnItemClick() { @Override public void onClick(char c) { Log.d("rustApp", "[abs] onClick: " + c); } });
Run it to see the effect. You can see the log.
rustApp: [abs] onClick: h
Abstract classes and interfaces
By comparing interfaces, we can realize that abstract classes have their own uniqueness. An abstract class forces us to implement its abstract methods. The common method is to choose whether to rewrite or not. In actual development, we can choose whether to use interfaces or abstract classes according to our needs.