I don't know how to use currnet item in viewpager

60 Views Asked by At

I want to use currentItem on Fragments in viewpager2 ViewPager is also fragment. and my all fragments have viewbinding. My fragment call viewpager by findViewById. But It isn't worked. It's data is always 'null'. Can I use findviewbyid and viewbinding both of them? I already tried to use findViewbyID in onviewcreated or oncreateview Please help me pagertest.kt(first page fragment)

class pagertest : Fragment() {


    private var _binding: FragmentPagertestBinding? = null
    private val binding get() = _binding!!


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        _binding = FragmentPagertestBinding.inflate(inflater, container, false)
        binding.button5.setOnClickListener {
            val pager = activity?.findViewById<ViewPager2>(R.id.dayspager22)
            pager?.currentItem = 2
        }
        return binding.root

    }
}

dayspager22.kt(ViewPagerFragment)

class dayspager22 : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null
    private var _binding: FragmentDayspager22Binding? = null
    private val binding get() = _binding!!
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentDayspager22Binding.inflate(inflater, container, false)
        val fragmentList = arrayListOf<Fragment>(
            pagertest(),
            days_1(),
            days_2(),
            days_3()
        )

        val adapter = ViewPagerAdapter(
            fragmentList,
            requireActivity().supportFragmentManager,
            lifecycle
        )
        binding.views.adapter = adapter

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)



    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment dayspager22.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            dayspager22().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

ViewPage Adapter

class ViewPagerAdapter(
    list: ArrayList<Fragment>,
    fm: FragmentManager,
    lifecycle: Lifecycle
) : FragmentStateAdapter(fm, lifecycle) {

    private val fragmentList = list

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

    override fun createFragment(position: Int): Fragment {
        return fragmentList[position]
    }
}
1

There are 1 best solutions below

1
On

I think it might be because you're creating and adding pagertest to the adapter inside dayspager22's onCreateView method. You haven't returned its layout view yet, so it's not added to the Activity's layout - and if it's not added, you won't be able to find it with findViewById. (I'm not sure exactly when pagertest's onCreateView runs here, but I'd look into that possibility.)

Also make sure fragment_dayspager_22.xml or whatever actually has a ViewPager2 with an id of dayspager22 - you're setting your adapter on something called views:

val adapter = ViewPagerAdapter(
    fragmentList,
    requireActivity().supportFragmentManager,
    lifecycle
)

// you're setting a ViewPagerAdapter on something that's probably a ViewPager,
// and its id is R.id.views
binding.views.adapter = adapter

Are you sure that's not the ID you want to look up with findViewById?