0

我有TabLayout和ViewPager片段在裏面:手柄TabLayout選項卡中單擊

<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" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
tools:context="layout.LinksFragment"> 

<android.support.design.widget.TabLayout 
    android:id="@+id/tab_layout_info" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:elevation="6dp" 
    android:minHeight="?attr/actionBarSize" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager_info" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

</FrameLayout> 

在我的片段文件,裏面onCreateView()方法,我設置了兩個ViewPager和TabLayout:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_links, container, false); 

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout_info); 
    tabLayout.addTab(tabLayout.newTab()); 
    tabLayout.addTab(tabLayout.newTab()); 
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 


    ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager_info); 
    PagerAdapterLinks adapter = new PagerAdapterLinks(getActivity().getSupportFragmentManager(), tabLayout.getTabCount()); 
    viewPager.setAdapter(adapter); 
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 

    tabLayout.setupWithViewPager(viewPager); 


    return view; 
} 

在android 5及更高版本上,一切正常,我可以使用滑動或通過單擊選項卡名稱在選項卡之間切換。然而,在較低的android版本上,單擊選項卡不會做任何事情,並且在ViewPager中更改頁面的唯一方法是通過滑動。我如何讓我的TabLayout在包括4.1和4.4在內的Android上工作?

感謝您的幫助!

回答

0

只需添加android.support.design.widget.TabLayout,它將用於渲染不同的選項卡選項。 android.support.v4.view.ViewPager組件將用於在我們將創建的各個片段之間進行頁面切換。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/sliding_tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMode="scrollable" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="0px" 
     android:layout_weight="1" 
     android:background="@android:color/white" /> 

既然我們在佈局中有ViewPager和我們的標籤,我們應該開始定義每個標籤的內容。由於每個選項卡只是一個正在顯示的片段,因此我們需要創建並定義要顯示的片段。根據您的要求,您的應用程序中可能會有一個或多個片段。

在res /佈局/ fragment_page.xml定義將顯示在屏幕上的片段的XML佈局當選擇了特定標籤:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" /> 

PageFragment.java限定充氣邏輯選項卡的片段內容:

// In this case, the fragment displays simple text based on the page 
public class PageFragment extends Fragment { 
    public static final String ARG_PAGE = "ARG_PAGE"; 

    private int mPage; 

    public static PageFragment newInstance(int page) { 
     Bundle args = new Bundle(); 
     args.putInt(ARG_PAGE, page); 
     PageFragment fragment = new PageFragment(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mPage = getArguments().getInt(ARG_PAGE); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_page, container, false); 
     TextView textView = (TextView) view; 
     textView.setText("Fragment #" + mPage); 
     return view; 
    } 
} 

接下來要做的是爲您的ViewPager實現適配器,它控制標籤,標題及其關聯內容的順序。這裏實現的最重要的方法是getPageTitle(int position),它用於獲取每個選項卡的標題以及確定每個選項卡片段的getItem(int position)。

public class SampleFragmentPagerAdapter extends FragmentPagerAdapter { 
    final int PAGE_COUNT = 3; 
    private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" }; 
    private Context context; 

    public SampleFragmentPagerAdapter(FragmentManager fm, Context context) { 
     super(fm); 
     this.context = context; 
    } 

    @Override 
    public int getCount() { 
     return PAGE_COUNT; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return PageFragment.newInstance(position + 1); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     // Generate title based on item position 
     return tabTitles[position]; 
    } 
} 

最後,我們需要把我們的ViewPager連接到SampleFragmentPagerAdapter,然後用兩個步驟配置滑動標籤:

在你活動的onCreate()方法,找到ViewPager和連接適配器。 在TabLayout上設置ViewPager以將尋呼機連接到選項卡。

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Get the ViewPager and set it's PagerAdapter so that it can display items 
     ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 
     viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(), 
      MainActivity.this)); 

     // Give the TabLayout the ViewPager 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); 
     tabLayout.setupWithViewPager(viewPager); 
    } 

} 

來源:https://guides.codepath.com/android/google-play-style-tabs-using-tablayout

+0

我不是在尋找一個完整的教程中,我想知道如何使這個方法與API16兼容。你粘貼的內容與我的代碼相同,在Android 4.4下不起作用 – TheTechGuy96

相關問題