处理TabLayout标签点击

时间:2016-09-29 17:14:00

标签: android android-fragments android-viewpager android-tablayout

我有一个带有TabLayout和ViewPager的片段:

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

在我的Fragment文件中,在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在Android上运行,包括4.1和4.4?

感谢您的帮助!

2 个答案:

答案 0 :(得分: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 / layout / 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并连接适配器。 在ViewPager上设置TabLayout以将寻呼机与标签连接起来。

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

答案 1 :(得分:0)

像这样使用addOnTabSelectedListener

tabLayout.addOnTabSelectedListener(new OnTabSelectedListener() {
    @Override
    public void onTabSelected(Tab tab) {
         switch(tab.getPosition()) {
             case 0:
                // ...
         }
    }
}
相关问题