# ViewPager

布局：

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMode="fixed"
        app:tabTextColor="#FF000000"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
```

Java代码：

```java
mTablayout.setupWithViewPager(mViewpager);
```

属性： tabMode：可选值scrollable和fixed

当设置为scrollable条目过多可以进行滚动，条目过少则会居左显示。:

![](/files/-M6c_gdAnaBa3DIwhXnk)

当设置为fixed，则不会进行滚动，tabGravity只有当tabMode是fixed时候起作用，tabGravity可选是是fill和center。 当设置为center时居中。

![](/files/-M6c_gdENLqlAcu7axxZ)

当设置为fill时将会占满所有的位置。

![](/files/-M6c_gdGWOkxWu6vruXz)

tabIndicatorColor：设置指示器颜色。

tabTextColor：设置文本颜色。

## 扩展更多

* [Great animations with PageTransformer](https://medium.com/@BashaChris/the-android-viewpager-has-become-a-fairly-popular-component-among-android-apps-its-simple-6bca403b16d4)
* [为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment？](http://www.cnblogs.com/dancefire/archive/2013/01/02/why-notifyDataSetChanged-does-not-work.html)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://android.malinkang.com/ui/viewpager.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
