diff options
author | Chris Banes <chrisbanes@google.com> | 2015-11-03 13:36:35 +0000 |
---|---|---|
committer | Chris Banes <chrisbanes@google.com> | 2015-11-16 09:11:03 +0000 |
commit | 372bb98e8acf4a75401f0bfa58afcd2e493db156 (patch) | |
tree | 6843739e8722c32ed6eb9501481a382b719bd29f | |
parent | d775d3a528d7a1d4dc41a7cc847322a2fbef6da3 (diff) | |
download | android_development-372bb98e8acf4a75401f0bfa58afcd2e493db156.tar.gz android_development-372bb98e8acf4a75401f0bfa58afcd2e493db156.tar.bz2 android_development-372bb98e8acf4a75401f0bfa58afcd2e493db156.zip |
Update TabLayout demo to use new ViewPager functionality
Also removed an old unused layout file
BUG: 20908355
Change-Id: I95d79f7faec68122b43c68b621af4121c52c17aa
3 files changed, 42 insertions, 86 deletions
diff --git a/samples/SupportDesignDemos/res/layout/design_tabs.xml b/samples/SupportDesignDemos/res/layout/design_tabs.xml deleted file mode 100644 index b52d80833..000000000 --- a/samples/SupportDesignDemos/res/layout/design_tabs.xml +++ /dev/null @@ -1,41 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<!-- - Copyright (C) 2015 The Android Open Source Project - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. ---> - -<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.v7.widget.Toolbar - android:id="@+id/toolbar" - android:layout_height="?attr/actionBarSize" - android:layout_width="match_parent" - android:background="?attr/colorPrimary" - android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" - app:contentInsetStart="72dp" - app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> - - <android.support.design.widget.TabLayout - android:id="@+id/tabs" - android:layout_height="wrap_content" - android:layout_width="match_parent" - android:background="?attr/colorPrimary" - android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" - app:tabContentStart="72dp"/> - -</LinearLayout> diff --git a/samples/SupportDesignDemos/res/layout/design_tabs_viewpager.xml b/samples/SupportDesignDemos/res/layout/design_tabs_viewpager.xml index f25a3ce5f..11ddca639 100644 --- a/samples/SupportDesignDemos/res/layout/design_tabs_viewpager.xml +++ b/samples/SupportDesignDemos/res/layout/design_tabs_viewpager.xml @@ -54,19 +54,22 @@ android:id="@+id/btn_add_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="@string/add_tab"/> + android:text="@string/add_tab" + android:onClick="addTab"/> <Button android:id="@+id/btn_remove_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="@string/remove_tab"/> + android:text="@string/remove_tab" + android:onClick="removeTab"/> <Button android:id="@+id/btn_select_first_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="@string/select_first_tab"/> + android:text="@string/select_first_tab" + android:onClick="selectFirstTab"/> </LinearLayout> diff --git a/samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java index 8efccb530..26b61083f 100644 --- a/samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java +++ b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java @@ -21,8 +21,6 @@ import com.example.android.support.design.R; import android.os.Bundle; import android.support.design.widget.TabLayout; -import android.support.design.widget.TabLayout.TabLayoutOnPageChangeListener; -import android.support.design.widget.TabLayout.ViewPagerOnTabSelectedListener; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; @@ -46,6 +44,8 @@ public class TabLayoutUsage extends AppCompatActivity { private ViewPager mViewPager; private CheesePagerAdapter mPagerAdapter; + private final Random mRandom = new Random(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -57,50 +57,29 @@ public class TabLayoutUsage extends AppCompatActivity { getSupportActionBar().setDisplayHomeAsUpEnabled(true); mTabLayout = (TabLayout) findViewById(R.id.tabs); - mViewPager = (ViewPager) findViewById(R.id.tabs_viewpager); + mPagerAdapter = new CheesePagerAdapter(); mViewPager.setAdapter(mPagerAdapter); - mViewPager.setOnPageChangeListener(new TabLayoutOnPageChangeListener(mTabLayout)); - mTabLayout.setOnTabSelectedListener(new ViewPagerOnTabSelectedListener(mViewPager)); - setupButtons(); + mTabLayout.setupWithViewPager(mViewPager); + setupRadioGroup(); } - private void setupButtons() { - findViewById(R.id.btn_add_tab).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - addRandomTab(); - } - }); - - findViewById(R.id.btn_remove_tab).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (mTabLayout.getTabCount() >= 1) { - mTabLayout.removeTabAt(mTabLayout.getTabCount() - 1); - mPagerAdapter.removeTab(); - } - } - }); + public void addTab(View view) { + String cheese = Cheeses.sCheeseStrings[mRandom.nextInt(Cheeses.sCheeseStrings.length)]; + mPagerAdapter.addTab(cheese); + } - findViewById(R.id.btn_select_first_tab).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (mTabLayout.getTabCount() > 0) { - mViewPager.setCurrentItem(0); - } - } - }); + public void selectFirstTab(View view) { + if (mTabLayout.getTabCount() > 0) { + mViewPager.setCurrentItem(0); + } } - private void addRandomTab() { - Random r = new Random(); - String cheese = Cheeses.sCheeseStrings[r.nextInt(Cheeses.sCheeseStrings.length)]; - mTabLayout.addTab(mTabLayout.newTab().setText(cheese)); - mPagerAdapter.addTab(cheese); + public void removeTab(View view) { + mPagerAdapter.removeTab(); } private void setupRadioGroup() { @@ -156,7 +135,6 @@ public class TabLayoutUsage extends AppCompatActivity { } private static class CheesePagerAdapter extends PagerAdapter { - private final ArrayList<CharSequence> mCheeses = new ArrayList<>(); public void addTab(String title) { @@ -177,21 +155,31 @@ public class TabLayoutUsage extends AppCompatActivity { } @Override + public int getItemPosition(Object object) { + final Item item = (Item) object; + final int index = mCheeses.indexOf(item.cheese); + return index >= 0 ? index : POSITION_NONE; + } + + @Override public Object instantiateItem(ViewGroup container, int position) { - TextView tv = new TextView(container.getContext()); + final TextView tv = new TextView(container.getContext()); tv.setText(getPageTitle(position)); tv.setGravity(Gravity.CENTER); tv.setTextAppearance(tv.getContext(), R.style.TextAppearance_AppCompat_Title); - container.addView(tv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); - return tv; + Item item = new Item(); + item.cheese = mCheeses.get(position); + item.view = tv; + return item; } @Override public boolean isViewFromObject(View view, Object object) { - return view == object; + final Item item = (Item) object; + return item.view == view; } @Override @@ -201,7 +189,13 @@ public class TabLayoutUsage extends AppCompatActivity { @Override public void destroyItem(ViewGroup container, int position, Object object) { - container.removeView((View) object); + final Item item = (Item) object; + container.removeView(item.view); + } + + private static class Item { + TextView view; + CharSequence cheese; } } |