简体   繁体   中英

viewpager,recylerview not working if using same fragment for all tabs

i am using the same fragment for all the tabs.

But the problem is scroll of the recycler view of fragment working for last tab only.

Recycler view not scrolling for the previous tabs. Below is my code of the FragmentPagerAdapter:

class DataViewPagerAdapter(fm: FragmentManager?) : FragmentStatePagerAdapter(fm) {

var fragmentList: MutableList<Fragment> = ArrayList()
var fragmentTitleList: MutableList<String> = ArrayList()

override fun getItem(position: Int): Fragment {
    return DataFragment.newInstance(position)
}

override fun getCount(): Int {
    return 3
}

override fun getPageTitle(position: Int): CharSequence? {
    return "Deepak"
}

}

Here is my code for binding adapter:

 var adapter = DataViewPagerAdapter( getChildFragmentManager())

    dataViewpager.adapter = adapter
    dataViewpager.offscreenPageLimit=1
    tabLayout.setupWithViewPager(dataViewpager)

This is code of my fragment:

class DataFragment : Fragment() {
lateinit var recyclerView: RecyclerView
companion object {
    fun newInstance(position: Int): DataFragment {
        val fragment = DataFragment()
        val args = Bundle()
        args.putInt("KEY_POSITION", position)
        args.putInt("KEY_ID",1)
        fragment.setArguments(args)
        return fragment
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    var view = LayoutInflater.from(activity).inflate(R.layout.fragment_data, null)
    recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
    var adapter = DataFragmentAdapter()
    recyclerView.layoutManager = GridLayoutManager(activity, 3)
    recyclerView.adapter = adapter
    return view;
}

}

我不确定是否与您的问题有关,但offscreenPageLimit应该是您的适配器数量的大小。

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