Recyclerview上的全部按钮清单不起作用

时间:2018-11-26 08:15:18

标签: android android-recyclerview kotlin

我有带复选框的recyclerview,我想使用按钮检查所有数据。我已经尝试过this教程,但是当我单击该按钮时,该日志称为isSelectedAll函数,但是无法选中该复选框。我的代码有什么问题?

这是我的适配器代码

var isSelectedAll = false

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListApproveDeatilViewHolder {
    val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.activity_list_approve_row, parent, false)

    return ListApproveDeatilViewHolder(itemView)
}

private lateinit var mSelectedItemsIds: SparseBooleanArray

fun selectAll() {
    Log.e("onClickSelectAll", "yes")
    isSelectedAll = true
    notifyDataSetChanged()
}

override fun onBindViewHolder(holder: ListApproveDeatilViewHolder, position: Int) {
    val approve = dataSet!![position]

    holder.soal.text = approve.title
    holder.kategori.text = approve.kategori

    if (!isSelectedAll){
        holder.checkBox.setChecked(false)
    } else {
        holder.checkBox.setChecked(true)
    }
}

这是我的活动代码

override fun onCreate(savedInstanceState: Bundle?) {
     private var adapter: ListApproveDetailAdapter? = null

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_list_approve)
    ButterKnife.bind(this)

    getData()

    // this is my button onclick code

    select.setOnClickListener(){
        if (select.getText().toString().equals("Select all")){
            Toast.makeText(this, "" + select.getText().toString(), Toast.LENGTH_SHORT).show()
            adapter?.selectAll()
            select.setText("Deselect all")

        } else {
            Toast.makeText(this, "" + select.getText().toString(), Toast.LENGTH_SHORT).show()
            select.setText("Select all")

        }
    }

}
//this is for get my data for the recyclerview
fun getData() {
    val created_by = intent.getStringExtra(ID_SA)
    val tgl_supervisi = intent.getStringExtra(TGL_SURVEY)
    val no_dlr = intent.getStringExtra(NO_DLR)

    API.getListApproveDetail(created_by, tgl_supervisi, no_dlr).enqueue(object : Callback<ArrayList<ListApprove>> {
        override fun onResponse(call: Call<ArrayList<ListApprove>>, response: Response<ArrayList<ListApprove>>) {
            if (response.code() == 200) {
                tempDatas = response.body()
                Log.i("Data Index History", "" + tempDatas)
                recyclerviewApprove?.setHasFixedSize(true)
                recyclerviewApprove?.layoutManager = LinearLayoutManager(this@ListApproveActivity)
                recyclerviewApprove?.adapter = ListApproveDetailAdapter(tempDatas)
                adapter?.notifyDataSetChanged()
            } else {
                Toast.makeText(this@ListApproveActivity, "Error", Toast.LENGTH_LONG).show()
            }

            swipeRefreshLayout.isRefreshing = false

        }

        override fun onFailure(call: Call<ArrayList<ListApprove>>, t: Throwable) {
            Toast.makeText(this@ListApproveActivity, "Error", Toast.LENGTH_SHORT).show()

            swipeRefreshLayout.isRefreshing = false

        }
    })

}

this is the log when i click the button

谢谢您的帮助:)

3 个答案:

答案 0 :(得分:0)

正如我在评论中已经提到的那样,您正在使用两个不同的适配器实例。 现在,我看到您已全局声明adapter。 只需按如下所示修改您的代码,并确保response.body()包含数据即可:

 if (response.code() == 200) {
            tempDatas = response.body()
            Log.i("Data Index History", "" + tempDatas)
            recyclerviewApprove?.setHasFixedSize(true)
            recyclerviewApprove?.layoutManager = LinearLayoutManager(this@ListApproveActivity)
            adapter = ListApproveDetailAdapter(tempDatas)
            recyclerviewApprove?.adapter=adapter
        } else {
            Toast.makeText(this@ListApproveActivity, "Error", Toast.LENGTH_LONG).show()
        }

答案 1 :(得分:0)

在模型类中添加一个变量。

var isSelect : Boolean

在您的selectAll()方法中,更新适配器列表并通知适配器。

编辑: 在适配器类中。

if (approve.isSelect){
        holder.checkBox.setChecked(true)
    } else {
        holder.checkBox.setChecked(false)
    }

希望这对您有所帮助。

OR

如果您使用的是 AndroidX ,则应使用一种recyclerview功能。

  

androidx.recyclerview.selection

     

RecyclerView加载项库,提供对项目选择的支持。的   该库同时支持触摸和鼠标驱动的选择。   开发人员保留对视觉表示的控制权,   控制选择行为的政策(例如符合条件的项目   进行选择,以及可以选择多少个项目。)

来自here的引用

答案 2 :(得分:0)

我正在发布有关演示项目实施的答案。我尚未修改您的代码,但根据您的要求,我已经完成了此操作。

MainActivity类:

class MainActivity : AppCompatActivity() {

var selectAll: Boolean = false;

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


    val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) as RecyclerView

    val btnSelectAll = findViewById<Button>(R.id.btnSelectAll) as Button


    //adding a layoutmanager
    recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)


    //crating an arraylist to store users using the data class user
    val users = ArrayList<User>()

    //adding some dummy data to the list
    users.add(User("Piyush", "Ranchi"))
    users.add(User("Mehul", "Chennai"))
    users.add(User("Karan", "TamilNadu"))
    users.add(User("Bela", "Kolkata"))

    //creating our adapter
    val adapter = CustomAdapter(users, selectAll)

    //now adding the adapter to recyclerview
    recyclerView.adapter = adapter

    btnSelectAll.setOnClickListener {

        if (!selectAll) {
            selectAll = true
        } else {
            selectAll = false
        }
        adapter?.selectAllCheckBoxes(selectAll)
    }
    }
    }

用户类别:

data class User(val name: String, val address: String)

适配器类:

class CustomAdapter(val userList: ArrayList<User>, val selectAll: Boolean) :
RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

var selectAllA = selectAll;


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
    return ViewHolder(v)
}


override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
    holder.textViewName.text = userList[position].name;

    if (!selectAllA){
        holder.checkBox.setChecked(false)
    } else {
        holder.checkBox.setChecked(true)
    }
}

//this method is giving the size of the list
override fun getItemCount(): Int {
    return userList.size

}

//the class is hodling the list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    val textViewName = itemView.findViewById(R.id.textViewUsername) as TextView
    val checkBox = itemView.findViewById(R.id.checkbox) as CheckBox
}

 fun selectAllCheckBoxes(selectAll: Boolean) {
     selectAllA = selectAll
     notifyDataSetChanged()
}

}