Android recycleView的应用和点击事件实例详解
时间:2021-08-13 07:35:04|栏目:Android代码|点击: 次
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="http://schemas.android.com/apk/res/android" xmlns:tools="http://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>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇:Flutter质感设计之弹出菜单
栏 目:Android代码
本文标题:Android recycleView的应用和点击事件实例详解
本文地址:http://www.codeinn.net/misctech/166413.html