Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – How to enable next item in RecyclerView?

Posted on November 11, 2022 By

Solution 1 :

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.

p.s. You can get items by position (f.e. 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.)

Problem :

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.

enter image description here
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

READ  [FIXED] android - Calling a function and passing arguments from Kotlin to Flutter
Powered by Inline Related Posts

Comments

Comment posted by Cyd

I cannot recreate this but I get what you mean. Is it possible that you can get the total value index? its like for(x in 0 until totalIndex){ if (x > 10){ x+1 = enabled }else{ x+1 = disabled } } This is not the exact code sorry.

Comment posted by Rajen Raiyarela

You can even put a check in your onClick method, get the currently clicked item, and see that items highScope is greater than 10 or not, if not then don’t call

Android Tags:android, android-recyclerview, java, kotlin

Post navigation

Previous Post: [FIXED] android – How to achieve ionic permissions on first load
Next Post: [FIXED] android – How do i put online database into local database

Related Posts

[FIXED] i want to send image and some data using volley in android Android
[FIXED] android – Generate PDF file and attach to e-mail Android
[FIXED] android – React-Native production app crashes all of sudden Android
[FIXED] android – Listview ArrayAdapter custom item onClickListener working only first time Android
[FIXED] android – how to bring change in TextView ,when simultaneously making change/write in EditView? Android
[FIXED] java – MockK Kotlin: Verification failed: call 1 of 1: was not called Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme