編輯:關於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>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
Android 通過Socket 和服務器通訊,是一種比較常用的通訊方式,時間比較緊,說下大致的思路,希望能幫到使用socket 進行通信的人(1)開啟一個線程發送消息&
Android WebView加載了Chromium動態庫之後,就可以啟動Chromium渲染引擎了。Chromium渲染引擎由Browser、Render和GPU三端組
應用的啟動 啟動方式 通常來說,在安卓中應用的啟動方式分為兩種:冷啟動和熱啟動。 1、冷啟動:當啟動應用時,後台沒有該應用的進程,這時系統會重新創建一個新
我推薦的網站,都是我在學習Android 開發過程中發現的好網站,給初學者一些建議,少走一些彎路。Android Developers作為一個Android 開發者,官網