简体   繁体   English

如何在 Android Studio 中从 fragment 移动到 viewpager?

[英]How to move from fragment to viewpager in Android Studio?

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.澄清一下:我有片段 A、B 和 C。一旦在片段 A 中按下按钮,我想要一个视图页面,用户只能在片段 B 和 C 之间自由滑动。

How and where would I initialise the viewpager and its adapter?我将如何以及在哪里初始化 viewpager 及其适配器? Or is there some other method I could use?或者我可以使用其他方法吗?

Previously, in another project, I successfully initialised a viewpager in the MainActivity using the following:以前,在另一个项目中,我使用以下方法成功地在 MainActivity 中初始化了一个 viewpager:

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.视图寻呼机需要包含在 Fragment 或 Activity 中。

If you want to navigate from a fragment to the viewpager, then you need 3 fragments.如果你想从一个片段导航到 viewpager,那么你需要 3 个片段。

  1. Fragment A (The one with the button)片段A(有按钮的那个)

  2. Fragment B (The fragment container which initializes the view pager with the adapter)片段 B(使用适配器初始化视图分页器的片段容器)

  3. Fragment C (The fragment view pager. This fragment is in charge of showing data inside the viewpager) Fragment C(fragment view pager。这个fragment负责显示viewpager里面的数据)

I created this secret gist for you.我为你创造了这个秘密要点。

https://gist.github.com/agusmagne/4ccd7b55c4d71a91b712d3e51bdaf591 https://gist.github.com/agusmagne/4ccd7b55c4d71a91b712d3e51bdaf591

I'm just going to add it here in case the url goes down.我只是要在这里添加它以防 url 出现故障。

// 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.澄清一下:我有片段 A、B 和 C。一旦在片段 A 中按下按钮,我想要一个视图页面,用户只能在片段 B 和 C 之间自由滑动。

You need Fragment A to navigate to Fragment D.您需要片段 A 才能导航到片段 D。

Fragment D would contain a ViewPager with a FragmentPagerAdapter that would be able to show Fragment B and Fragment C. Fragment D 将包含一个带有 FragmentPagerAdapter 的 ViewPager,可以显示 Fragment B 和 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")
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM