通過Google和度娘的不懈努力书在,總結(jié)出6種方法(歡迎大家吐槽):
1.簡單粗暴心俗,Adapter中吠勘,直接在控件名后面setOnClickListener
viewHolder.allItemHome.setOnClickListener(new SingleOnClickListener() {
@Override
public void onSingleClick(View v) {
}
});
2.通過接口回調(diào)(有兩種寫法)
寫法一:
(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添加點(diǎn)擊事件
@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);
//將創(chuàng)建的View注冊點(diǎn)擊事件
view.setOnClickListener(this);
return vh;
}
(4)將點(diǎn)擊事件轉(zhuǎn)移給外面的調(diào)用者:
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
//注意這里使用getTag方法獲取position
mOnItemClickListener.onItemClick(v,(int)v.getTag());
}
}
(5)注意上面調(diào)用接口的 onItemClick() 中的 v.getTag() 方法若治,這需要在onBindViewHolder()方法中設(shè)置和item的position
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.mTextView.setText(datas[position]);
//將position保存在itemView的Tag中慨蓝,以便點(diǎn)擊時進(jìn)行獲取
viewHolder.itemView.setTag(position);
}
(6)最后暴露給外面的調(diào)用者,定義一個設(shè)置Listener的方法():
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
以上所有步驟都發(fā)生在自定義的adapter中端幼,典型的觀察者模式礼烈,有點(diǎn)繞的地方在于,這里涉及到兩個觀察者模式的使用婆跑,view的setOnClickListener本來就是觀察者模式济丘,我們將這個觀察者模式的事件監(jiān)聽傳遞給了我們自己的觀察者模式。
在Activity中使用
mRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);
//創(chuàng)建默認(rèn)的線性LayoutManager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
//如果可以確定每個item的高度是固定的洽蛀,設(shè)置這個選項(xiàng)可以提高性能
mRecyclerView.setHasFixedSize(true);
//創(chuàng)建并設(shè)置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();
}
});
來自“泡在網(wǎng)上的日子”: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時看到的一種實(shí)現(xiàn)方式,很簡單近哟,而且很速度
(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>
關(guān)鍵屬性android:onClick="onItemClick"驮审,此方法在Activity中申明并實(shí)現(xiàn)了。
這里簡單說下RecyclerView.getChildAdapterPosition()方法吉执,此方法可以獲取ItemView在Adapter中的位置疯淫,而我們onItemClick方法中的參數(shù)view剛好是我們點(diǎn)擊的ItemView,但有一個缺陷戳玫,那就是xml布局中設(shè)置的onClick只能在Activity中去實(shí)現(xiàn)熙掺,不能在Fragment中實(shí)現(xiàn)。
來自簡書:http://www.reibang.com/p/2bbb025be972