RecyclerView的Item的单击事件
2018-10-19 06:30:09来源:博客园 阅读 ()
显示效果
RecyclerView 的每个Item的点击事件并没有像ListView一样封装在组件中,需要Item的单击事件时就需要自己去实现,在Adapter中为RecyclerView添加单击事件参考如下:
RecyclerView的使用方法请参考:RecyclerView的简单使用
第一步:
在RecyclerView的Adapyer中定义单击事件的回调接口:
/**
* 定义RecyclerView选项单击事件的回调接口
*/
public interface OnItemClickListener{
//参数(父组件,当前单击的View,单击的View的位置,数据)
void onItemClick(RecyclerView parent,View view, int position, String data);
}
第二步:
在RecyclerView的Adapyer中声明该接口,并提供setter方法:
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
第三步:
在RecyclerView的Adapyer类实现View.OnClickListener接口,并重写onClick(View view)方法,然后设置给接口的事件监听:
public class RvAdapter1 extends RecyclerView.Adapter<RvAdapter1.DataViewHolder> implements View.OnClickListener{
...
view.setOnClickListener(this);//设置监听器
...
@Override
public void onClick(View view) {
//根据RecyclerView获得当前View的位置
int position = recyclerView.getChildAdapterPosition(view);
//程序执行到此,会去执行具体实现的onItemClick()方法
if (onItemClickListener!=null){
onItemClickListener.onItemClick(recyclerView,view,position,mList.get(position));
}
}
...
}
第四步:
在MainActivity中通过Adapter设置每个Item的单击事件:
adapter.setOnItemClickListener(new RvAdapter1.OnItemClickListener() {
@Override
public void onItemClick(RecyclerView parent, View view, int position, String data) {
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}
});
参考代码
Adapter
/**
* Created by jzman on 2017/5/13 0013.
* RecycleView的Adapter
*/
public class RvAdapter1 extends RecyclerView.Adapter<RvAdapter1.DataViewHolder> implements View.OnClickListener{
private Context mContext;
private RecyclerView recyclerView;
private ArrayList<String> mList;
public RvAdapter1() {}
public RvAdapter1(Context mContext, ArrayList<String> mList) {
this.mContext = mContext;
this.mList = mList;
}
/**
* 用于创建ViewHolder
* @param parent
* @param viewType
* @return
*/
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
view.setOnClickListener(this);
//使用代码设置宽高(xml布局设置无效时)
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
DataViewHolder holder = new DataViewHolder(view);
return holder;
}
/**
* 绑定数据
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(DataViewHolder holder, int position) {
holder.tv_data.setText(mList.get(position));
}
/**
* 选项总数
* @return
*/
@Override
public int getItemCount() {
return mList.size();
}
@Override
public void onClick(View view) {
//根据RecyclerView获得当前View的位置
int position = recyclerView.getChildAdapterPosition(view);
//程序执行到此,会去执行具体实现的onItemClick()方法
if (onItemClickListener!=null){
onItemClickListener.onItemClick(recyclerView,view,position,mList.get(position));
}
}
/**
* 创建ViewHolder
*/
public static class DataViewHolder extends RecyclerView.ViewHolder{
TextView tv_data;
public DataViewHolder(View itemView) {
super(itemView);
tv_data = (TextView) itemView.findViewById(R.id.tv_recycle);
}
}
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
/**
* 定义RecyclerView选项单击事件的回调接口
*/
public interface OnItemClickListener{
//参数(父组件,当前单击的View,单击的View的位置,数据)
void onItemClick(RecyclerView parent,View view, int position, String data);
}
/**
* 将RecycleView附加到Adapter上
*/
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView= recyclerView;
}
/**
* 将RecycleView从Adapter解除
*/
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
this.recyclerView = null;
}
}
MainActivity
/**
* Created by jzman on 2017/5/13 0013.
*/
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
RvAdapter1 adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.rv);
//设置布局管理器
// rv.setLayoutManager(new LinearLayoutManager(this));//线性
rv.setLayoutManager(new GridLayoutManager(this,4));//线性
// rv.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));//线性
adapter = new RvAdapter1(this,initData());
adapter.setOnItemClickListener(new RvAdapter1.OnItemClickListener() {
@Override
public void onItemClick(RecyclerView parent, View view, int position, String data) {
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}
});
rv.setAdapter(adapter);
}
public static ArrayList<String> initData(){
ArrayList<String> arrayList = new ArrayList<>();
for (int i=0;i<50;i++){
arrayList.add("第"+i+"条数据");
}
return arrayList;
}
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Tornadofx学习笔记(2)——FxRecyclerView控件的打造 2020-02-06
- effective java 3th item1:考虑静态工厂方法代替构造器 2019-09-04
- javaska 2019-06-13
- java 环境配置 2019-04-20
- ToolStripMenuItem控件实现DatagridView行的上下移 2018-06-18
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash