android - 单击没有搜索视图的按钮集后如何过滤回收视图
问题描述
我已经将 json 文件中的所有数据放入回收视图中。取而代之的是,我在布局顶部分配了 4 个按钮(我在下面提到了它)。单击每个按钮时,我想按评估书类别过滤我的回收视图。我没有任何想法这样做。
这是我的模型课
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)
}
}
这是主要活动
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)
}
解决方案
您只需要对您设置为 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()
}
现在在每次单击按钮时从您的活动中调用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")
}
现在,在每次单击按钮时,您将根据类别设置排序列表。
推荐阅读
- python-3.x - cartopy rotate winds incorrectly?
- django - django - How can I prefill formset forms data using database query result?
- php - Add product weight and dimensions as order item meta data on WooCommerce admin manual orders
- java - ChronicleMap is accessing OS folders & files
- r - Convert string to HEX in R
- python - ModSim -Package - MacBookPro 上的安装问题 (2014)
- c++ - Get n-th character of string : AddressSanitizer: stack-use-after-scope
- scala - How filter seq of tuples by compare two values in tuple?
- java - Android: Fails to display RecyclerView correctly
- mysql - SQL COUNT if a specific value is found and output in the same line