'How to get position in Horizontal RecyclerView?

I'm a Kotlin's student. I'm doing a tutorial layout using RecyclerView horizontal, but I'm having an issue, already tried to find some answer on stack but didn't helped. So my RecyclerView has 3 items basically, what I want to do is:

  • When I pass on first and second item horizontally my activity will display 2 TextViews visible, 1 Button hided,
  • But on 3rd item from RecyclerView I need hide 2 TextViews on Activity and Show a Button. I know that I need get position from RecyclerView but I don't know how.

I tried to use inside onBindViewHolder, if(adapterpositon == 0 || adapterposition == 1)else but doesnt work really well, appreciate help!!

My xml

        <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/listtutorial"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:layout_marginTop="50dp" />

    <TextView
        android:id="@+id/skip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/listtutorial"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="40dp"
        android:fontFamily="@font/robotmedium"
        android:text="Skip" />

    <TextView
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/listtutorial"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="40dp"
        android:layout_marginRight="24dp"
        android:fontFamily="@font/robotmedium"
        android:text="Next" />

        <Button
            android:id="@+id/btngetstarted"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/listtutorial"
            android:layout_marginTop="40dp"
            android:layout_marginHorizontal="32dp"
            android:text="Get Started"
            android:textAllCaps="false"
            android:visibility="gone"
            android:backgroundTint="@color/orange"
            android:fontFamily="@font/robotmedium"
            android:textColor="@color/white"/>

Activity

var s : ArrayList<TutorialResponse> = arrayListOf()
        s.add(TutorialResponse(R.drawable.add_to_cart_vkjp,"Choose Your Desire Product","Contrary to popular belief, Lorem Ipsum is not simply random text"))
        s.add(TutorialResponse(R.drawable.successful_purchase,"Complete your shopping","Contrary to popular belief, Lorem Ipsum is not simply random text"))
        s.add(TutorialResponse(R.drawable.on_the_way_ldaq,"Get product at your door","Contrary to popular belief, Lorem Ipsum is not simply random text"))

        listtutorial.adapter = TutorialAdapter(this,s)
        listtutorial.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)

        val snapHelper = PagerSnapHelper()
        snapHelper.attachToRecyclerView(listtutorial)
    
        listtutorial.addOnScrollListener(object : RecyclerView.OnScrollListener() 
        {
            override fun onScrollStateChanged(recyclerView: RecyclerView, 
            newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if(newState == RecyclerView.SCROLL_STATE_IDLE){
                    position = getCurrentItem()
                    if(position == 2){
                        setUI()
                    }else{
                        setUI2()
                    }
                }
            }
        })
    }

    fun getCurrentItem(): Int {
        return (listtutorial.getLayoutManager() as 
                LinearLayoutManager).findFirstVisibleItemPosition()
    }

    fun setUI(){
        skip.visibility = View.GONE
        next.visibility = View.GONE
        btngetstarted.visibility = View.VISIBLE
    }

    fun setUI2(){
        skip.visibility = View.VISIBLE
        next.visibility = View.VISIBLE
        btngetstarted.visibility = View.GONE
    }

Adapter

class TutorialAdapter (var context: Context, var list: List<TutorialResponse>) : RecyclerView.Adapter<TutorialAdapter.ViewTutorial>(){
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewTutorial {
        val view = LayoutInflater.from(context).inflate(R.layout.item_tutorial, parent, false)

        return ViewTutorial(view)
    }

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

    override fun onBindViewHolder(holder: ViewTutorial, position: Int) {
        holder.bind(context, list[position])
    }

    class ViewTutorial (itemView : View) : RecyclerView.ViewHolder(itemView){
        fun bind (context: Context, item : TutorialResponse){
                var title = itemView.findViewById<TextView>(R.id.title)
                var subtitle = itemView.findViewById<TextView>(R.id.subtitle)
                var image = itemView.findViewById<ImageView>(R.id.image)

                title.text = item.title
                subtitle.text = item.subtitle
                Glide.with(context).load(item.image).into(image)
            
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source