时间:2021-05-21
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>感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Android开发订单流程view实例详解先看看最终效果图:怎么样,效果还是很不错的吧?群里有人说切四张图的、recycleview的、各种的都有啊,但是最简单
Android获取分享应用列表详解及实例如果在应用的AndroidManifest.xml中含有ACTION_SEND属性,那就证明该应用可以供第三方应用进行调
本文实例为大家分享了Android猜拳小游戏,供大家参考,具体内容如下简单的页面跳转和点击事件的实现...-->AndroidManifest.xmlAndro
Android中ActivityLifecycleCallbacks的实例详解以上就是使用ActivityLifecycleCallbacks的实例,代码注释写
本文实例讲述了Android基于广播事件机制实现简单定时提醒功能代码。分享给大家供大家参考,具体如下:1.Android广播事件机制Android的广播事件处理