RecyclerView the delegation mode operation item using the Activity

RecyclerView ListView different and can not be obtained directly by clicking time for a single item setItemClicklistener, this time, we can define the adapter interface, and to get each Item click event to achieve the appropriate interface Activity, that is the standard commission mode.

Specifically, the following:

The first step in the definition of internal interfaces Adapter class, my own definition is as follows:

1
2
3
4
5
6
7
8
9
10
11
/ ** 
* interfaces implemented in the event
* /
public interface the SelectItem {
/ **
* The method defined in activity
* @param view view objects
* @param position item location
* /
void SELECT (View View, int position) ;
}

Step 2. Define a click event defined in onBindViewHolder method, and execution logic of the interface in the Click event

Big Box RecyclerView the item using delegates mode of operation in an Activity  
1
2
3
4
5
6
7
8
holder.mainView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != mSelectItem) {
mSelectItem.select(v, i);
}
}
});
In this way we get the view and position in the callback function, so you can operate the corresponding data in the Activity

The fourth step is provided delegate object definition defines a function in the adapter

1
2
3
public void setSelectItem(SelectItem selectItem) {
mSelectItem = selectItem;
}

Do this step, we can obtain the current location and view objects by clicking a class interface methods in rewriting Activity

The final step, rewritten interface functions in an Activity

1
2
3
4
5
6
pickItemAdapter.setSelectItem (new new PickItemAdapter.SelectItem () { 
@Override
public void SELECT (View View, int position) {
selectItem (position); // data operation function, their definition
}
});

So that we can easily get the current position of the item, the data is manipulated!

Guess you like

Origin www.cnblogs.com/dajunjun/p/11698524.html