FragmentPageAdaptor - 在选项卡内容中滑动视图

时间:2015-10-06 09:16:55

标签: android swipe android-tabs fragmentpageradapter

我创建了一个屏幕,其上有一些只读数据字段,底部有一组horizontal tabs

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

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

我一直在寻找FragmentPageAdapter,但我不想要title bar中的任何内容,我也想对每个fragment使用相同的swipe作为唯一改变的内容是显示的数据,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”。

这样可以显示我的临时地址片段:

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

我几乎需要用页面适配器功能替换它或以某种方式让它们一起工作但我不知道如何做到这一点。

2 个答案:

答案 0 :(得分:0)

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

为第一个标签内容显示水平标签的片段,以前的地址(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>

表示上述片段的类,还包含内部类(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;
}

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

}

另一个代表我想要刷卡的实际内容\详细信息的片段(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" />

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

我还在我的实际活动类(MainActivity.java)中实现了以下inerface:

TemporaryAddressesFragment.OnFragmentInteractionListener, TemporaryAddressDetailFragment.OnFragmentInteractionListener

所以现在当我的片段上显示第一个标签时,我有3个可以刷卡的地址窗格,但这并不会以任何方式滑动标签。

答案 1 :(得分:-1)

相关问题