Issue
So I'm doing RecyclerView of buttons. And I don't know how/where to write the onClick for the button. I have a JSONArray to pass, and the text of the button will have the cName
.
So this is my item_unit.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_unit"
android:background="#000000"
android:textColor="#ffffff"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This is my ItemAdapter
//Here the stype is <CategoryItem> because this button will open
//new fragment to show the list of the items.
//So it's recyclerview item opens another recyclerview.
class CatAdapter(var btnList: ArrayList<CategoryItem>, var context: Context): RecyclerView.Adapter<ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
var v: View = LayoutInflater.from(context).inflate(R.layout.item_unit, parent, false)
return ViewHolder(v)
}
override fun getItemCount( ): Int {
return btnList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val data = btnList[position]
holder.itemBtn.text = data.name
}
}
class ViewHolder(view: View): RecyclerView.ViewHolder(view){
var itemBtn = view.findViewById<Button>(R.id.item_btn)
}
This recycler view is in a fragment. I need to
class MainActFrag : Fragment() {
var btnList = ArrayList<CategoryItem>()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
var view = inflater.inflate(R.layout.fragment_main_act, container, false)
//Recycler view
view.recyclerView.layoutManager = LinearLayoutManager(context)
//I have the url
var stringRequest = StringRequest(Request.Method.GET, url,
Response.Listener {
var jsonObject = JSONObject(it)
var jsonArrayCates =jsonObject.getJSONArray("category")
for(i in 0 until jsonArrayCats.length()){
var btns =jsonArrayCates.getJSONObject(i)
var iD = btns.getString("id")
var name = btns.getString("name")
var des = btns.getString("description")
btnList.add(CategoryItem(cID, cName, cDes))
}
val adapter = ItemAdapter(btnList, view.context)
recyclerView.adapter = adapter
},
Response.ErrorListener {}
)
Volley.newRequestQueue(view.context).add(stringRequest)
return view
}
}
So I don't know where to call each item onClick. If it's just a normal TextView item, I can do do it, but since I chose to make the item button
, I don't know the specific way to handle this. I thought it would be easy because the button is... clickable by nature?
Thank you!
Solution
add a bind function to the ViewHolder Class then you can put your click handler on each button
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val data = btnList[position]
holder.itemBtn.text = data.name
holder.bindButtons()
}
class ViewHolder(view: View): RecyclerView.ViewHolder(view){
var itemBtn = view.findViewById<Button>(R.id.item_btn)
fun bindButtons()
itemBtn.setOnClickListener{
//do you click event stuff
}
}
Answered By - Chooven
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.