Reputation: 325
I have already get all the data in json file into recycle view. Instead of that i assign 4 buttons on top of the layout(I mentioned it in bellow). I want to filter my recycle view by evaluation book category when click each button. I haven't any idea to do this.
This is my Model class
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
)
This is my Adapter
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)
}
}
This is main 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)
}
Upvotes: 2
Views: 1960
Reputation: 1482
You only need to sort the list which you are setting to RecyclerView.
On every category button click, you have to sort the list and set to RecyclerView.
I had modified your code.
Add this code to your 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()
}
now call the showListByCatagory() from your activity on every button click.
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")
}
Now on Every button click, You will set a sorted list according to category.
Upvotes: 2