简体   繁体   中英

How to enable next item in RecyclerView?

I want the next item in the RecyclerView can be clicked. For example, if item 1 has score > 10, then the item 2 can be clicked, but the item 3,4,5,6... cannot be clicked. If item 2 has scrore > 10,then the item 3 can be clicked,but the item 4,5,6,7... cannot be clicked. If item 3 has score > 10, then the item 4 can be clicked, but the item 5,6,7,8... cannot be clicked.

在此处输入图像描述 class RecyclerView

class MathCategoryDetailActivity : AppCompatActivity() {

private var progressBar: ProgressBar? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_math_category_detail)

    progressBar = findViewById(R.id.progressBar)


    recycler_math_category_detail.layoutManager = GridLayoutManager(this, 2)
    recycler_math_category_detail.setHasFixedSize(true)

    val adapter = MathCategoryDetailAdapter(
        this,
        DBHelperOther.getInstance(this).allCategoryDetail(Common.selectedCategory!!.id)
    )

    Common.selectedCategory!!.id
    recycler_math_category_detail.addItemDecoration(
        SplacesItemDecoration(
            4
        )
    )

    recycler_math_category_detail.adapter = adapter
}


}

Adapter of RecyclerView

class MathCategorySpecifyDetailAdapter(
    internal var context: Context,
    internal var categoryDetailList: List<MathCategorySpecifyDetail>) :

RecyclerView.Adapter<MathCategorySpecifyDetailAdapter.MyViewHolder1>() {
private var dialog: Dialog? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder1 {
    val itemView =
        LayoutInflater.from(context).inflate(R.layout.activity_math_category_specify_detail_adapter, parent, false)

    return MyViewHolder1(itemView)
}

override fun getItemCount(): Int {
    return categoryDetailList.size
}

override fun onBindViewHolder(holder: MyViewHolder1, position: Int) {
    holder.card_category_detail_2.isEnabled = false
    holder.txt_category_specify_detail_2_name.isEnabled = false

     //if(the id =1 , item can be clicked)
    if(categoryDetailList[position].id == 1) {
        holder.card_category_detail_2.isEnabled = true
        holder.txt_category_specify_detail_2_name.isEnabled = true
        if(categoryDetailList[position].highScore> 10) {

             //I added code here, but it doens't work
            categoryDetailList[position].id++
            holder.card_category_detail_2.isEnabled = true
        }
    }

    holder.txt_category_specify_detail_2_name.text = categoryDetailList[position].name
    holder.score.text = categoryDetailList[position].highScore.toString()

    dialog = Dialog(context)
    dialog!!.setContentView(R.layout.activity_lesson)

    holder.card_category_detail_2.setOnClickListener {

        var txtLesson: TextView = dialog!!.findViewById(R.id.txtLesson)
        txtLesson.text = categoryDetailList[position].lesson

        var txtDescribe: TextView = dialog!!.findViewById(R.id.txtDescribe)
        txtDescribe.text = categoryDetailList[position].nameOfTypeDetail

        var txtType: TextView = dialog!!.findViewById(R.id.txtType)
        txtType.text = categoryDetailList[position].mathCategoryDetail

        var btnPlay: Button =dialog!!.findViewById(R.id.btnPlay)

        btnPlay.setOnClickListener{
            Common2.selectedCategoryId = categoryDetailList[position]
            val intent = Intent(context, QuestionActivity::class.java)
            context.startActivity(intent)

        }

        var window : Window? = dialog!!.window
        window!!.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT)
        dialog!!.show()

    }

}

inner class MyViewHolder1(itemView: View) : RecyclerView.ViewHolder(itemView),
    View.OnClickListener {

    internal val txt_category_specify_detail_2_name: TextView
    internal val score: TextView
    internal val card_category_detail_2: CardView
    internal lateinit var iOnRecyclerViewItemClickListener2: IOnRecyclerViewItemClickListener2

    fun setiOnRecyclerViewItemClickListener2(iOnRecyclerViewItemClickListener2: IOnRecyclerViewItemClickListener2) {
        this.iOnRecyclerViewItemClickListener2 = iOnRecyclerViewItemClickListener2

    }

    init {
        txt_category_specify_detail_2_name =
            itemView.findViewById(R.id.txt_category_specify_detail_2_name) as TextView
        score = itemView.findViewById(R.id.score) as TextView
        card_category_detail_2 = itemView.findViewById(R.id.card_category_detail_2) as CardView

        itemView.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        iOnRecyclerViewItemClickListener2.onClick(view, adapterPosition)

    }

}

}

MathCategorySpecifyDetail

class MathCategorySpecifyDetail(
    var id: Int,
    var idMathCategoryDetail: Int,
    var name: String,
    var lesson: String,
    var nameOfTypeDetail: String,
    var mathCategoryDetail: String,
    var highScore: Int
)

Common1

 object Common1 {
        var selectedCategoryIdDetail: MathCategoryDetail? = null
    }

I have tried in many ways but it doesn't work. Please help me

Try creating a method inside the adapter that will enable/disable the items. For example:

fun toggleEnabled(enabled: Boolean){
   //enable or disable the items according to the score
}

Use RxJava or LiveData for observing the score. It will check and trigger the toggleEnabled() every time when the score is changed.

ps You can get items by position (fe if the score is between 10 and 20 you will get the item with index of 1 from your adapter and call toggleEnabled() for that item.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM