RecyclerView确实拥有强大的功能及超级变换能力,可以用极少的代码实现从ListView,GridView及横向滑动的切换,但仍然是有不完美的地方,最显著的就是没有自带的OnitemClick和OnItemLongClick事件。今天学习的目标就是把RecyclerView实现点击事件,实现方法很简单,就是添加相应的接口。
代码中有相应注释,直接上代码:
public class MainActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
private List<TestData> mDatas;
private TestAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDatas();
initViews();
}
private void initDatas() {
mDatas = new ArrayList<TestData>();
TestData data = new TestData();
for (int i = 'A'; i < 'z'; i++) {
data.setTitle("Title" + (char) i);
data.setContent("Content" + (char) i);
mDatas.add(data);
}
}
private void initViews() {
mRecyclerView = (RecyclerView) findViewById(R.id.test_rcv);
mAdapter = new TestAdapter(this, mDatas);
mRecyclerView.setAdapter(mAdapter);
LinearLayoutManager ll = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
mRecyclerView.setLayoutManager(ll);
/* 在这里实现点击事件* */
mAdapter.onitemClick(new ItemEventImpl() {
@Override
public void onItemLongClick(View v, int position) {
Toast.makeText(MainActivity.this, "onItemLongClick:position"+position, Toast.LENGTH_LONG).show();
}
@Override
public void onItemClick(View v, int position) {
Toast.makeText(MainActivity.this, "onItemClick:position"+position, Toast.LENGTH_LONG).show();
}
});
}
}
--------------------------------------------
public class TestAdapter extends Adapter<TestViewHolder> {
private Context mContext;
private List<TestData> mDatas;
private ItemEventImpl mEventImpl;
public TestAdapter(Context mContext, List<TestData> mDatas) {
this.mContext = mContext;
this.mDatas = mDatas;
}
@Override
public TestViewHolder onCreateViewHolder(ViewGroup paramViewGroup, int paramInt) {
View v = LayoutInflater.from(mContext).inflate(R.layout.lv_item, null);
TestViewHolder holder = new TestViewHolder(v);
return holder;
}
public void onitemClick(ItemEventImpl mEventImpl) {// 对外公布adapter的item的点击事件
this.mEventImpl = mEventImpl;
}
@Override
public void onBindViewHolder(final TestViewHolder paramVH, final int paramInt) {
paramVH.titleTv.setText(mDatas.get(paramInt).getTitle());
paramVH.contentTv.setText(mDatas.get(paramInt).getContent());
if (mEventImpl != null) {
paramVH.itemView.setOnClickListener(new OnClickListener() {// 点击事件,实现Item的点击事件
@Override
public void onClick(View v) {
mEventImpl.onItemClick(paramVH.itemView, paramInt);
}
});
paramVH.itemView.setOnLongClickListener(new OnLongClickListener() {// 长按事件,实现Item的长按事件
@Override
public boolean onLongClick(View v) {
mEventImpl.onItemLongClick(paramVH.itemView, paramInt);
return false;
}
});
}
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return mDatas.size();
}
}
class TestViewHolder extends ViewHolder {
ImageView iconIv;
TextView titleTv;
TextView contentTv;
public TestViewHolder(View holder) {
super(holder);
iconIv = (ImageView) holder.findViewById(R.id.item_iv);
titleTv = (TextView) holder.findViewById(R.id.item_title);
contentTv = (TextView) holder.findViewById(R.id.item_content);
}
/**
* 添加事件接口
*/
public interface ItemEventImpl {
void onItemClick(View v, int position);// item点击事件
void onItemLongClick(View v, int position);// item的长按事件
}
}