Androidsupportdesign tablayout use of the detailed
Source: Internet
Author: User
<span id="Label3"></p><p><p>In the previous blog, "UI-ASD (Android support Design)", We <code>TabLayout</code> have a general understanding of the preliminary, this blog, separate <code>TabLayout</code> to continue to explain because: look at <code>Android Support Design</code> the library of several controls, in addition to, such as: <code>FAB</code> , <code>SnackBar</code> such as a few small controls commonly used, but also the <code>TabLayou</code> T ok, and other for example <code>CollapsingToolbarLayout</code> , such a control encapsulation is too strong, can only fix those scenes to use (purely personal opinion).<br>What <code>TabLayout</code> about that? Previous blog We just simply understand, today this blog we use Tablayout with Viewpager using the Following.<br>Before we start, let's take a look at the official website documentation and how to link with viewpager:</p></p> <blockquote> <blockquote> <p>You should set a listener via Setontabselectedlistener (ontabselectedlistener) to being notified when any tab ' s selection stat E has been changed.<br>If you ' re using a viewpager together with this layout, you can use Settabsfrompageradapter (pageradapter) which would popula Te the tabs using the given Pageradapter ' s page titles. You should also use a tablayout.tablayoutonpagechangelistener to forward the scroll and selection changes to this layout</p> </blockquote> </blockquote><p><p>well, It looks easy, simple translation is:</p></p> <blockquote> <blockquote> <p>To configure a listener to respond to the selection status of a tab by Setontabselectedlistener<br>You can use the Pageradapter page title from Settabsfrompageradapter.<br>Use Tablayout.tablayoutonpagechangelistener to Slide</p> </blockquote> </blockquote><p><p>ok, basically, in the general translation process, in fact, the steps have been listed, then we just need to follow the above steps to write our code is OK.<br>Come on~, the first appearance is the XML layout file:</p></p><pre class="prettyprint"><code class="language-xml hljs "><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">linearlayout </span> <span class="hljs-attribute">xmlns: Android </span> =<span class="hljs-value"> "http://schemas.android.com/apk/res/android" </span> <span class=" Hljs-attribute ">xmlns:app </span> =<span class=" hljs-value ">" http://schemas.android.com/apk/res-auto "</span> Span class= "hljs-attribute" >xmlns:tools </span> =<span class="hljs-value"> "http://schemas.android.com/tools" </span> <span class="hljs-attribute">android:layout_width </span> =<span class="hljs-value"> "match_parent" </span> <span class="hljs-attribute">android:layout_height </span> =<span class="hljs-value"> "match_parent" </span> <span class="hljs-attribute">android:orientation </span> =<span class="hljs-value"> "vertical" </span> <span class=" Hljs-attribute ">tools:context </span> =<span class=" hljs-value ">". Mainactivity "</span>; </span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">android.support.design.widget.tablayout </span> <span class=" Hljs-attribute ">android:id </span> =<span class=" hljs-value " " @+id tl "< span> <span class=" hljs-attribute "> Android:layout_width </span> =<span class="hljs-value"> "match_parent" </span> <span class="hljs-attribute">android : Layout_height </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">app: Tabindicatorcolor </span> =<span class="hljs-value"> "#FF00FF00" </span> <span class="hljs-attribute">app: Tabselectedtextcolor </span> =<span class="hljs-value"> "#FF00FF00" </span> <span class="hljs-attribute">app: Tabtextcolor </span> =<span class="hljs-value"> "#FF000000" </span> <span class="hljs-attribute">app:tabmode </span> =<span class="hljs-value"> "scrollable" </span> <span class="hljs-attribute">app:tabgravity </span> =<span class=" Hljs-value ">" Center "</span>/> </span></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">android.support.v4.view.ViewPager</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/viewpager"</span><span class="hljs-attribute">android: Layout_width</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span>/> </span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">linearlayout</span>></span></span></code></pre><p><p>well, It's simple, one <code>TabLayout</code> and one <code>ViewPager</code> , and it's arranged up and Down.</p></p><p><p>In activity, we first want to find these two controls:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs "> mTabLayout = (TabLayout) findViewById(R.id.tl); mViewPager = (ViewPager) findViewById(R.id.viewpager);</code></pre></pre><p><p>To demonstrate the program, two Pseudo-data is also required to act as the content and title of the Viewpager.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword">private</span><span class="hljs-keyword">new</span> String[<span class="hljs-number">20</span>];<span class="hljs-keyword">private</span><span class="hljs-keyword">new</span> String[<span class="hljs-number">20</span>];{ <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i<<span class="hljs-number">20</span>;i++) { <span class="hljs-string">"title"</span> + i; <span class="hljs-string">"data"</span> + i; }}</code></pre></pre><p><p>below, we will follow the above steps, a little bit to the code:<br>The first step is to set <code>TabLayout</code> the tab listener:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs">mtablayout.setontabselectedlistener (<span class=" Hljs-keyword ">new </span> tablayout.ontabselectedlistener () {<span class=" hljs-annotation "> @Override </span> <span class="hljs-keyword">public </span> <span class="hljs-keyword">void </span> <span class="hljs-title">ontabselected </span> (tablayout.tab Tab) {mviewpager.setcurrentitem (tab.getposition ()); } <span class="hljs-annotation"> @Override </span> <span class="hljs-keyword">public </span> <span clas s="hljs-keyword">void </span> <span class="hljs-title">ontabunselected </span> (tablayout.tab tab) {} <span class="hljs-annotation"> @Override </span> <span class="hljs-keyword">public </span> <span class="hljs-keyword">void </span> <span class="hljs-title">ontabreselected </span> (tablayout.tab Tab) { } });</code></pre></pre><p><p>When our tab is selected, let <code>viewpager</code> 's Select the corresponding Item.<br>The second step is to set <code>Tab</code> the caption from <code>PagerAdapter.getPageTitle()</code> .</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs ">mTabLayout.setTabsFromPagerAdapter(mAdapter);</code></pre></pre><p><p>The third step, set <code>TabLayout.TabLayoutOnPageChangeListener</code> , to who set it? Of course it is viewpager, how to set it?</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword">final</span> TabLayout.TabLayoutOnPageChangeListener listener = <span class="hljs-keyword">new</span> TabLayout.TabLayoutOnPageChangeListener(mTabLayout);mViewPager.addOnPageChangeListener(listener);</code></pre></pre><p><p>oh, the original <code>TabLaout.TabLayoutOnPageChangeLisetener</code> inherited from <code>OnPageChangeListener</code> , not to see:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword">public</span><span class="hljs-keyword">static</span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TabLayoutOnPageChangeListener</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">OnPageChangeListener</span> {</span>...}</code></pre></pre><p><p>Note that this is addonpagechangelistener, which means you can add N listener without being Overwritten.<br>We're all done here, but don't forget to set the adapter for Viewpage.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs ">mViewPager.setAdapter(mAdapter);</code></pre></pre><p><p>The last is the adapter code:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Pageradapter Madapter =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Pageradapter () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>Charsequence<span class="hljs-title"><span class="hljs-title">Getpagetitle</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Position) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>mtitle[position]; }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span> <span class="hljs-title"><span class="hljs-title">GetCount</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>mdata.length; }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>Object<span class="hljs-title"><span class="hljs-title">Instantiateitem</span></span>(View container,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Position) {TextView TV =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>TextView (mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); Tv.settextsize (<span class="hljs-number"><span class="hljs-number">.</span></span>f); Tv.settext (mdata[position]); ((viewpager) container). AddView (tv);<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Tv }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Destroyitem</span></span>(viewgroup container,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>position, Object Object) {((viewpager) container). removeview ((View) object); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">Isviewfromobject</span></span>(view view, Object Object) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>View = = object; } };</code></pre><p><p>Run the code and see the Effect:<br></p></p><p><p>Isn't it simple? A few lines of code will take care of this effect, of course, you can also choose to rewrite the view of the way to achieve, you can refer to my other blog "make history of the most easy to use tab indicator--indicator."<br>...<br>If you feel that it's easy, then you're too easy to meet, and Google gives us a way to encapsulate these steps in one piece, so that developers can get it done. <code>public void setupWithViewPager (ViewPager viewPager)</code>, go ahead and look at the description of the Document:</p></p> <blockquote> <blockquote> <p>The One-stop shop is setting up this tablayout with a viewpager.</p> <p>This method would:</p> <p>Add a Viewpager.onpagechangelistener that'll forward events to this Tablayout.<br>Populate the Tablayout ' s tabs from the Viewpager ' s pageradapter.<br>Set our Tablayout.ontabselectedlistener which would forward selected events to the Viewpager</p> </blockquote> </blockquote><p><p>The general explanation is:</p></p> <blockquote> <blockquote> <p>This method is <code>addOnPageChangeListener</code> and <code>setOnTabSelectedListener</code> the Encapsulation.</p> </blockquote> </blockquote><p><p>So we comment out the corresponding code and replace it with <code>setupWithViewPager</code> .</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-comment"><span class="hljs-comment">//mtablayout.setontabselectedlistener (new tablayout.ontabselectedlistener () {</span></span><span class="hljs-comment"><span class="hljs-comment">//@Override</span></span><span class="hljs-comment"><span class="hljs-comment">//public void ontabselected (tablayout.tab Tab) {</span></span><span class="hljs-comment"><span class="hljs-comment">//mviewpager.setcurrentitem (tab.getposition ());</span></span><span class="hljs-comment"><span class="hljs-comment">// }</span></span><span class="hljs-comment"><span class="hljs-comment">//</span></span><span class="hljs-comment"><span class="hljs-comment">//@Override</span></span><span class="hljs-comment"><span class="hljs-comment">//public void ontabunselected (tablayout.tab Tab) {</span></span><span class="hljs-comment"><span class="hljs-comment">//</span></span><span class="hljs-comment"><span class="hljs-comment">// }</span></span><span class="hljs-comment"><span class="hljs-comment">//</span></span><span class="hljs-comment"><span class="hljs-comment">//@Override</span></span><span class="hljs-comment"><span class="hljs-comment">//public void ontabreselected (tablayout.tab Tab) {</span></span><span class="hljs-comment"><span class="hljs-comment">//</span></span><span class="hljs-comment"><span class="hljs-comment">// }</span></span><span class="hljs-comment"><span class="hljs-comment">// });</span></span>Mtablayout.settabsfrompageradapter (madapter);<span class="hljs-comment"><span class="hljs-comment">//final Tablayout.tablayoutonpagechangelistener listener =</span></span><span class="hljs-comment"><span class="hljs-comment">//new Tablayout.tablayoutonpagechangelistener (mtablayout);</span></span><span class="hljs-comment"><span class="hljs-comment">//mviewpager.addonpagechangelistener (listener);</span></span>Mviewpager.setadapter (madapter); Mtablayout.setupwithviewpager (mviewpager);</code></pre><p><p>The effect is the above effect, but the code is significantly less, just two lines of code to get it Done.<br>But notice that <code>setupWithViePager</code> you have to <code>ViewPager.setAdapter()</code> call it later! Why is it? Source code to find the Answer:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">setupWithViewPager</span>(ViewPager viewPager) { PagerAdapter adapter = viewPager.getAdapter(); <span class="hljs-keyword">if</span><span class="hljs-keyword">null</span>) { <span class="hljs-keyword">throw</span><span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">"ViewPager does not have a PagerAdapter set"</span>); <span class="hljs-keyword">else</span> { ... }}</code></pre></pre><p><p>First go to get the Viewpager on the adapter, if there is no adapter, then throw an exception!<br>well, to now <code>TabLayout</code> and a <code>ViewPager</code> use of the usage of the explanation is finished, believe it is very simple, just two lines of code can be done.<br>In the future projects, will certainly often use them two good base friend, but so easy, not afraid of ~ ~</p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Androidsupportdesign tablayout use of the detailed</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service