To clarify: I have Fragments A, B and C. Once a button is pressed in Fragment A, I want to have a viewpager where the user can freely swipe between Fragments B and C only.
How and where would I initialise the viewpager and its adapter? Or is there some other method I could use?
Previously, in another project, I successfully initialised a viewpager in the MainActivity using the following:
viewPager = findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(5);
FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().commit();
...but I have no clue where to add and what to change to adapt this to my current project. Help would be appreciated!
The view pager needs to be contained inside a Fragment or an Activity.
If you want to navigate from a fragment to the viewpager, then you need 3 fragments.
Fragment A (The one with the button)
Fragment B (The fragment container which initializes the view pager with the adapter)
Fragment C (The fragment view pager. This fragment is in charge of showing data inside the viewpager)
I created this secret gist for you.
https://gist.github.com/agusmagne/4ccd7b55c4d71a91b712d3e51bdaf591
I'm just going to add it here in case the url goes down.
// In this example, your FragmentA (not shown in here) has a button
// When you click on that button, you commit a transaction to an instance of FragmentB
// This FragmentB is the CONTAINER of the View Pager, so when it's done loading it will show the view pager
// FragmentC is in charge of the logic of each individual page
// You can control which page is which by looking at its position
class FragmentB : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_news, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// These two lines of code will build the view pager
viewPager.adapter = MyCustomViewPagerAdapter(childFragmentManager)
tabViewPager.setupWithViewPager(viewPager)
}
}
class MyCustomViewPagerAdapter(childFragmentManager: FragmentManager) :
FragmentPagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
// This is an example of how you can set the title of your page tab
override fun getPageTitle(position: Int): CharSequence? {
return if (position == 0) {
"Page 1"
} else {
"Page 2"
}
}
// Here you're setting the number of tabs (pages) your view pager has.
// For every page, you're creating a new FragmentC
// You pass its position as argument, so the Page knows which position corresponds to it.
override fun getItem(position: Int): Fragment = FragmentC(position)
override fun getCount(): Int = 2
}
class FragmentC(private val position: Int) : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_news_page, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Here you can do logic using the POSITION of this specific fragment
if (position == 0) {
// do something
}
if (position == 1) {
// do something else
}
}
}
To clarify: I have Fragments A, B and C. Once a button is pressed in Fragment A, I want to have a viewpager where the user can freely swipe between Fragments B and C only.
You need Fragment A to navigate to Fragment D.
Fragment D would contain a ViewPager with a FragmentPagerAdapter that would be able to show Fragment B and Fragment C.
class MyFragmentPagerAdapter(
private val context: Context,
fragmentManager: FragmentManager
) : FragmentPagerAdapter(fragmentManager) {
override fun getCount() = 2
override fun getItem(position: Int) = when(position) {
0 -> FirstFragment()
1 -> SecondFragment()
else -> throw IllegalStateException("Unexpected position $position")
}
override fun getPageTitle(position: Int): CharSequence = when(position) {
0 -> context.getString(R.string.first)
1 -> context.getString(R.string.second)
else -> throw IllegalStateException("Unexpected position $position")
}
}
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.