android - 如何在没有搜索 View 的情况下单击一组按钮后过滤回收 View

标签 android kotlin android-recyclerview gson retrofit2

我已经将 json 文件中的所有数据获取到回收 View 中。相反,我在布局顶部分配了 4 个按钮(我在下面提到过)。我想在单击每个按钮时按评估书籍类别过滤我的回收 View 。我不知道该怎么做。

这是我的模型类

data class Model(
    val author: Author,
    val cover_page: String,
    val description: String,
    val file_size: Int,
    val id: Int,
    val isbn: String,
    val language: String,
    val name: String,
    val no_of_pages: String,
    val price: String,
    val category: String
)

data class Author(
    val name: String
)

这是我的适配器

class RecycleAdapter(val context: Context) : RecyclerView.Adapter<RecycleAdapter.MyViewHolder>() {

    var dataList : List<Model> = listOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.adapter_layout, parent, false)
        return MyViewHolder(view)
    }


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

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

        holder.name.text = dataList.get(position).name
        holder.description = dataList.get(position).description
        Glide.with(context).load(dataList.get(position).cover_page)
            .apply(RequestOptions().centerCrop())
            .into(holder.cover)
        holder.author.text = dataList.get(position).author.name
        holder.price.text = dataList.get(position).price
        holder.language = dataList.get(position).language
        holder.isbn = dataList.get(position).isbn
        holder.numbOfPages = dataList.get(position).no_of_pages

        holder.layoutPay.setOnClickListener {

            val intent = Intent(context, BookDetails::class.java)
            intent.putExtra("name", dataList.get(position).name)
            intent.putExtra("description", dataList.get(position).description)
            intent.putExtra("cover", dataList.get(position).cover_page)
            intent.putExtra("author", dataList.get(position).author.name)
            intent.putExtra("price", dataList.get(position).price)
            intent.putExtra("language", dataList.get(position).language)
            intent.putExtra("isbn", dataList.get(position).isbn)
            intent.putExtra("pages_count", dataList.get(position).no_of_pages)

            val options =
                ActivityOptions.makeCustomAnimation(
                    context,
                    android.R.anim.fade_in,
                    android.R.anim.fade_out
                )
            context.startActivity(intent, options.toBundle())
        }
    }

    fun setData(dataList: List<Model>) {
        this.dataList = dataList
        //Collections.sort(dataList)
        notifyDataSetChanged()
    }

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val name: TextView = itemView.findViewById(R.id.txt_title)
        val cover: ImageView = itemView.findViewById(R.id.img_cover)
        val author: TextView = itemView.findViewById(R.id.txt_author)
        val price: TextView = itemView.findViewById(R.id.txt_price)
        lateinit var description: String
        lateinit var language: String
        lateinit var isbn: String
        lateinit var numbOfPages: String
        var layoutPay: ConstraintLayout = itemView.findViewById(R.id.layout_buy)
    }
}

这是主要 Activity

class MainActivity : AppCompatActivity() {

    lateinit var recyclerView: RecyclerView
    lateinit var recyclerAdapter: RecycleAdapter

    private val ACTIVITY_NUM:Int = 0

    //lateinit var dialog: Dialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //dialog.setContentView(R.layout.activity_detailview)

        btn_feeds_all.setOnClickListener {
            btn_feeds_all.setBackgroundResource(R.drawable.click_button)
            btn_feeds_biographic.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_adventure.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_children.setBackgroundResource(R.drawable.non_click_button)
            btn_cook.setBackgroundResource(R.drawable.non_click_button)
        }

        btn_feeds_biographic.setOnClickListener {
            btn_feeds_biographic.setBackgroundResource(R.drawable.click_button)
            btn_feeds_all.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_adventure.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_children.setBackgroundResource(R.drawable.non_click_button)
            btn_cook.setBackgroundResource(R.drawable.non_click_button)
        }

        btn_feeds_adventure.setOnClickListener {
            btn_feeds_adventure.setBackgroundResource(R.drawable.click_button)
            btn_feeds_biographic.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_all.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_children.setBackgroundResource(R.drawable.non_click_button)
            btn_cook.setBackgroundResource(R.drawable.non_click_button)
        }

        btn_feeds_children.setOnClickListener {
            btn_feeds_children.setBackgroundResource(R.drawable.click_button)
            btn_feeds_biographic.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_adventure.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_all.setBackgroundResource(R.drawable.non_click_button)
            btn_cook.setBackgroundResource(R.drawable.non_click_button)
        }

        btn_cook.setOnClickListener {
            btn_cook.setBackgroundResource(R.drawable.click_button)
            btn_feeds_biographic.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_adventure.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_all.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_children.setBackgroundResource(R.drawable.non_click_button)
        }

        btnLike.setOnClickListener {

        }


        recyclerView = findViewById(R.id.recyclerView)
        recyclerAdapter = RecycleAdapter(this)
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        recyclerView.adapter = recyclerAdapter
        val apiInterface = JsonApi.create().getData()

        apiInterface.enqueue(object : Callback<List<Model>> {
            override fun onResponse(call: Call<List<Model>>?, response: Response<List<Model>>?) {

                if (response?.body() != null)
                    recyclerAdapter.setData(response.body()!!)
            }

            override fun onFailure(call: Call<List<Model>>?, t: Throwable?) {
            }
        })

        bottomNavigationView()
    }
    private fun bottomNavigationView() {
        val bottomNavigationViewEx = findViewById<BottomNavigationViewEx>(R.id.bottomNaBar)
        BottomNavigationViewHelper.enableNavigation(this, bottomNavigationViewEx)
        val menu = bottomNavigationViewEx.getMenu()
        val menuItem = menu.getItem(ACTIVITY_NUM)
        menuItem.setChecked(true)
    }

enter image description here

最佳答案

您只需对设置到RecyclerView的列表进行排序即可。

每次单击类别按钮时,您都必须对列表进行排序并设置为 RecyclerView。

我修改了你的代码。

将此代码添加到您的 RecyclerAdapter

//save all model list to this list
var allDataList : List<Model> = listOf()

//this will hold the sorted list which will be filter by category
var dataList : List<Model> = listOf()

//call this when you get data from API call
fun setData(dataList: List<Model>) {
    //set all list to allDataList
    this.allDataList = dataList

    //Show initial all list
    showListByCatagory("all")
}


//call this to show the sorted list by category
fun showListByCatagory(category: String){

    // Filter the list by its category and set to recyclerView
    when(category){
        "all" -> {
            this.dataList = allDataList
        }

        "biographic" -> {
            this.dataList = allDataList.filter { it.category == "biographic" }
        }

        "adventure" -> {
            this.dataList = allDataList.filter { it.category == "adventure" }
        }

        "children" -> {
            this.dataList = allDataList.filter { it.category == "children" }
        }

        "cook"-> {
            this.dataList = allDataList.filter { it.category == "cook" }
        }

        else ->{
            this.dataList = allDataList
        }
    }

    //Notify RecyclerView to change List.
    notifyDataSetChanged()
}

现在,每次点击按钮时都会从 Activity 中调用 showListByCatagory()

 btn_feeds_all.setOnClickListener {
            btn_feeds_all.setBackgroundResource(R.drawable.click_button)
            btn_feeds_biographic.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_adventure.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_children.setBackgroundResource(R.drawable.non_click_button)
            btn_cook.setBackgroundResource(R.drawable.non_click_button)

            // call adapter's sort function.
            recyclerAdapter.showListByCatagory("all")
        }

        btn_feeds_biographic.setOnClickListener {
            btn_feeds_biographic.setBackgroundResource(R.drawable.click_button)
            btn_feeds_all.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_adventure.setBackgroundResource(R.drawable.non_click_button)
            btn_feeds_children.setBackgroundResource(R.drawable.non_click_button)
            btn_cook.setBackgroundResource(R.drawable.non_click_button)

            // call adapter's sort function.
            recyclerAdapter.showListByCatagory("biographic")
        }

现在,每次单击按钮时,您都将根据类别设置一个排序列表。

关于android - 如何在没有搜索 View 的情况下单击一组按钮后过滤回收 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58253923/

相关文章:

java - 新建的 SQLitedatabase 表不存在

android - 加载时将 Recyclerview 项目对齐到中心

java - 具有不同布局的 Android RecyclerView 不起作用

java - 转到在 RecyclerView 上选择的项目

Android BACK 键点击两次

android - 使用 LinearLayout.LayoutParams 时出现 ClassCastException

华氏转换的 Kotlin 打印问题

algorithm - 如何确保 RNG 结果出现的次数最少?

android - RecyclerView 与 ListAdapter

android notifyItemRangeInserted 禁用自动滚动