Android FragmentPageAdapter-在选项卡内容内滑动视图
我创建了一个屏幕,屏幕上有一些只读数据字段,底部有一组Android FragmentPageAdapter-在选项卡内容内滑动视图,android,swipe,android-tabs,fragmentpageradapter,Android,Swipe,Android Tabs,Fragmentpageradapter,我创建了一个屏幕,屏幕上有一些只读数据字段,底部有一组选项卡 每个选项卡都有一个片段,表示选项卡内容,并由一组数据对象备份,例如其中一个选项卡是以前的地址,其中可以有0到多个 我希望选项卡内容片段是可切换的,这样它将首先显示上一个最近的地址,并且向左滑动将从数组中拉入下一个,如果合适,继续刷卡将在任一方向的地址中导航 我一直在寻找FragmentPageAdapter,但我不希望标题栏中有任何内容,我还想对每次滑动使用相同的片段,因为唯一改变的是显示的数据,布局每次都是相同的 我仍然希望使用滑动
选项卡
每个选项卡都有一个片段
,表示选项卡内容
,并由一组数据对象备份,例如其中一个选项卡是以前的地址,其中可以有0到多个
我希望选项卡
内容片段
是可切换的
,这样它将首先显示上一个最近的地址,并且向左
滑动
将从数组
中拉入下一个,如果合适,继续刷卡将在任一方向的地址中导航
我一直在寻找FragmentPageAdapter
,但我不希望标题栏中有任何内容
,我还想对每次滑动使用相同的片段
,因为唯一改变的是显示的数据,布局每次都是相同的
我仍然希望使用滑动
操作,使其看起来像是从一个页面滑动到另一个页面
这是最好的解决方法吗?还有更好的选择吗?目前有5个选项卡
,其中3个选项卡
将具有可滑动的内容,这些内容将显示更多数据,但视图本身不会改变
我想我会编辑这个并添加一些代码,以便更容易看到我想要实现的目标:
我有一个片段,它代表一个被点击的垂直选项卡,在该片段内是一组水平选项卡:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="5dp">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
android:id="@+id/name_table">
</TableLayout>
</LinearLayout>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:layout_marginTop="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="20">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="horizontal">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/tab_btn_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:background="@color/lightest_grey"
android:id="@+id/temp_addr_tab_btn"
android:text="Temporary Addresses"
android:layout_marginRight="1dp"
android:textSize="7sp"
android:gravity="bottom|center"
android:drawableTop="@mipmap/ic_launcher"
android:paddingTop="4dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:background="@color/lightest_grey"
android:id="@+id/postal_addr_tab_btn"
android:text="Postal Addresses"
android:layout_marginRight="1dp"
android:textSize="7sp"
android:gravity="bottom|center"
android:drawableTop="@mipmap/ic_launcher"
android:paddingTop="4dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:background="@color/lightest_grey"
android:id="@+id/home_addr_tab_btn"
android:text="Home Addresses"
android:layout_marginRight="1dp"
android:textSize="7sp"
android:gravity="bottom|center"
android:drawableTop="@mipmap/ic_launcher"
android:paddingTop="4dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:background="@color/lightest_grey"
android:id="@+id/tel_fax_tab_btn"
android:text="Telephone / Fax"
android:layout_marginRight="1dp"
android:textSize="7sp"
android:gravity="bottom|center"
android:drawableTop="@mipmap/ic_launcher"
android:paddingTop="4dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:background="@color/lightest_grey"
android:id="@+id/email_other_tab_btn"
android:text="Email / Other"
android:textSize="7sp"
android:gravity="bottom|center"
android:drawableTop="@mipmap/ic_launcher"
android:paddingTop="4dp"
/>
</LinearLayout>
</FrameLayout>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="17"
android:background="@color/white"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
我几乎需要用页面适配器功能来替换它,或者让它们一起工作,但我不知道怎么做。看看这个链接
此选项卡使用material tab创建,易于实现。我已经设法做到了这一点,但并不完全确定这是最好的方法,但我将详细说明我的尝试:
第一个选项卡内容显示水平选项卡的片段,以前的地址(Fragment_record.xml):
ViewPager的占位符片段(fragment\u temporary\u addresses.xml):
这个片段类中包含的内部类是适配器,目前只提供3个硬编码的临时地址细节片段:
public class TemporaryAddressDetailFragmentStatePageAdapter extends FragmentStatePagerAdapter {
public TemporaryAddressDetailFragmentStatePageAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
fragment = new TemporaryAddressDetailFragment();
return fragment;
}
}
}
另一个片段表示我希望具有swipeable的实际内容\详细信息(片段\临时\地址\详细信息.xml):
因此,当第一个标签显示在我的片段上时,我有3个地址窗格,它们可以滑动,但无论如何都不会滑动标签。我查看了它,但这完全不是我想要的?我希望用户单击选项卡,但每个选项卡的内容视图都可以滑动?我不希望滑动在选项卡之间移动。
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("temp_addresses").setIndicator("Temporary Addresses"), TemporaryAddressesFragment.class, null);
mTabHost.setCurrentTab(0);
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:layout_marginTop="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="20">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="horizontal">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/tab_btn_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:background="@color/lightest_grey"
android:id="@+id/temp_addr_tab_btn"
android:text="Temporary Addresses"
android:layout_marginRight="1dp"
android:textSize="7sp"
android:gravity="bottom|center"
android:drawableTop="@mipmap/ic_launcher"
android:paddingTop="4dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:background="@color/lightest_grey"
android:id="@+id/postal_addr_tab_btn"
android:text="Postal Addresses"
android:layout_marginRight="1dp"
android:textSize="7sp"
android:gravity="bottom|center"
android:drawableTop="@mipmap/ic_launcher"
android:paddingTop="4dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:background="@color/lightest_grey"
android:id="@+id/home_addr_tab_btn"
android:text="Home Addresses"
android:layout_marginRight="1dp"
android:textSize="7sp"
android:gravity="bottom|center"
android:drawableTop="@mipmap/ic_launcher"
android:paddingTop="4dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:background="@color/lightest_grey"
android:id="@+id/tel_fax_tab_btn"
android:text="Telephone / Fax"
android:layout_marginRight="1dp"
android:textSize="7sp"
android:gravity="bottom|center"
android:drawableTop="@mipmap/ic_launcher"
android:paddingTop="4dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:background="@color/lightest_grey"
android:id="@+id/email_other_tab_btn"
android:text="Email / Other"
android:textSize="7sp"
android:gravity="bottom|center"
android:drawableTop="@mipmap/ic_launcher"
android:paddingTop="4dp"
/>
</LinearLayout>
</FrameLayout>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="17"
android:background="@color/white"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("temp_addresses").setIndicator("Temporary Addresses"), TemporaryAddressesFragment.class, null);
mTabHost.setCurrentTab(0);
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/temp_addr_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
private ViewPager temporaryAddressViewPager;
private TemporaryAddressDetailFragmentStatePageAdapter temporaryAddressStateAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = (View)inflater.inflate(R.layout.fragment_temporary_addresses, container, false);
temporaryAddressStateAdapter = new TemporaryAddressDetailFragmentStatePageAdapter(getFragmentManager());
temporaryAddressViewPager = (ViewPager)rootView.findViewById(R.id.temp_addr_pager);
temporaryAddressViewPager.setAdapter(temporaryAddressStateAdapter);
return rootView;
}
public class TemporaryAddressDetailFragmentStatePageAdapter extends FragmentStatePagerAdapter {
public TemporaryAddressDetailFragmentStatePageAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
fragment = new TemporaryAddressDetailFragment();
return fragment;
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.oxygenmockattempt1.TemporaryAddressDetailFragment">
<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="This is the temporary address detail fragment screen" />
TemporaryAddressesFragment.OnFragmentInteractionListener, TemporaryAddressDetailFragment.OnFragmentInteractionListener