gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:46:54 25 4
gpt4 key购买 nike

我已经将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com