如何在Android中实现这种选项卡式布局?
我目前正在开发一个包含4个不同视图的注册页面。让我们称它们为View1、View2、View3和View4 当用户按下“注册”按钮时,应将他/她带到View1,然后用户只有在按下“下一步”按钮后才能转到View2。如果用户单击View2、View3或View4,则不应发生任何事情,但应显示它们,以便用户知道他在注册过程中取得了多大进展 目前,我已经完成了View1布局,并成功加载。如何实现我所寻找的选项卡式布局?我应该为视图创建一个单独的类来处理选项卡,还是需要为每个视图添加选项卡?我是一个大机器人和Stackoverflow新手,所以如果这是一个奇怪的请求,我会提前道歉,并很高兴详细说明任何不清楚的事情 非常感谢您的帮助 u可以使用:如何在Android中实现这种选项卡式布局?,android,Android,我目前正在开发一个包含4个不同视图的注册页面。让我们称它们为View1、View2、View3和View4 当用户按下“注册”按钮时,应将他/她带到View1,然后用户只有在按下“下一步”按钮后才能转到View2。如果用户单击View2、View3或View4,则不应发生任何事情,但应显示它们,以便用户知道他在注册过程中取得了多大进展 目前,我已经完成了View1布局,并成功加载。如何实现我所寻找的选项卡式布局?我应该为视图创建一个单独的类来处理选项卡,还是需要为每个视图添加选项卡?我是一个大机
ViewPager
TabLayout
BaseFragmentStatePagerAdapter (pageAdapter)
ViewPager.setAdapter(pageAdapter);
TabLayout.setupWithViewPager(ViewPager)
适配器的示例实现:
/** 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);
}
}
然后在主活动中创建一个容器:
/** create fragment container */
if (_fragments == null) {
_fragments = new ArrayList<>();
_fragments.add(new Fragment1());
_fragments.add(new Fragment2());
_fragments.add(new Fragment3());
}
要填充片段,您可以使用以下方法:
/**
* 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;
}
**如果你使用活动作为查看寻呼机的主机,你应该通过FragmentManager
但是如果你在片段上托管,你得到了嵌套的片段,你必须传递给adpter子片段管理器
布局示例:
<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" />
<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" />