RecycleView的使用+自定义监听事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecycleView的使用+自定义监听事件相关的知识,希望对你有一定的参考价值。
最近使用了RecycleView,发下这个控件十分好用,替代了listView和GridView,包括适配器都很方便。
效果如下:
具体使用如下所示:
1 compile ‘com.android.support:recyclerview-v7:25.3.1‘
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bway.recycleviewtest.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" > <!--android:orientation="horizontal"--> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity
package com.bway.recycleviewtest; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<MyImage> mList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); RecyclerView mRecycler = (RecyclerView) findViewById(R.id.recycler); /** * listView */ LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); mRecycler.setLayoutManager(manager); HomeAdapter adapter = new HomeAdapter(this,mList); mRecycler.setAdapter(adapter); /** * GridView */ /* GridLayoutManager manager1 = new GridLayoutManager(this,3,GridLayoutManager.VERTICAL,false); mRecycler.setLayoutManager(manager1); HomeAdapter adapter = new HomeAdapter(this,mList); mRecycler.setAdapter(adapter);*/ /** * 瀑布流 */ /* StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); mRecycler.setLayoutManager(manager); HomeAdapter adapter = new HomeAdapter(this,mList);*/ /** * 设置自定义的条目点击事件 */ adapter.setOnItemClickLisitenter(new RecycleViewLisitenter.onItemClickLisitenter() { @Override public void onItemClick(View v, int position) { Toast.makeText(MainActivity.this,position+"",Toast.LENGTH_SHORT).show(); } }); /** * 设置自定义的条目长按监听 */ adapter.setOnItemLongClickLisitenter(new RecycleViewLisitenter.onItemLongClickLisitenter() { @Override public void onItemLongClick(View v, int position) { Toast.makeText(MainActivity.this,position+"long",Toast.LENGTH_SHORT).show(); } }); mRecycler.setAdapter(adapter); } /** * 初始化数据 */ private void initData() { mList = new ArrayList<>(); mList.add(new MyImage("第一张","https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1497864641&di=7a7a381bca514d830aea92d66a7f9024&src=http://pic.58pic.com/58pic/13/61/00/61a58PICtPr_1024.jpg")); mList.add(new MyImage("第二张","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497874818536&di=6ddb63c3e66ac32feb5762d8d5c1fd42&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F13%2F60%2F97%2F48Q58PIC92r_1024.jpg")); mList.add(new MyImage("第三张","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497874818535&di=9d786693e181801447e49c37bc2bc627&imgtype=0&src=http%3A%2F%2Fi2.sanwen.net%2Fdoc%2F1608%2F704-160PQ43458.png")); mList.add(new MyImage("第四张","https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1497864813&di=64e84994c2f8864a69dab604d6f4d3a0&src=http://p4.gexing.com/qqpifu/20120821/1212/50330ab3e811f.jpg")); mList.add(new MyImage("第五张","http://up.qqjia.com/z/18/tu19139_7.jpg")); } }
HomeAdapter
package com.bway.recycleviewtest; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener { Context context; List<MyImage> mList; private RecycleViewLisitenter.onItemClickLisitenter onItem; private RecycleViewLisitenter.onItemLongClickLisitenter onLongItem; public HomeAdapter(Context context, List<MyImage> mList) { this.context = context; this.mList = mList; } public void setOnItemLongClickLisitenter(RecycleViewLisitenter.onItemLongClickLisitenter onLongItem){ this.onLongItem = onLongItem; } public void setOnItemClickLisitenter(RecycleViewLisitenter.onItemClickLisitenter onItem){ this.onItem = onItem; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item, null); view.setOnClickListener(this); view.setOnLongClickListener(this); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { /** * 给itemView设置tag */ holder.itemView.setTag(position); MyImage myImage = mList.get(position); Glide.with(context).load(myImage.getUrl()).into(holder.mImage); holder.mTxt.setText(myImage.getTitle()); } @Override public int getItemCount() { return mList.size(); } /** * item点击事件 * @param v */ @Override public void onClick(View v) { if(onItem!=null){ onItem.onItemClick(v, (Integer) v.getTag()); } } @Override public boolean onLongClick(View v) { if(onLongItem!=null){ onLongItem.onItemLongClick(v, (Integer) v.getTag()); } return true; } class ViewHolder extends RecyclerView.ViewHolder{ ImageView mImage; TextView mTxt; public ViewHolder(View itemView) { super(itemView); mImage = (ImageView) itemView.findViewById(R.id.image); mTxt = (TextView) itemView.findViewById(R.id.txt_title); } } }
RecycleViewLisitenter(自定义条目点击事件)
package com.bway.recycleviewtest; import android.view.View; public class RecycleViewLisitenter { /** * RecycleView的条目点击监听 */ public interface onItemClickLisitenter{ void onItemClick(View v,int position); }; /** * RecycleView的条目长按点击监听 */ public interface onItemLongClickLisitenter{ void onItemLongClick(View v,int position); }; }
以上是关于RecycleView的使用+自定义监听事件的主要内容,如果未能解决你的问题,请参考以下文章