如何在androidstudio中从fragment移动到viewpager?

xj3cbfub  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(324)

澄清一下:我有a,b和c片段。一旦在片段a中按下一个按钮,我就想要一个viewpager,用户可以在片段b和c之间自由滑动。
如何以及在何处初始化viewpager及其适配器?或者我还有别的方法可以用吗?
以前,在另一个项目中,我使用以下方法成功初始化了mainactivity中的viewpager:

viewPager = findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(5);
FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().commit();

…但我不知道在哪里添加和更改,以适应我当前的项目。请帮忙!

u0njafvf

u0njafvf1#

视图寻呼机需要包含在片段或活动中。
如果要从片段导航到viewpager,则需要3个片段。
碎片a(带按钮的那个)
片段b(用适配器初始化视图分页器的片段容器)
片段c(片段视图页面)。此片段负责显示viewpager中的数据)
我为你创造了这个秘密要点。
https://gist.github.com/agusmagne/4ccd7b55c4d71a91b712d3e51bdaf591
我只是想把它加在这里,以防网址掉了。

// 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
        }

    }
}
hjqgdpho

hjqgdpho2#

澄清一下:我有a,b和c片段。一旦在片段a中按下一个按钮,我就想要一个viewpager,用户可以在片段b和c之间自由滑动。
你需要片段a来导航到片段d。
片段d将包含一个带有片段PageRadapter的viewpager,该片段PageRadapter能够显示片段b和片段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")
  }
}

相关问题