Android利用RecyclerView實(shí)現(xiàn)全選、置頂和拖拽功能示例
前言
今天給大家分享是如何在RecyclerView實(shí)現(xiàn)全選,ItemTouchHelper實(shí)現(xiàn)側(cè)滑刪除,拖拽功能。比較基礎(chǔ)。關(guān)于RecyclerView的強(qiáng)大,就不多說(shuō)了。在Android L SDK發(fā)布的新API中最有意思的就是RecyclerView 和 CardView了, 按照官方的說(shuō)法, RecyclerView 一個(gè)ListView 的一個(gè)更高級(jí)更靈活的一個(gè)版本, 可以自定義的東西太多了。
效果:
RecyclerView實(shí)現(xiàn)全選,ItemTouchHelper實(shí)現(xiàn)側(cè)滑刪除,拖拽功能
使用RecyclerView,首先我們需要依賴(lài)
compile 'com.android.support:recyclerview-v7:23.+'
項(xiàng)目結(jié)構(gòu):
項(xiàng)目結(jié)構(gòu)
主要是把選擇的存儲(chǔ)在HashMap記錄下來(lái),通知用eventbus發(fā)送。下面我們一步一步來(lái)實(shí)現(xiàn)這種效果.
方法如下
1、我們新建一個(gè)MainActivity 布局
public class MainActivity extends Activity { private RecyclerView recyclerView; private CheckBox checkbox; private TextView selected; private ListAdapter adapter; private EventBus event; private boolean isChange = false; private ArrayList<Book> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initView() { event = EventBus.getDefault(); event.register(this); recyclerView = (RecyclerView) findViewById(R.id.recyclerview); checkbox = (CheckBox) findViewById(R.id.checkbox); selected = (TextView) findViewById(R.id.selected); } private void initData() { for (int i = 0; i < 20; i++) { Book model = new Book(); model.setId(i); model.setName("商品" + i); model.setDesc("描述" + i); list.add(model); } adapter = new ListAdapter(list, event); recyclerView.setHasFixedSize(true); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { try { HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>(); int count = 0; if (isChecked) { isChange = false; } for (int i = 0, p = list.size(); i < p; i++) { if (isChecked) { map.put(i, true); count++; } else { if (!isChange) { map.put(i, false); count = 0; } else { map = adapter.getMap(); count = map.size(); } } } selected.setText("已選" + count + "項(xiàng)"); adapter.setMap(map); } catch (Exception e) { e.printStackTrace(); } } }); adapter.setOnItemClickListener(new ListAdapter.ItemClickListener() { @Override public void onItemClick(RecyclerView.ViewHolder holder, int positon) { Log.e("onItemClick", "" + positon); } @Override public void onItemLongClick(final RecyclerView.ViewHolder holder, final int positon) { Log.e("onItemLongClick", "" + positon); } }); } public void onEventMainThread(SelectEvent event) { int size = event.getSize(); if (size < list.size()) { isChange = true; checkbox.setChecked(false); } else { checkbox.setChecked(true); isChange = false; } selected.setText("已選" + size + "項(xiàng)"); } @Override protected void onDestroy() { super.onDestroy(); event.unregister(this); } }
2、我們建一個(gè)ListAdapter適配器
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ItemViewHolder> { private List<Book> mItems; private List<Book> selected; public HashMap<Integer, Boolean> map; private EventBus eventBus; public ListAdapter(List<Book> mItems, EventBus eventBus) { this.mItems = mItems; this.eventBus = eventBus; map = new HashMap<>(); selected = new ArrayList<>(); init(); } private void init() { if (null == mItems || mItems.size() <= 0) { return; } for (int i = 0, p = mItems.size(); i < p; i++) { map.put(i, false); } } @Override public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main_item, parent, false); ItemViewHolder itemViewHolder = new ItemViewHolder(view); return itemViewHolder; } @Override public void onBindViewHolder(final ItemViewHolder holder, final int position) { if (null == mItems || mItems.size() <= 0) { return; } holder.name.setText(mItems.get(position).getName()); holder.desc.setText(mItems.get(position).getDesc()); holder.checkBox.setTag(new Integer(position));//防止劃回來(lái)時(shí)選中消失 if (map != null) { ((ItemViewHolder) holder).checkBox.setChecked((map.get(position))); } else { ((ItemViewHolder) holder).checkBox.setChecked(false); } holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int mFlags = (Integer) view.getTag(); if (map != null) { if (map.get(position)) { map.put(position, false); eventBus.post(new SelectEvent(selected(map))); } else { map.put(mFlags, Boolean.TRUE); eventBus.post(new SelectEvent(selected(map))); } } } }); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mItemClickListener.onItemClick(holder,holder.getAdapterPosition()); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { mItemClickListener.onItemLongClick(holder,holder.getAdapterPosition()); return true; } }); } private int selected(HashMap<Integer, Boolean> map) { int size = 0; for (Integer key : map.keySet()) { if(map.get(key)){ size++; } } return size; } @Override public int getItemCount() { return mItems == null? 0 :mItems.size(); } public static class ItemViewHolder extends RecyclerView.ViewHolder{ public final CheckBox checkBox; public final TextView name; public final TextView desc; public ItemViewHolder(View itemView) { super(itemView); checkBox = (CheckBox) itemView.findViewById(R.id.checkbox); name = (TextView) itemView.findViewById(R.id.tv_name); desc = (TextView) itemView.findViewById(R.id.tv_desc); } } public HashMap<Integer, Boolean> getMap() { return map; } public void setMap(HashMap<Integer, Boolean> map) { this.map = map; notifyDataSetChanged(); } /** * 點(diǎn)擊事件和長(zhǎng)按事件 */ public interface ItemClickListener{ void onItemClick(RecyclerView.ViewHolder holder , int position); void onItemLongClick(RecyclerView.ViewHolder holder , int position); } private ItemClickListener mItemClickListener; public void setOnItemClickListener(ItemClickListener listener){ this.mItemClickListener=listener; } }
3、我們新建實(shí)體Book和SelectEvent
package com.aikaifa.checkall.bean; public class Book { private int id; private String name; private String desc; public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Book() { } } public class SelectEvent { private int size; public SelectEvent(int size) { this.size = size; } public int getSize() { return size; } }
4、建立checkbox_selector.xml選中樣式
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/checkbox_pressed" android:state_checked="true"/> <item android:drawable="@mipmap/checkbox_normal" android:state_checked="false"/> <item android:drawable="@mipmap/checkbox_normal"/> </selector>
5、建立一個(gè)activity_main.xml布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="10dp" android:text="全選" android:textColor="#969696" android:textSize="12sp" /> <TextView android:id="@+id/selected" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="已選0項(xiàng)" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
activity_main_item布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/item" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:clickable="true" android:focusable="true"> <RelativeLayout android:id="@+id/rl_app" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerHorizontal="true"> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_gravity="center_vertical" android:button="@drawable/checkbox_selector" /> </RelativeLayout> <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/rl_app" android:gravity="center" android:text="name" android:textColor="#ffffff" /> <TextView android:id="@+id/tv_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_name" android:gravity="center" android:textColor="#f5f5f5" android:textSize="9sp" /> </RelativeLayout>
這樣關(guān)于RecyclerView實(shí)現(xiàn)全選,ItemTouchHelper實(shí)現(xiàn)側(cè)滑刪除,拖拽功能就完成了。
示例代碼下載:
項(xiàng)目地址:https://github.com/88ios/RecyclerViewCheckAll
本地下載:點(diǎn)擊這里
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開(kāi)發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- android ListView和GridView拖拽移位實(shí)現(xiàn)代碼
- android 大圖片拖拽并縮放實(shí)現(xiàn)原理
- Android實(shí)現(xiàn)讓圖片在屏幕上任意移動(dòng)的方法(拖拽功能)
- Android自定義可拖拽的懸浮按鈕DragFloatingActionButton
- Android自定義ListView實(shí)現(xiàn)仿QQ可拖拽列表功能
- android RecyclerView側(cè)滑菜單,滑動(dòng)刪除,長(zhǎng)按拖拽,下拉刷新上拉加載
- Android中在GridView網(wǎng)格視圖上實(shí)現(xiàn)item拖拽交換的方法
- Android使用RecycleView實(shí)現(xiàn)拖拽交換item位置
- Android自定義View實(shí)現(xiàn)可以拖拽的GridView
- Android仿美團(tuán)拖拽效果實(shí)例代碼
相關(guān)文章
Android WebView 詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android WebView 詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04基于Flutter制作一個(gè)火箭發(fā)射動(dòng)畫(huà)
北京時(shí)間10月16日0時(shí)23分,神舟十三號(hào)飛船成功發(fā)射,為慶祝這一喜事,本文將用Flutter制作一個(gè)火箭發(fā)射動(dòng)畫(huà),感興趣的小伙伴可以動(dòng)手試一試2022-03-03Android實(shí)現(xiàn)調(diào)用系統(tǒng)分享功能示例的總結(jié)
這篇文章主要介紹了通過(guò)Android調(diào)用系統(tǒng)分享文本信息、單張圖片、多個(gè)文件和指定分享到微信、QQ,同時(shí)分享圖片和文字的功能示例,小編覺(jué)得挺不錯(cuò),一起跟隨小編過(guò)來(lái)看看吧2018-05-05Android自定義view實(shí)現(xiàn)圓形waveview
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)圓形waveview,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android 快速實(shí)現(xiàn)防止網(wǎng)絡(luò)重復(fù)請(qǐng)求&按鈕重復(fù)點(diǎn)擊的方法
下面小編就為大家分享一篇Android 快速實(shí)現(xiàn)防止網(wǎng)絡(luò)重復(fù)請(qǐng)求&按鈕重復(fù)點(diǎn)擊的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android編程實(shí)現(xiàn)Toast只顯示最后一條的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)Toast只顯示最后一條的方法,結(jié)合實(shí)例形式總結(jié)了Toast只顯示最后一條的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08Android判斷網(wǎng)絡(luò)狀態(tài)的代碼
這篇文章主要為大家詳細(xì)介紹了Android判斷網(wǎng)絡(luò)狀態(tài)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android將內(nèi)容分享到QQ和微信實(shí)例代碼
這篇文章主要介紹了Android將內(nèi)容分享到QQ和微信實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06Android開(kāi)發(fā)MQTT協(xié)議的模型及通信淺析
這篇文章主要W為大家介紹了Android開(kāi)發(fā)MQTT協(xié)議的模型及通信淺析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03