Search code examples
android-studiokotlinandroid-viewpager

How to change the value of the adapter position in the activity in an OnClick?


In my adapter of a ViewPager I have the position. What I want to do is add 1 to the position by pressing a Button in the activity but I cannot access the position from the activity. How can I do it?

Adapter:

class MyAdapter(var context: Context,private var myList: List<MyModel>) : PagerAdapter() {
        
    override fun getCount(): Int {
        return myList.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View?)
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {

        var view: View = LayoutInflater.from(context).inflate(R.layout.layout_item,container,false)
        var img: ImageView = view.findViewById(R.id.imgView)
        val listPos= myList[position]

        container.addView(view)
        return view

    }      
}

Activity:

class MyActivity : AppCompatActivity() {

    private var myAdapter: MyAdapter? = null
    private val myList = ArrayList<MyModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.my_activity_layout)
        myAdapter = MyAdapter(this,myList)
        myViewPager.adapter = myAdapter
        myList.add("Name",R.drawable.img1)
        myList.add("Name2",R.drawable.img2)
        myList.add("Name3",R.drawable.img3)

        myButtonPlus.setOnClickListener{
            //Here I want to +1 the position for every click
        }
        myButtonMinus.setOnClickListener{
            //Here I want to -1 the position for every click
        }
    }
}

Solution

  • If you want to increase and decrease your ViewPager position you should use the setCurrentItem function.

    Try to change your code like this:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.my_activity_layout)
        myAdapter = MyAdapter(this,myList)
        myViewPager.adapter = myAdapter
        myList.add("Name",R.drawable.img1)
        myList.add("Name2",R.drawable.img2)
        myList.add("Name3",R.drawable.img3)
    
        myButtonPlus.setOnClickListener{
            //Here I want to +1 the position for every click
            val currentPage = myAdapter?.currentItem ?: 0
            val maxPage = myAdapter?.childCount ?: 0
    
            if (currentPage < maxPage) {
                myAdapter?.currentItem = currentPage + 1
            }
        }
        myButtonMinus.setOnClickListener{
            //Here I want to -1 the position for every click
            val currentPage = myAdapter?.currentItem ?: 0
    
            if (currentPage > 0) {
                myAdapter?.currentItem = currentPage - 1
            }
        }
    }