AndroID recycleVIEw的应用和点击事件
效果图:
长按是删除:
下面代码:
MainActivity 类
public class MainActivity extends AppCompatActivity { RecyclerVIEw rv; List<String> mList = new ArrayList<>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); rv = (RecyclerVIEw) findVIEwByID(R.ID.rv); for (int i = 0; i < 1000; i++) { mList.add("条目" + i); } final MyAdapter adapter = new MyAdapter(this,mList); adapter.setonItemClickListener(new MyAdapter.OnItemClickListener() { @OverrIDe public voID onClick(int position) { Toast.makeText(getBaseContext(),"点击的是" + position + "的itemClick",Toast.LENGTH_SHORT).show(); } }); adapter.setonItemLongClickListener(new MyAdapter.OnItemLongClickListener() { @OverrIDe public voID onLongClick(int position) { //长按删除数据 mList.remove(position); adapter.notifyDataSetChanged(); } }); rv.setAdapter(adapter); //布局管理器 rv.setLayoutManager(new StaggeredGrIDLayoutManager(5,StaggeredGrIDLayoutManager.VERTICAL)); } public static class VH extends RecyclerVIEw.VIEwHolder { TextVIEw tv; public VH(VIEw itemVIEw) { super(itemVIEw); tv = (TextVIEw) itemVIEw.findVIEwByID(androID.R.ID.text1); tv.setBackgroundResource(R.drawable.bg); } }}
MyAdapter.java类
public class MyAdapter extends RecyclerVIEw.Adapter<MainActivity.VH> { Context mContext; List<String> mList; public MyAdapter(Context context,List<String> List) { this.mContext = context; this.mList = List; } @OverrIDe public MainActivity.VH onCreateVIEwHolder(VIEwGroup parent,int vIEwType) { return new MainActivity.VH(VIEw.inflate(mContext,androID.R.layout.simple_List_item_1,null)); } @OverrIDe public voID onBindVIEwHolder(MainActivity.VH holder,final int position) { holder.tv.setText(mList.get(position)); //自己做item点击 holder.itemVIEw.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if (onItemClickListener != null) onItemClickListener.onClick(position); } }); holder.itemVIEw.setonLongClickListener(new VIEw.OnLongClickListener() { @OverrIDe public boolean onLongClick(VIEw v) { if (onItemLongClickListener != null) onItemLongClickListener.onLongClick(position); //返回false会在长安结束后继续点击 return true; } }); } @OverrIDe public int getItemCount() { return mList.size(); } /** * 点击 */ public interface OnItemClickListener { voID onClick(int position); } OnItemClickListener onItemClickListener; public voID setonItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } public interface OnItemLongClickListener { voID onLongClick(int position); } OnItemLongClickListener onItemLongClickListener; public voID setonItemLongClickListener(OnItemLongClickListener onItemLongClickListener) { this.onItemLongClickListener = onItemLongClickListener; }}
activity_main.xml类
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="@R_404_6822@://schemas.androID.com/apk/res/androID" xmlns:tools="@R_404_6822@://schemas.androID.com/tools" androID:ID="@+ID/activity_main" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context="com.example.administrator.lesson10_recyclervIEw2.MainActivity"> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/rv" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" /></relativeLayout>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android recycleView的应用和点击事件实例详解全部内容,希望文章能够帮你解决Android recycleView的应用和点击事件实例详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)