FragmentPagerAdapter - 选项卡内容内的清扫视图

FragmentPageAdaptor - swiping views inside tab content

我创建了一个屏幕,上面有一些只读数据字段,底部有 horizontaltabs

每个选项卡都有一个 fragment 代表 tab content 并由一组数据对象备份,例如其中一个选项卡是以前的地址,其中可以有 0 到很多。

我希望 tab 内容 fragmentswipeable 这样一来它会显示最近的先前地址和 swipeleft 最初将从 an array 拉入下一个,如果合适,继续滑动将在任一方向浏览地址。

我一直在寻找 FragmentPageAdapter,但我不想要 title bar 中的任何东西,我还想对每个 swipe 使用相同的 fragment 作为唯一的东西变化的是显示的数据,layout 每次都是一样的。

不过,我仍然想要 swipe 操作,使页面看起来像是从一页滑动到另一页。

这是最好的方法吗?还有更好的选择吗?目前有 5 个 tabs,其中 3 个 tabs 将包含 swipe-able 内容,这些内容将显示更多数据,但视图本身不会改变。

我想我会编辑它并添加一些代码,这样可能更容易看到我想要实现的目标:

我有一个片段,它代表一个被点击的垂直制表符,在该片段中是一组水平制表符:

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

因此,当用户单击第一个选项卡或页面刚刚呈现时,它将在 android:id="@android:id/tabcontent".[=33 中显示另一个片段=]

这有效,它显示了我的临时地址片段:

<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.mockattempt1.TemporaryAddressesFragment">

<TextView android:layout_width="match_parent" android:layout_height="match_parent"
    android:text="Temp Addresses Fragment that will show address information soon" />

目前临时地址片段只是一个硬编码的文本视图,但我希望它是一组可滑动的临时地址片段,用户可以滑动查看地址。

我正在尝试将本教程 http://www.101apps.co.za/articles/swipe-view-tutorial.html 融入我的应用程序,但我不确定 ViewPager xml 应该去哪里,它几乎看起来应该是我的 tabcontent xml在父片段中?

我不认为这是正确的,因为在父片段中,选项卡内容是这样设置的:

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);

我几乎需要用页面适配器功能替换它,或者以某种方式让它们一起工作,但我不确定该怎么做。

看看这个link

http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html

https://github.com/neokree/MaterialTabs

此选项卡是使用 material 选项卡制作的,易于实现。

我已经设法做到了,但不完全确定这是最好的方法,但我会详细说明我的尝试:

第一个标签内容显示水平标签的片段,以前的地址(fragment_record.xml):

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

为下面详述的临时地址片段设置选项卡和意图 (RecordFragment.java):

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);

ViewPager 的占位符片段 (fragment_temporary_addresses.xml):

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

Class 表示上面的片段,还包含内部 class (TemporaryAddressesFragment.java):

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

此片段 class 中包含的内部 class 是适配器,目前仅提供 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;
    }
}

}

代表我想要滑动的实际 content\detail 的另一个片段 (fragment_temporary_address_detail.xml):

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

还有一个 class 表示上面的片段 (TemporaryAddressDetailFragment.java) 目前没有做任何事情,因为片段 xml 显示硬编码字符串。

我还在我的实际 activity class (MainActivity.java) 中实现了以下接口:

TemporaryAddressesFragment.OnFragmentInteractionListener, TemporaryAddressDetailFragment.OnFragmentInteractionListener

现在,当第一个选项卡显示在我的片段上时,我有 3 个可滑动的地址窗格,但这无论如何都不会滑动选项卡。