Tablayout combined with viewpager in Android to realize page switching

1、 Realization idea

1. Add dependencies in build.gradle, for example:

compile 'com.android.support:support-v4:23.4.0' compile 'com.android.support:design:23.4.0'

You can also replace support-v4 with appcompat-v7, for example:

compile 'com.android.support:appcompat-v7:23.4.0'

Because appcompat-v7 depends on support-v4.

For more instructions, refer to the support library section of the official document.

2. Add tablayout and viewpager to XML, for example:

TabLayout:

(1) Tabmode has two attributes, one is "scrollable", which is used for multiple tags; The other is "fixed", which is used for fewer labels. It will make all labels evenly distributed on the screen, so there can be no more labels, and the name cannot be long, otherwise the display will be incomplete.

(2) Tabindicator refers to the indicator bar under the text. When a tab is selected, the indicator bar will appear below the text.

3. Get view object

4. Create a subclass of fragmentstatepageradapter and implement the construction method

Create an instance object of this class

ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

In this step, you can choose to implement the subclass of fragmentpageradapter or the subclass of fragmentstatepageradapter.

Fragmentpageradapter is used for those with fewer pages and fewer fragments. As long as the user stays in the current activity, the fragments will not be destroyed, so the memory consumption will be relatively large.

The working principle of the fragmentstatepageradapter is similar to that of listview. As long as the fragment is invisible to the user, it will be destroyed and only its state will be retained.

Because I use the fragment under the V4 compatibility package, I need to use getsupportfragmentmanager () to obtain the fragmentmanager.

5. Set viewpager and tablayout

2、 Perfect adapter

1. Override three methods

2. Create the title data of the tab:

Private string [] mtitles = new string [] {"Chinese", "English", "Mathematics", "physics", "biology", "Chemistry", "geography", "politics", "history"};

Create a subclass of fragment:

Create the collection object of the fragment and add the instance object to the collection:

3. Modify methods in the adapter

4. Transfer data to adapter

3、 Perfect fragment

1、fragment_ view_ pager_ item.xml

2. How to improve fragment

When creating a fragment, the oncreate method will be called to initialize some state information for recovery after suspension or stop.

When the fragment loads the view for the first time, the oncreateview method will be called to load and initialize the view, and the root view of the fragment layout should be returned. The third parameter of the inflate method means whether to add the loaded layout (r.layout.fragment_view_pager_item) to the ViewGroup of the container. According to the official document, if the above example is done, the system has added this layout to the container, so it is false here.

Static rendering:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>