2017-04-13 70 views
-3

我使用FragmentTabHost在片段如何在碎片中添加可滑动的标签?

下面添加选项卡是我的代码:

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个标签成功,但我无法刷他们或可以说他们不能滑动。

如何使标签可以滑动?

请帮忙!!

+0

您需要添加viewpager的刷卡功能 – AbhayBohra

+0

退房此链接http://www.androidhive.info/2015/09/android-material-design-working-with- tabs/ – AbhayBohra

+0

但viewpager需要getSupportFragmentManager();你不能在片段中使用。 – Android

回答

2

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

要使用ViewPager设置布局,请在您的XML布局中添加一个<ViewPager>元素。例如,如果在刷卡视图中的每个页面要消耗整个布局,那么你的布局是这样的:

<?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了解更多详情。

0

试试这个,

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