简体   繁体   English

如何在Android中实现此选项卡式布局?

[英]How can I achieve this tabbed layout in Android?

I'm currently working on a "Sign Up" page which consists of 4 different views. 我目前正在“注册”页面上,该页面包含4个不同的视图。 Let's call them View1 , View2 , View3 and View4 . 我们称它们为View1View2View3View4

When the user presses Sign Up, it should take him/her to View1, and then the user can only go to View2 after a button "Next" is pressed. 当用户按下“注册”时,应该将他/她带到View1,然后用户只有在按下“下一步”按钮后才能进入View2。 If the user clicks View2, View3 or View4 nothing should happen - but they should be displayed so that the user knows how much progress he has made in the registration process. 如果用户单击View2,View3或View4,则不会发生任何事情-但应显示它们,以便用户知道他在注册过程中取得了多少进展。

Currently I have finished my View1 layout and it loads with success. 目前,我已完成View1布局,并成功加载。 How can I achieve the tabbed layout that I'm looking for? 如何获得所需的选项卡式布局? Should I have a separate class to the views to handle the tabbing or do I need to add the tabs to each individual view? 我是否应该对视图使用单独的类来处理选项卡,还是需要将选项卡添加到每个单独的视图? I'm a big Android and Stackoverflow rookie so I apologise in advance if this is a strange request and am happy to elaborate on anything that is unclear. 我是Android和Stackoverflow方面的新秀,因此,如果这是一个奇怪的要求,我会提前道歉,并很高兴为您解释不清楚的任何内容。

Any help is highly appreciated! 任何帮助深表感谢!

u can use : 您可以使用:

ViewPager
TabLayout
BaseFragmentStatePagerAdapter (pageAdapter)

ViewPager.setAdapter(pageAdapter);
TabLayout.setupWithViewPager(ViewPager)

example implementation of adapter: 适配器的示例实现:

/** fragment state page adapter
* is different from pageAdapter
* in way that is not loading all fragments on same time
* The first PageAdapter might destroy View hierarchy and re load it when  needed,
* the StatePageAdapter only saves the state of the Fragment and completely destroys it,
* if the user then comes back to that page, the state is retrieved.*/
public class SectionPagerAdapter extends BaseFragmentStatePagerAdapter {

     private List<CustomFragment> _fragments;

     public SectionPagerAdapter(FragmentManager fm, List<CustomFragment> fragments) {
        super(fm);
        _fragments = fragments;
    }

    /**
     *
     * @param object
     * @return POSITION_NONE;
     *
     * This way, when you call notifyDataSetChanged(),
     * the view pager will remove all views and reload them all.
     * s so the reload effect is obtained.
     */
     @Override
     public int getItemPosition(Object object) {
        /** check if object is custom fragment class */
        if (object.getClass().isAssignableFrom(CustomFragment.class)) {
           /** cast object to fragment */
           CustomFragment customFragment = (CustomFragment)object;
           return _fragments.indexOf(customFragment);
        } else {
            /** else recreate */
            return POSITION_NONE;
        }
     }

     @Override
     public CustomFragment getItem(int position) {
        return _fragments.get(position);
     }

     @Override
     public CharSequence getPageTitle(int position) {
         return getItem(position).getTittle();
     }

     @Override
     public int getCount() {
         return _fragments.size();
     }

     @Override
     public Parcelable saveState() {
         return super.saveState();
     }

     /** to prevent crash on restore state 
      * - due to GOOGLE BUG (i use my own imlementation of adapter 
      * don't call super ! */
     @Override
     public void restoreState(Parcelable state, ClassLoader loader) {
         super.restoreState(state, loader);
      }
  }

*CustomFragment is abstract class extendent from Fragment class with extra methods update() & getTitle() * CustomFragment是Fragment类的抽象类扩展,带有额外的方法update()和getTitle()

 /** to handle extended property in fragment*/
 public abstract class CustomFragment extends Fragment implements IGetDetails{

    public CustomFragment() {
    }

     /** method to perform fragment update */
     public abstract void update();
}

public interface IGetDetails {
    String getTittle();
}

then in main activity u create a container: 然后在主要活动中创建一个容器:

    /** create fragment container */
    if (_fragments == null) {
        _fragments = new ArrayList<>();
        _fragments.add(new Fragment1());
        _fragments.add(new Fragment2());
        _fragments.add(new Fragment3());
    }

to populate fragments u can use this metyhod: 要填充片段,您可以使用此方法:

/**
 * populate view pager
 */
private ViewPager populateViewPager(ViewPager pager, List<CustomFragment> list) {
    /** get child fragment manager of this fragment  */
    FragmentManager childFragmentManager = getChildFragmentManager();
    /** we need pass to adapter child fragment manager because of nested fragments  */
    SectionPagerAdapter sectionPagerAdapter = new SectionPagerAdapter(childFragmentManager, list);
    /** set adapter to view pager */
    pager.setAdapter(sectionPagerAdapter);
    /** testify data set changed */
    sectionPagerAdapter.notifyDataSetChanged();
    return pager;
}

** if u use Activity as host for view pager u should pass FragmentManager but if u host on fragment ( u got nested frgments )u sholud pass to adpter child fragment manager **如果您将Activity用作视图传呼器的主机,则应通过FragmentManager,但如果主机位于片段(您有嵌套的碎片),则应将其传递给adpter 子片段管理器

and example layout: 和示例布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="scrollable" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

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

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