1

Objective

I want to expand new items in a RecyclerView at the same time, and the old selected items will be automatically collapsed.

What I done, it can be like this >>

The items expand and collapse on click but do not automatically collapse if another item is expanded

image1

Actually, what I want to make it like this >>

Any expanded item should be automatically collapse when another item is expanded.

image2

What I had researched

  1. From this stack overflow had done what I want, but I don't how to convert into Kotlin, basically as I know it store the previous position then compare to the current position, through the If-Else Statement to determine and perform the action.

  2. There are one more stack overflow, it also from java, slightly understand, briefly know the whole concept, but cannot go deeply explain for myself line-by-line.

  3. This Github Sample is pretty matched my needs, but code styling quite complicated, and not fexible enough for me to add other feature and design.

Question

  1. Why the previousPosition need to be -1?
  2. How should separate currentPosition and previousPosition, and store the previousPosition to different object and value?
  3. If the previousPosition is workable, how should I implement into my project?
  4. If available, can share any related resources to me?
     like: screen shot the part of sample code, or other resources

Code

product_list_item.kt

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.google.android.material.floatingactionbutton.FloatingActionButton
import org.json.JSONArray
import org.json.JSONObject

class ViewPartActivity : AppCompatActivity() {
    private lateinit var newRecylerview : RecyclerView
    private lateinit var newArrayList : ArrayList<Product>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_part)
        val btnInsertView: FloatingActionButton = findViewById(R.id.btnInsertView)
        btnInsertView.setOnClickListener {
            val insertViewIntent = Intent(this, AddPartActivity::class.java)
            startActivity(insertViewIntent)
        }
        getData()
    }

    private fun getData() {
        val url = "WEBAPI"
        val requestQueue: RequestQueue = Volley.newRequestQueue(this)
        val jsonObjectRequest: JsonObjectRequest = JsonObjectRequest(Request.Method.GET, url, null,
            { response ->
                val jsonArray: JSONArray = response.getJSONArray("tbl_product")
                val namelist = ArrayList<String>()
                val categorylist = ArrayList<String>()
                val quantitylist = ArrayList<String>()
                val pricelist = ArrayList<String>()

                for (x in 0 until jsonArray.length()) {
                    val jsonObject: JSONObject = jsonArray.getJSONObject(x)
                    val name: String = jsonObject.getString("NAME")
                    val category: String = jsonObject.getString("CATOGORYID")
                    val quantity: String = jsonObject.getString("QUANTITY")
                    val price: String = jsonObject.getString("PRICE")

                    namelist.add(name)
                    categorylist.add(category)
                    quantitylist.add(quantity)
                    pricelist.add(price)
                }
                newRecylerview =findViewById(R.id.recyclerView)
                newRecylerview.layoutManager = LinearLayoutManager(this)
                newRecylerview.setHasFixedSize(true)
                newArrayList = arrayListOf<Product>()
                for(i in namelist.indices){
                    val product = Product(namelist[i],categorylist[i],quantitylist[i],pricelist[i])
                    newArrayList.add(product)
                }

                val adapter = ProductAdapter(newArrayList)
                newRecylerview.adapter = adapter
            }, { error ->
                Toast.makeText(this, error.message, Toast.LENGTH_LONG).show()
            })
        requestQueue.add(jsonObjectRequest)
    }
}

ProductAdapter.kt

class ProductAdapter(private val productList : ArrayList<Product>) : RecyclerView.Adapter<ProductAdapter.MyViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.product_list_item, parent,false)
        return MyViewHolder(itemView)
    }


    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val currentItem = productList[position]

        holder.tvProductName.text = currentItem.name
        holder.tvProductCategory.text= currentItem.category
        holder.tvProductQuantity.text=currentItem.quantity
        holder.tvProductPrice.text= "RM "+currentItem.price

        val isVisible : Boolean = currentItem.visibility
        holder.constraintLayout.visibility = if (isVisible) View.VISIBLE else View.GONE

        holder.tvProductName.setOnClickListener{
            currentItem.visibility =!currentItem.visibility
            notifyItemChanged(position)
        }
    }


    override fun getItemCount(): Int {
        return productList.size
    }
    class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
        val tvProductName: TextView = itemView.findViewById(R.id.productName)
        val tvProductCategory : TextView = itemView.findViewById(R.id.productCategory)
        val tvProductQuantity : TextView = itemView.findViewById(R.id.productQuantity)
        val tvProductPrice : TextView = itemView.findViewById(R.id.productPrice)
        val constraintLayout : ConstraintLayout = itemView.findViewById(R.id.expandedLayout)
    }
}

Product.kt

data class Product(
    var name: String, var category: String, var quantity: String, var price: String, var visibility: Boolean = false
)

Promise

  • Once I get the solution, I would try my best to explain the code more deeply and update at here.
Low Jung Xuan
  • 25
  • 1
  • 7
  • https://github.com/thoughtbot/expandable-recycler-view You can check out this library and programmatically collapse all other group when one is clicked. – Ishaan Kumar Oct 20 '21 at 19:01
  • You said you didn't know how to convert to kotlin. Paste Java Code in .kt file. Android Studio will do the conversion. MAGIC!! – Ishaan Kumar Oct 20 '21 at 19:05
  • I think it cannot work, some of the code it convert not accurately. The more that I debugged, the more I get confused. – Low Jung Xuan Oct 21 '21 at 02:43

0 Answers0