vendredi 6 décembre 2019

How can I check all items when I click selectAll button in RecyclerView?

when I click selecAll just first item drawable change. so if I want all item will be change. How can I solve this problem? Please check activity and adapter.

(require add some more contents) (require add some more contents) (require add some more contents)(require add some more contents) (require add some more contents)(require add some more contents)(require add some more contents)(require add some more contents)(require add some more contents)(require add some more contents)

class CartViewActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {
var itemList: MutableList<CartItemDataVo> = arrayListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cart_view)

 selectAll.setOnClickListener {

    if(selectAll.isChecked){
                selectAll.buttonDrawable=it.context.getDrawable(R.drawable.check_box_active_cs)
            checkBox.buttonDrawable=it.context.getDrawable(R.drawable.check_box_active_cs)  }

            else {
                selectAll.buttonDrawable= it.context.getDrawable(R.drawable.check_box_no)
            checkBox.buttonDrawable= it.context.getDrawable(R.drawable.check_box_no)

        }

        }

swipeRefreshLo.setOnRefreshListener(this)

        itemList.add(CartItemDataVo("1", 1, 16800, "cart_doll"))
        itemList.add(CartItemDataVo("2", 1, 16800, "cart_cup"))
        itemList.add(CartItemDataVo("3", 1, 30000, "cart_perfume"))
        itemList.add(CartItemDataVo("4", 1, 16800, "cart_fan"))
        itemList.add(CartItemDataVo("5", 1, 16800, "cart_bear"))
        itemList.add(CartItemDataVo("6", 1, 16800, "cart_doll"))
        itemList.add(CartItemDataVo("7", 1, 16800, "cart_cup"))
        itemList.add(CartItemDataVo("8", 1, 30000, "cart_perfume"))
        itemList.add(CartItemDataVo("9", 1, 16800, "cart_fan"))
        itemList.add(CartItemDataVo("10", 1, 16800, "cart_bear"))

        val itemRecyclerAdapter = CartItemRecyclerAdapter(this, this , itemList)

        recycler_view.adapter = itemRecyclerAdapter
        recycler_view.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(applicationContext)
    }

    override fun onRefresh() {
        swipeRefreshLo.isRefreshing = false
    }
}

class CartItemDataVo(title: String, itemNumber: Int, pointValue: Int, imageView: String) {
    var title: String = title
    var itemNumber: Int = itemNumber
    var pointValue: Int = pointValue
    var image: String = imageView


}


class CartItemRecyclerAdapter(val context: Context, private var activity : Activity, private var dataList : MutableList<CartItemDataVo>) : RecyclerView.Adapter<CartItemRecyclerAdapter.Holder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
       val view = LayoutInflater.from(context).inflate(R.layout.cart_item_list,parent, false)
        return Holder(view)
    }


    override fun onBindViewHolder(holder: Holder, position: Int) {
       holder?.bind(dataList[position], context)

    }

    override fun getItemCount(): Int = dataList.size

    inner class Holder(itemView: View?): RecyclerView.ViewHolder(itemView!!) {

        var titleText = itemView?.findViewById(R.id.titleText) as TextView
        var temNumerTextt = itemView?.findViewById(R.id.textViewItemNumer) as TextView
        var pointValueText = itemView?.findViewById(R.id.pointValueText) as TextView
        var imageView= itemView?.findViewById(R.id.imageView)as ImageView
        var checkBox = itemView?.findViewById(R.id.checkBox) as CheckBox

        fun bind(data: CartItemDataVo, context: Context) {
            if(data.image !="") {

                val resourceId = context.resources.getIdentifier(data.image, "drawable", context.packageName)
                imageView?.setImageResource(resourceId)
            }else {
                imageView.setImageResource(R.mipmap.ic_launcher)
            }
            titleText?.text=data.title
            temNumerTextt?.text=data.itemNumber.toString() + "개"
            pointValueText?.text=data.pointValue.toString() + "P"
            checkBox?.setOnClickListener {
                if(checkBox.isChecked) {
                    checkBox.buttonDrawable= it.context.getDrawable(R.drawable.check_box_active_cs)
                    val layout = activity?.findViewById(R.id.layoutOrder) as LinearLayout
                    layout.visibility = View.VISIBLE
                }
                else {
                    checkBox.buttonDrawable= it.context.getDrawable(R.drawable.check_box_no)
                    val layout = activity?.findViewById(R.id.layoutOrder) as LinearLayout
                    layout.visibility = View.GONE
                }
            }
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire