RecycleView in Android

An android recyclerView demo.

Features 1 : Event passing from adapter to activity

One way is to send otto messages,but a wiser way and the way I used here is to implement an interface which is declared in the adapter,then pass the activity’s handle to the adapter instance,then,when events are triggered,call the interface’s method,which will then call the implemented method in the activity next.

interface in the adapter

1
2
3
4
public static interface RecyclerViewImp {
void onItemClick(int position);
boolean onItemLongClick(int position);
}

implement the interface

1
public class RecyclerActivity extends Activity implements PersonAdapter.RecyclerViewImp

Pass the activity into the adapter

1
2
adapter = new PersonAdapter(personList);
adapter.setOnRecyclerViewListener(this);

When event is triggered

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
public void onClick(View v) {
if (null != recyclerViewImp) {
recyclerViewImp.onItemClick(this.getPosition());
}
}
@Override
public boolean onLongClick(View v) {
if (null != recyclerViewImp) {
return recyclerViewImp.onItemLongClick(this.getPosition());
}
return false;
}

View codes on github:

https://github.com/FengyangZhang/RecyclerDemo