package com.haocent.android.recyclerview.click;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.haocent.android.recyclerview.R;
import java.util.ArrayList;
import java.util.List;
public class RcvClickActivity extends AppCompatActivity implements RcvClickAdapter.OnItemClickListener {
private static final String TAG = RcvClickActivity.class.getSimpleName();
private List mList = new ArrayList();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rcv_click_activity);
initData();
initView();
}
private void initData() {
mList.add("亚特兰大老鹰");
mList.add("夏洛特黄蜂");
mList.add("迈阿密热火");
mList.add("奥兰多魔术");
mList.add("华盛顿奇才");
mList.add("波士顿凯尔特人");
mList.add("布鲁克林篮网");
mList.add("纽约尼克斯");
mList.add("费城76人");
mList.add("多伦多猛龙");
mList.add("芝加哥公牛");
mList.add("克里夫兰骑士");
mList.add("底特律活塞");
mList.add("印第安纳步行者");
mList.add("密尔沃基雄鹿");
mList.add("达拉斯独行侠");
mList.add("休斯顿火箭");
mList.add("孟菲斯灰熊");
mList.add("新奥尔良鹈鹕");
mList.add("圣安东尼奥马刺");
mList.add("丹佛掘金");
mList.add("明尼苏达森林狼");
mList.add("俄克拉荷马城雷霆");
mList.add("波特兰开拓者");
mList.add("犹他爵士");
mList.add("金州勇士");
mList.add("洛杉矶快船");
mList.add("洛杉矶湖人");
mList.add("菲尼克斯太阳");
mList.add("萨克拉门托国王");
}
private void initView() {
RcvClickAdapter adapter = new RcvClickAdapter(this, this);
RecyclerView rcvClick = findViewById(R.id.rcv_click);
rcvClick.setLayoutManager(new LinearLayoutManager(this));
rcvClick.setHasFixedSize(true);
rcvClick.setAdapter(adapter);
adapter.setRcvClickDataList(mList);
}
@Override
public void onItemClick(String content) {
Toast.makeText(this, "你点击的是:" + content, Toast.LENGTH_SHORT).show();
}
}
RcvClickAdapter.java
package com.haocent.android.recyclerview.click;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.haocent.android.recyclerview.R;
import java.util.ArrayList;
import java.util.List;
public class RcvClickAdapter extends RecyclerView.Adapter {
private static final String TAG = RcvClickAdapter.class.getSimpleName();
private Context mContext;
private List mList = new ArrayList();
private OnItemClickListener mListener;
public RcvClickAdapter(Context context, OnItemClickListener listener) {
mContext = context;
mListener = listener;
}
public void setRcvClickDataList(List list) {
Log.d(TAG, "setRcvClickDataList: " + list.size());
mList = list;
notifyDataSetChanged();
}
@NonNull
@Override
public RcvClickViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.rcv_click_recycle_item, parent, false);
return new RcvClickViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RcvClickViewHolder holder, int position) {
final String content = mList.get(position);
holder.tvNum.setText(position + 1 + "");
holder.tvContent.setText(content);
// 第一种写法:直接在 Adapter 里写
// holder.itemView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(mContext, "你点击的是:" + content, Toast.LENGTH_SHORT).show();
// }
// });
// 第二种写法:将点击事件传到 Activity 里去写
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClick(content);
}
});
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
public class RcvClickViewHolder extends RecyclerView.ViewHolder {
TextView tvNum, tvContent;
public RcvClickViewHolder(View itemView) {
super(itemView);
tvNum = itemView.findViewById(R.id.tv_num);
tvContent = itemView.findViewById(R.id.tv_content);
}
}
public interface OnItemClickListener {
void onItemClick(String content);
}
}
代码部分总结
RecyclerView点击事件的两种写法:
①在Adapter里面直接对控件做点击事件
@Override
public void onBindViewHolder(@NonNull XxxViewHolder holder, int position) {
holder.控件名.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO:
}
});
}
②写接口,在Activity或Fragment上实现接口中定义的方法
public interface OnItemClickListener {
void onItemClick(参数类型 参数名 , ...);
}
private OnItemClickListener mListener;
public XxxAdapter(Context context, OnItemClickListener listener) {
mContext = context;
mListener = listener;
}
@Override
public void onBindViewHolder(@NonNull XxxViewHolder holder, int position) {
holder.控件名.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClick(content);
}
});
}
在 Activity 或 Fragment 上实现接口中定义的方法
public class XxxActivity extends AppCompatActivity implements XxxAdapter.OnItemClickListener
XxxAdapter adapter = new XxxAdapter(context, this);
@Override
public void onItemClick(String content) {
// TODO:
}
样式部分
① 在activity_main.xml内添加recyclerView,并将id设置为recyclerView
②新建rcv_click_recycle_item.xml文件,添加两个TextView组件,并将id分别设置为tv_num和tv_content
左侧列出了list中的数据,可上下滑动
右侧为点击效果,点击后有弹窗出现
作者:X-DAYTOY