Android 一個(gè)RecyclerView實(shí)現(xiàn)篩選列表

效果圖

choice.gif

簡介

如上圖展示的內(nèi)容,篩選條件的功能很常見,一般情況如果條件很多,那么布局文件就會(huì)寫的很復(fù)雜,這篇文章可以提供一個(gè)簡潔的方案,布局文件只用一個(gè)RecyclerView就可以了。當(dāng)然,處理邏輯可能要少費(fèi)點(diǎn)功夫,不過這些邏輯可以復(fù)用,如果有多個(gè)地方用到就省很多事了。

代碼分析

先看看Bean類的處理:
數(shù)據(jù)中有幾個(gè)必須要添加的屬性
type:用于區(qū)分是標(biāo)題item還是內(nèi)容item,標(biāo)題item也可以分很多類。
choice:標(biāo)記item是否選中
multiChoice:這一類型標(biāo)簽是不是多選
allChoice:標(biāo)簽item是不是全選按鈕(根據(jù)需求調(diào)整)

public class GridItemBean {

    /**
     * type:
     * 標(biāo)題item 自定義
     * 內(nèi)容item 默認(rèn)為0
     */
    private int type;
    /**
     * 標(biāo)題(標(biāo)題item)
     */
    private String title;

    private String id;
    private String name;
    /**
     * 內(nèi)容item 是否選擇(內(nèi)容item)
     */
    private boolean choice;
    /**
     * 是否是多選(標(biāo)題item)
     */
    private boolean multiChoice;
    /**
     * 是否是全選按鈕(內(nèi)容item)
     */
    private boolean allChoice;

    public GridItemBean(int type, String title) {
        this.type = type;
        this.title = title;
    }

    public GridItemBean(int type, String title, boolean multiChoice) {
        this.type = type;
        this.title = title;
        this.multiChoice = multiChoice;
    }

    public GridItemBean(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public GridItemBean(String id, String name, boolean allChoice) {
        this.id = id;
        this.name = name;
        this.allChoice = allChoice;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isChoice() {
        return choice;
    }

    public void setChoice(boolean choice) {
        this.choice = choice;
    }

    public boolean isMultiChoice() {
        return multiChoice;
    }

    public void setMultiChoice(boolean multiChoice) {
        this.multiChoice = multiChoice;
    }

    public boolean isAllChoice() {
        return allChoice;
    }

    public void setAllChoice(boolean allChoice) {
        this.allChoice = allChoice;
    }

    @Override
    public String toString() {
        return "name='" + name + '\'';
    }
}

既然布局只是一個(gè)RecyclerView,那么邏輯都在adapter中了,首先是adapter的ViewHolder,分為兩類,標(biāo)題類和內(nèi)容標(biāo)簽,根據(jù)bean里面的type來區(qū)分,這部分沒什么要多說的。

class ChoiceAdapter : ChoiceGridAdapter() {

    override fun initData(mData: MutableList<GridItemBean>) {
        this.mData = mData
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == 0){
            TabHolder(View.inflate(parent?.context , R.layout.item_choice_tab , null))
        }else{
            TitleHolder(View.inflate(parent?.context , R.layout.item_choice_title , null))
        }
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == 0){
            (holder as TabHolder).bindData(position)
        }else{
            (holder as TitleHolder).bindData(position)
        }
    }

    inner class TabHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        fun bindData(position: Int)= with(itemView){
            var data = mData[position]
            tv_item_tab?.text = data.name
            if (data.isChoice){
                tv_item_tab?.setBackgroundResource(R.mipmap.search_label_bg_sel02)
            }else{
                tv_item_tab?.setBackgroundResource(R.mipmap.search_label_bg_nor)
            }
            setOnClickListener {
                if(data.isChoice){
                    mData[position].isChoice = false
                    notifyItemChanged(position)
                }else{
                    changeStatus(position)
                }
            }
        }

    }

    inner class TitleHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        fun bindData(position: Int) = with(itemView){
            tv_item_title?.text = mData[position].title
        }

    }

}

下面是處理邏輯,寫在了adapter的基類中,方便復(fù)用,布局畢竟是都不一樣的,但是邏輯可以通用,具體要按項(xiàng)目需求來定,靈活修改。

abstract class ChoiceGridAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var mData: MutableList<GridItemBean> = mutableListOf()

    abstract fun initData(mData: MutableList<GridItemBean>)

    override fun getItemViewType(position: Int): Int {
        return mData[position].type
    }

    /**
     * 修改選擇條目
     */
    fun changeStatus(position: Int){
        var first = 0               // 同類型的第一條數(shù)據(jù)位置
        var last = mData.size   //  同類型的最后一條數(shù)據(jù)位置
        for(index in position downTo 0){
            if (mData[index].type != 0){
                first = index
                break
            }
        }
        for (index in (position + 1) until mData.size){
            if (mData[index].type != 0){
                last = index
                break
            }
        }
        if (last > first){
            if (mData[first].isMultiChoice){
                // 是多選
                if (mData[position].isAllChoice){ // 全選按鈕
                    for (index in first until last){
                        mData[index].isChoice = false
                    }
                    mData[position].isChoice = true
                    notifyDataSetChanged()
                }else{
                    for (index in first until last){
                        // 重置全選按鈕
                        if (mData[index].isAllChoice){
                            mData[index].isChoice = false
                            notifyItemChanged(index)
                            break
                        }
                    }
                    mData[position].isChoice = true
                    notifyItemChanged(position)
                }
            }else{
                // 是單選
                var currentIndex = 0
                for (index in first until last){
                    // 查找當(dāng)前選擇的位置
                    if (mData[index].isChoice){
                        mData[index].isChoice = false
                        currentIndex = index
                        break
                    }
                }
                notifyItemChanged(currentIndex)
                mData[position].isChoice = true
                notifyItemChanged(position)
            }
        }
    }

    /**
     * 查找選擇的條目
     */
    fun getChoiceItem(type: Int): GridItemBean?{
        var first = 0               // 同類型的第一條數(shù)據(jù)位置
        var last = mData.size   //  同類型的最后一條數(shù)據(jù)位置
        for (index in mData.indices){
            if (mData[index].type == type){
                first = index
                break
            }
        }
        for (index in (first + 1) until mData.size){
            if (mData[index].type != 0){
                last = index
                break
            }
        }
        for (index in first until last){
            if (mData[index].isChoice){
                return mData[index]
                break
            }
        }
        return null
    }

    /**
     * 查找多選選擇的條目
     */
    fun getMultiChoiceItem(type: Int): MutableList<GridItemBean>?{
        var multiData = mutableListOf<GridItemBean>()
        var first = 0               // 同類型的第一條數(shù)據(jù)位置
        var last = mData.size   //  同類型的最后一條數(shù)據(jù)位置
        for (index in mData.indices){
            if (mData[index].type == type){
                first = index
                break
            }
        }
        for (index in (first + 1) until mData.size){
            if (mData[index].type != 0){
                last = index
                break
            }
        }
        var allChoice = false
        for (index in first until last){
            // 判斷是否全選
            if (mData[index].isAllChoice){
                allChoice = mData[index].isChoice
                break
            }
        }
        if (allChoice){
            // 全選
            for (index in first until last){
                if (mData[index].type == 0 && !mData[index].isAllChoice){
                    multiData.add(mData[index])
                }
            }
        }else{
            // 非全選
            for (index in first until last){
                if (mData[index].isChoice){
                    multiData.add(mData[index])
                }
            }
        }
        return multiData
    }

    /**
     * 重置
     */
    fun clearChoice(){
        for (index in mData.indices){
            mData[index].isChoice = false
            notifyDataSetChanged()
        }
    }

}

changeStatus()方法是用來更新item選中的,更新的時(shí)候,要先查出當(dāng)前選的這個(gè)標(biāo)簽的所有同類型的坐標(biāo),第一個(gè)和最后一個(gè),邏輯就是查上一個(gè)標(biāo)題類和下一個(gè)標(biāo)題類,中間所有的標(biāo)簽都是這個(gè)類型的,然后根據(jù)是多選還是單選,更新標(biāo)簽的choice屬性。
getChoiceItem()和getMultiChoiceItem()是獲取單選或者多選選中的標(biāo)簽,也是要先獲取同類標(biāo)簽的第一個(gè)和最后一個(gè)位置,遍歷獲取。

最后看看activity中數(shù)據(jù)的準(zhǔn)備(根據(jù)需求調(diào)整)

override fun initData() {

        mData.add(GridItemBean(1, "類型(多選)" , true))
        mData.add(GridItemBean("1" , "全部" , true))
        mData.add(GridItemBean("2" , "11"))
        mData.add(GridItemBean("3" , "12"))
        mData.add(GridItemBean("4" , "13"))
        mData.add(GridItemBean("4" , "14"))

        mData.add(GridItemBean(2 , "標(biāo)準(zhǔn)(單選)"))
        mData.add(GridItemBean("5" , "20"))
        mData.add(GridItemBean("6" , "21"))
        mData.add(GridItemBean("7" , "22"))
        mData.add(GridItemBean("8" , "23"))
        mData.add(GridItemBean("8" , "24"))

        mData.add(GridItemBean(3 , "屬性(多選)", true))
        mData.add(GridItemBean("5" , "30"))
        mData.add(GridItemBean("6" , "31"))
        mData.add(GridItemBean("7" , "32"))
        mData.add(GridItemBean("8" , "33"))
        mData.add(GridItemBean("8" , "34"))

        mAdapter = ChoiceAdapter()
        mAdapter?.initData(mData)

        RecyclerViewUtil.initGrid(this, recycler_grid , mAdapter,4)

        var layoutManager : GridLayoutManager = recycler_grid?.layoutManager as GridLayoutManager
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup(){
            override fun getSpanSize(position: Int): Int {
                var type = mAdapter?.getItemViewType(position)
                return if (type == 0) 1 else 4
            }
        }

    }

    fun getData(view: View){

        var data1 = mAdapter?.getMultiChoiceItem(1)
        LogUtil.logShow(data1?.toString())
        var data2 = mAdapter?.getChoiceItem(2)
        LogUtil.logShow(data2?.toString())
        var data3 = mAdapter?.getMultiChoiceItem(3)
        LogUtil.logShow(data3?.toString())

        tv_show?.text = "data1 = " + data1.toString() + "\ndata2 = " + data2.toString() + "\ndata3 = " + data3.toString()
    }


    fun backData(view: View){
        mAdapter?.clearChoice()
        tv_show?.text = ""
    }

源碼地址

https://github.com/QQzs/ChoiceView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 內(nèi)容 抽屜菜單 ListView WebView SwitchButton 按鈕 點(diǎn)贊按鈕 進(jìn)度條 TabLayo...
    小狼W閱讀 1,666評(píng)論 0 10
  • 最近做了一個(gè)Android UI相關(guān)開源項(xiàng)目庫匯總,里面集合了OpenDigg 上的優(yōu)質(zhì)的Android開源項(xiàng)目庫...
    OpenDigg閱讀 17,589評(píng)論 6 222
  • 抽屜菜單 MaterialDrawer★7337 - 安卓抽屜效果實(shí)現(xiàn)方案 Side-Menu.Android★3...
    彬哥狠逍遙閱讀 5,996評(píng)論 4 59
  • 這一周總的來說周一到周五是平淡普通的工作日,周末是玩耍忙碌的2天。 雙十二血拼 這周正好趕上雙十二,就算沒什么要買...
    周唐閱讀 337評(píng)論 1 0
  • 我想去遠(yuǎn)方 找尋自由的他鄉(xiāng) 舒暢萎靡的思慮 讓心在綠海里徜徉 我想去遠(yuǎn)方 放棄不存在的幻想 聞聞梔子花的香 親親荷...
    釋空沙閱讀 228評(píng)論 0 1

友情鏈接更多精彩內(nèi)容