Фильтр в расширяемом ReyclerView в Kotlin - PullRequest
1 голос
/ 06 февраля 2020

Я пытаюсь установить фильтр в расширяемом обзоре recylerview, но не могу достичь: Пожалуйста, найдите код ниже:

class adapter1(internal var context: Context, internal var mData: MutableList<faqBody?>,val 
 fragmentInterface: FragmentInterface) : RecyclerView.Adapter<adapter1.myViewHolder>()
 ,Filterable {

    internal var mfilter: NewFilter
    var orginallist: MutableList<faqBody?> = ArrayList()



    override fun getFilter(): Filter {
        return mfilter
    }

    init {
        mfilter = NewFilter(this@adapter1)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): adapter1.myViewHolder {
        val view =
            LayoutInflater.from(context).inflate(R.layout.recyclerview_adapter12, parent, false)
        return myViewHolder(view)
    }

    override fun onBindViewHolder(holder: adapter1.myViewHolder, position: Int) {
        val countryDataItem = mData[position]
        val intMaxNoOfChild = 0

        holder.country.text = countryDataItem!!.menuText
        val noOfChildTextViews = holder.linearLayout_childItems.childCount
        val noOfChild = countryDataItem.childItem.size

        for (index in 0 until noOfChildTextViews) {
            val currentTextView =
                holder.linearLayout_childItems.getChildAt(index) as TextView
            currentTextView.visibility = View.VISIBLE
        }

        if (noOfChild < noOfChildTextViews) {
            for (index in noOfChild until noOfChildTextViews) {
                val currentTextView =
                    holder.linearLayout_childItems.getChildAt(index) as TextView
                currentTextView.visibility = View.GONE
            }
        }
        for (textViewIndex in 0 until noOfChild) {
            val currentTextView =
                holder.linearLayout_childItems.getChildAt(textViewIndex) as TextView
            currentTextView.setText(countryDataItem.childItem[textViewIndex].menuText)
        }
    }

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

    inner class myViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
        View.OnClickListener {
        internal var country: TextView
        val linearLayout_childItems: LinearLayout

        init {
            country = itemView.findViewById(R.id.country)
            linearLayout_childItems = itemView.findViewById(R.id.ll_child_items)
            linearLayout_childItems.visibility = View.GONE
            var intMaxNoOfChild = 0

            for (index in mData.indices) {
                val intMaxSizeTemp = mData[index]!!.childItem.size
                if (intMaxSizeTemp > intMaxNoOfChild) intMaxNoOfChild = intMaxSizeTemp
            }
            for (indexView in 0 until intMaxNoOfChild) {
                val textView = TextView(context)
                textView.id = indexView
                textView.setPadding(5, 20, 0, 20)
                // textView.background = ContextCompat.getDrawable(context, R.drawable.background_sub_module_text)
                val layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                )
                textView.setOnClickListener(this)
                linearLayout_childItems.addView(textView, layoutParams)
            }
            country.setOnClickListener(this)


        }

        override fun onClick(view: View) {
            if (view.getId() == R.id.country) {
                if (linearLayout_childItems.visibility == View.VISIBLE) {
                    linearLayout_childItems.visibility = View.GONE
                } else {
                    linearLayout_childItems.visibility = View.VISIBLE
                }
            } else {
                val textViewClicked = view as TextView
                Toast.makeText(
                    context, "" + textViewClicked.text.toString(), Toast.LENGTH_SHORT
                ).show()

                fragmentInterface.onClick(textViewClicked.text.toString())


            }
        }


    }


    inner class NewFilter(var mAdapter: adapter1) : Filter() {
        override fun performFiltering(charSequence: CharSequence): FilterResults {

            orginallist.clear()
            orginallist.addAll(mData)


            val results = FilterResults()
            if (charSequence.length == 0) {

                mData.addAll(orginallist)


            } else {

                val filterPattern = charSequence.toString().toLowerCase().trim { it <= ' ' }
                for (i in 0..orginallist.size) {

                    val newList: ArrayList<childItem> = ArrayList<childItem>()

                    for (childmenu in orginallist[i]!!.childItem) {

                        if (childmenu.menuText.toLowerCase().contains(filterPattern)) {

                            newList.add(childmenu)
                        }
                    }

                    val faqBody = faqBody(menuText = orginallist[i]!!.menuText, childItem = newList)
                    mData.add(faqBody)
                }

            }

            results.values = mData
            results.count = mData.size

            return results
        }

        override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) { 

            mAdapter.notifyDataSetChanged()
        }

    }
}

Может кто-нибудь помочь по вышеуказанному. mData - это список faqBody, содержащий одинаковые chilitems.

faqbody (
menutext: String
childItem : List<childItem> 
)

chilitem(
menutext: String
)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...