我目前正致力于"注册"页面,包含4个不同的视图。我们称他们为View1
,View2
,View3
和View4
。
当用户按下注册时,应该将他/她带到View1,然后用户只能在按钮"下一步"之后进入View2。被压了。如果用户单击View2,View3或View4,则不会发生任何事情 - 但应显示它们以便用户知道他在注册过程中取得了多少进展。
目前我已经完成了我的View1布局,并且加载成功。如何实现我正在寻找的标签式布局?我应该在视图中使用单独的类来处理标签,还是需要将标签添加到每个单独的视图中?我是一个很棒的Android和Stackoverflow新手,所以如果这是一个奇怪的请求,我会提前道歉,我很乐意详细说明任何不清楚的事情。
非常感谢任何帮助!
答案 0 :(得分:0)
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);
}
}
* CustomFragment 是Fragment类中的抽象类extendent,带有额外的方法update()&amp;的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();
}
然后在主要活动中你创建一个容器:
/** create fragment container */
if (_fragments == null) {
_fragments = new ArrayList<>();
_fragments.add(new Fragment1());
_fragments.add(new Fragment2());
_fragments.add(new Fragment3());
}
填充片段你可以使用这个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;
}
**如果您使用Activity作为查看寻呼机的主机,则应传递 FragmentManager 但如果你托管片段(你有嵌套的frgments)你sholud传递给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" />