package zhangphil.myrecyclerview;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
/*
* Android RecyclerView点击事件
*
*
* Android最新的RecyclerView没有像之前的Android ListView那样事先已经提供好了
* onItemClickListener()这样的回调事件响应用户点击。
* 如果要实现类似ListView的onItemClickListener,则需要我们自己写代码实现。实现的思路和解决方案有多种。
* 本案例的解决思路是这样的:
* 第1步:自己写一个public interface OnRecyclerViewItemClickListener,此接口用于回调响应。用户的点击事件。
* 第2步:把OnRecyclerViewItemClickListener new出来,其实就是完成里面的接口方法:onRecyclerViewItemClickListener()。我们将在onRecyclerViewItemClickListener方法体内完成类似ListView的onItemClickListener()。
* 第3步,关键点。在此处,实现RecyclerView点击响应回调的解决方案出现多种选择和道路,本案例选择在ViewHolder的构造函数中为参数View增加点击事件:
* public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_click(v);
}
});
}
然后我们在外部额外多写一个: _click(View v),然后调用_click(v)触发onRecyclerViewItemClickListener(注意:根据需求,此过程中可以携带和传递更多参数):
_click(View v) {
mOnRecyclerViewItemClickListener.onRecyclerViewItemClickListener(this, v, pos);
}
至此,就完成了为RecyclerView增加点击响应时间的功能。
*
*
* by Phil
*
* */
public class MainActivity extends ActionBarActivity {
// 增加点击事件接口 part 1
private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener = new OnRecyclerViewItemClickListener() {
// 具体的点击事件在此
@Override
public void onRecyclerViewItemClickListener(ViewHolder holder,
View view, int pos) {
Toast.makeText(getApplication(), pos + "", Toast.LENGTH_SHORT)
.show();
}
};
public interface OnRecyclerViewItemClickListener {
public void onRecyclerViewItemClickListener(ViewHolder holder,
View view, int pos);
}
private RecyclerView mRecyclerView;
// 测试用的数据集条目。
private final int SIZE = 30;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
// Orientation=LinearLayout.HORIZONTAL ,水平的“ListView”。
// VERTICAL,就是以前的ListView样式。
mLayoutManager.setOrientation(LinearLayout.HORIZONTAL);
mRecyclerView.setLayoutManager(mLayoutManager);
RecyclerView.Adapter mAdapter = new MyRecyclerViewAdapter();
mRecyclerView.setAdapter(mAdapter);
}
public class MyRecyclerViewAdapter extends
RecyclerView.Adapter<MyViewHolder> {
public MyRecyclerViewAdapter() {
super();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
TextView tv = new TextView(getApplication());
tv.setPadding(20, 10, 20, 10);
tv.setGravity(Gravity.CENTER);
MyViewHolder holder = new MyViewHolder(tv);
holder.text = tv;
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder viewHolder, int pos) {
viewHolder.text.setText("pos:" + pos);
viewHolder.pos = pos;
}
@Override
public int getItemCount() {
return SIZE;
}
}
private class MyViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public int pos;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_click(v);
}
});
}
// 增加点击事件接口 part 2,触发点击事件。
private final void _click(View v) {
mOnRecyclerViewItemClickListener.onRecyclerViewItemClickListener(
this, v, pos);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>