簡體   English   中英

如何使用Swipe視圖實現android TabLayout設計支持libarary

[英]How to implement android TabLayout design support libarary with Swipe views

我將使用android TabLayout設計支持庫,但我不知道如何使用滑動視圖。

這是我的代碼

XML:

<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

Java的:

TabLayout tabLayout;

tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));

派對有點晚了,但要做到這一點,你必須使用ViewPager類並為每個視圖使用片段(在選項卡下)。 然后將ViewPager附加到您的TabLayout實例和賓果游戲! 你有刷卡tabLayout。

以下是我的一些使用兩個標簽的工作代碼:

MyActivity.java:

super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// Initializing tab and pager views
TabLayout tabLayout = (TabLayout) findViewById(R.id.my_tab_layout);
final ViewPager viewPager = (ViewPager) findViewById(R.id.my_view_pager);

// Making new tabs and adding to tabLayout
tabLayout.addTab(tabLayout.newTab().setText("First Tab"));
tabLayout.addTab(tabLayout.newTab().setText("Second Tab"));

// Adding fragments to a list
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, MyFirstTabFragment.class.getName()));
fragments.add(Fragment.instantiate(this, MySecondTabFragment.class.getName()));

// Attaching fragments into tabLayout with ViewPager
viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager(), fragments));
tabLayout.setupWithViewPager(viewPager);

SectionPagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;


public class SectionPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;

    public SectionPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "First Tab";
            case 1:
            default:
                return "Second Tab";
        }
    }
}

MyFirstTabFragment.java:

public class MyFirstTabFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Declare your first fragment here
        return inflater.inflate(R.layout.my_first_fragment_layout, container, false);
    }
}

MySecondTabFragment.java:

public class MySecondTabFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Declare your second fragment here
        return inflater.inflate(R.layout.my_second_fragment_layout, container, false);
    }
}

main_layout.xml

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

    <!-- Declare android.support.v7.widget.Toolbar or... here -->

    <android.support.design.widget.TabLayout
            android:id="@+id/my_tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    <android.support.v4.view.ViewPager
            android:id="@+id/my_view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/my_tab_layout"/>
</RelativeLayout>

my_first_fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
    <!-- Declare first tab layout here -->
</RelativeLayout>

my_second_fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
    <!-- Declare second tab layout here -->
</RelativeLayout>

注意:這里我們使用來自支持庫v4的ViewPagerFragmentFragmentManagerFragmentPagerAdapter

希望能幫助到你。

將此行添加到您的Java代碼: - tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

或者您也可以在xml代碼中指定: - app:tabMode="scrollable"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM