【问题标题】:How to add swipe able tabs in Fragments?如何在 Fragments 中添加可滑动的标签?
【发布时间】:2017-04-13 10:28:27
【问题描述】:

我正在使用FragmentTabHost 在 Fragment 中添加标签

下面是我的代码:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExploreFragment extends Fragment {

    private FragmentTabHost mTabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View rootView = inflater.inflate(R.layout.fragment_explore, container, false);

        mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
                ContactUsFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"),
                MyProfileFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"),
                RaiseCampaignFragment.class, null);


        return rootView;
    }
}

而我的 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

我已经成功添加了 3 个标签,但是我无法滑动它们或者可以说它们不可滑动。

如何让标签页可滑动?

请帮忙!!

【问题讨论】:

  • 您需要为滑动功能添加viewpager
  • 但是viewpager需要getSupportFragmentManager();您不能在片段中使用。
  • 你在片段中使用 getChildFragmentManager()
  • @AbhayBohra 非常感谢.. 成功了..

标签: android android-fragments tabs


【解决方案1】:

您可以使用ViewPager 小部件在您的应用中创建滑动视图,该小部件可在Support Library 中找到。 ViewPager 是一个布局小部件,其中每个子视图都是布局中的一个单独页面(单独的选项卡)。

要使用ViewPager 设置您的布局,请将&lt;ViewPager&gt; 元素添加到您的XML 布局中。例如,如果滑动视图中的每个页面都应该占用整个布局,那么您的布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

要插入代表每个页面的子视图,您需要将此布局挂钩到 PagerAdapter

结帐Creating Swipe Views with Tabs 了解更多详情。

【讨论】:

    【解决方案2】:

    试试这个,

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/toolbar"
        android:layout_above="@+id/tab_layout"/>
    
    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      相关资源
      最近更新 更多