通过Google和度娘的不懈努力,总结出6种方法(欢迎大家吐槽):
1.简单粗暴,Adapter中,直接在控件名后面setOnClickListener
viewHolder.allItemHome.setOnClickListener(new SingleOnClickListener() {
@Override
public void onSingleClick(View v) {
}
});
2.通过接口回调(有两种写法)
写法一:
(1)自定义一个继承自RecyclerView.Adapter的MyAdapter,在MyAdapter中定义如下接口,模拟ListView的OnItemClickListener:
public static interface OnItemClickListener {
void onItemClick(View view , int position);
}
(2)声明一个这个接口的变量
private OnItemClickListener mOnItemClickListener = null;
(3)在onCreateViewHolder()中为每个item添加点击事件
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
ViewHolder vh = new ViewHolder(view);
//将创建的View注册点击事件
view.setOnClickListener(this);
return vh;
}
(4)将点击事件转移给外面的调用者:
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
//注意这里使用getTag方法获取position
mOnItemClickListener.onItemClick(v,(int)v.getTag());
}
}
(5)注意上面调用接口的 onItemClick() 中的 v.getTag() 方法,这需要在onBindViewHolder()方法中设置和item的position
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.mTextView.setText(datas[position]);
//将position保存在itemView的Tag中,以便点击时进行获取
viewHolder.itemView.setTag(position);
}
(6)最后暴露给外面的调用者,定义一个设置Listener的方法():
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
以上所有步骤都发生在自定义的adapter中,典型的观察者模式,有点绕的地方在于,这里涉及到两个观察者模式的使用,view的setOnClickListener本来就是观察者模式,我们将这个观察者模式的事件监听传递给了我们自己的观察者模式。
在Activity中使用
mRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);
//创建默认的线性LayoutManager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
//如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
mRecyclerView.setHasFixedSize(true);
//创建并设置Adapter
mAdapter = new MyAdapter(data);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(View view , int position){
Toast.makeText(MainActivity.this, data[position], 600).show();
}
});
来自“泡在网上的日子”:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2647.html#commettop
方法二:
(1)(2)(6)和方法一相同,在Activity中使用也与方法一相同,剩下不同的地方:
在ViewHolder 中,
static class ViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.tv_name)
TextView tvName;
@Bind(R.id.tv_info)
TextView tvInfo;
@Bind(R.id.tv_money)
TextView tvMoney;
ViewHolder(View view) {
super(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemClickListener.onItemClick(view, getPosition());
}
});
ButterKnife.bind(this, view);
}
}
3.是在google sample时看到的一种实现方式,很简单,而且很速度
(1)再Activity中
public class XmlImpItemClickActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RecyclerView recyclerView = new RecyclerView(this);
setContentView(recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new Adapter());
}
public void onItemClick(View view) {
int childAdapterPosition = recyclerView.getChildAdapterPosition(view);
Toast.makeText(XmlImpItemClickActivity.this, "item click index = "+childAdapterPosition, Toast.LENGTH_SHORT).show();
}
}
(2)在Adapter中
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.xml_imp_click_item,parent,false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
TextView textView = (TextView) holder.itemView.findViewById(android.R.id.text1);
textView.setText("item" + position);
}
@Override
public int getItemCount() {
return 50;
}
static class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
}
}
}
(3)item布局文件
<?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="50dp"
android:orientation="vertical"
android:onClick="onItemClick"
android:background="?attr/selectableItemBackground">
<TextView
android:id="@android:id/text1"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
关键属性android:onClick="onItemClick",此方法在Activity中申明并实现了。
这里简单说下RecyclerView.getChildAdapterPosition()方法,此方法可以获取ItemView在Adapter中的位置,而我们onItemClick方法中的参数view刚好是我们点击的ItemView,但有一个缺陷,那就是xml布局中设置的onClick只能在Activity中去实现,不能在Fragment中实现。
来自简书://www.greatytc.com/p/2bbb025be972