Add sample activity for ViewPager in Support4Demos

Bug: 23467280
Change-Id: I20453e15e5fb7b235da910d0c290641bd1c3d4c8
This commit is contained in:
Alan Viverette
2015-08-24 15:41:17 -04:00
committed by Kirill Grouchnikov
parent 0c356e44b3
commit 8d3349a080
5 changed files with 97 additions and 149 deletions

View File

@@ -297,6 +297,14 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".view.ViewPagerActivity"
android:label="@string/view_pager_layout_support">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".widget.SwipeRefreshLayoutActivity" <activity android:name=".widget.SwipeRefreshLayoutActivity"
android:label="@string/swipe" android:label="@string/swipe"
android:theme="@style/ThemeHoloLight"> android:theme="@style/ThemeHoloLight">
@@ -351,14 +359,6 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".view.ViewPagerActivity"
android:label="@string/view_pager_support">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".graphics.RoundedBitmapDrawableActivity" <activity android:name=".graphics.RoundedBitmapDrawableActivity"
android:label="Graphics/RoundedBitmapDrawable"> android:label="Graphics/RoundedBitmapDrawable">
<intent-filter> <intent-filter>

View File

@@ -14,8 +14,17 @@
limitations under the License. limitations under the License.
--> -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:textSize="32sp"
android:gravity="center"/> <android.support.v4.view.PagerTitleStrip
android:id="@+id/titles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>

View File

@@ -1,57 +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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"/>
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="end">
<CheckBox
android:id="@+id/view_pager_smooth_scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/view_pager_smooth_scroll"/>
<Button
android:id="@+id/view_pager_switch_tabs_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dip"
android:text="@string/view_pager_switch_tabs"/>
<Button
android:id="@+id/view_pager_double_switch_tabs_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dip"
android:text="@string/view_pager_double_switch_tabs"/>
</LinearLayout>
</LinearLayout>

View File

@@ -156,6 +156,8 @@
<string name="sample_transport_controller_activity">Media/TransportController</string> <string name="sample_transport_controller_activity">Media/TransportController</string>
<string name="view_pager_layout_support">View/View pager</string>
<string name="sliding_pane_layout_support">Widget/Sliding pane layout</string> <string name="sliding_pane_layout_support">Widget/Sliding pane layout</string>
<string name="sliding_pane_layout_summary">This activity illustrates the use of sliding panes. The content pane may be slid to one side on narrow devices to reveal the left pane used to select content. Sliding panes can be used to fit a UI intended for wider screens in a smaller space. Tapping the Action Bar\'s Up button at the left side of the bar will navigate up in the hierarchy, represented by the left pane. If you rotate the device to landscape mode, on most devices you will see that both panes fit together side by side with no sliding necessary.</string> <string name="sliding_pane_layout_summary">This activity illustrates the use of sliding panes. The content pane may be slid to one side on narrow devices to reveal the left pane used to select content. Sliding panes can be used to fit a UI intended for wider screens in a smaller space. Tapping the Action Bar\'s Up button at the left side of the bar will navigate up in the hierarchy, represented by the left pane. If you rotate the device to landscape mode, on most devices you will see that both panes fit together side by side with no sliding necessary.</string>
@@ -181,10 +183,4 @@
<string name="drawable_compat_no_tint">Not tint</string> <string name="drawable_compat_no_tint">Not tint</string>
<string name="drawable_compat_color_tint">Color tint</string> <string name="drawable_compat_color_tint">Color tint</string>
<string name="drawable_compat_color_list_tint">Color state list</string> <string name="drawable_compat_color_list_tint">Color state list</string>
<!-- ViewPager -->
<string name="view_pager_support">View/View pager</string>
<string name="view_pager_smooth_scroll">Smooth scroll</string>
<string name="view_pager_switch_tabs">Switch tabs</string>
<string name="view_pager_double_switch_tabs">Double-switch tabs</string>
</resources> </resources>

View File

@@ -14,97 +14,97 @@
* limitations under the License. * limitations under the License.
*/ */
package com.example.android.supportv4.view; package com.example.android.supportv4.view;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.supportv4.R; import com.example.android.supportv4.R;
import java.lang.Override; import android.app.Activity;
import java.lang.Runnable; import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
public class ViewPagerActivity extends FragmentActivity { import java.util.ArrayList;
private static int[] PAGE_COLORS = { 0xFF700000, 0xFF500020, 0xFF300030, 0xFF200050,
0xFF000070}; public class ViewPagerActivity extends Activity {
private ViewPager mPager;
private PagerTitleStrip mTitles;
private ColorPagerAdapter mAdapter;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager_sample); setContentView(R.layout.view_pager_layout);
mAdapter = new ColorPagerAdapter();
mAdapter.add("Red", Color.RED);
mAdapter.add("Green", Color.GREEN);
mAdapter.add("Blue", Color.BLUE);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mTitles = (PagerTitleStrip) findViewById(R.id.titles);
}
private static class ColorPagerAdapter extends PagerAdapter {
private ArrayList<Pair<String, Integer>> mEntries = new ArrayList<>();
public void add(String title, int color) {
mEntries.add(new Pair(title, color));
}
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@Override @Override
public int getCount() { public int getCount() {
return PAGE_COLORS.length; return mEntries.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
final View view = new View(container.getContext());
view.setBackgroundColor(mEntries.get(position).second);
// Unlike ListView adapters, the ViewPager adapter is responsible
// for adding the view to the container.
container.addView(view);
return new ViewHolder(view, position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// The adapter is also responsible for removing the view.
container.removeView(((ViewHolder) object).view);
}
@Override
public int getItemPosition(Object object) {
return ((ViewHolder) object).position;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return ((ViewHolder) object).view == view;
} }
@Override @Override
public CharSequence getPageTitle(int position) { public CharSequence getPageTitle(int position) {
return "Page " + position; return mEntries.get(position).first;
} }
@Override private static class ViewHolder {
public Fragment getItem(int position) { final View view;
Fragment fragment = new DemoObjectFragment(); final int position;
Bundle args = new Bundle();
args.putInt(DemoObjectFragment.ARG_INDEX, position); public ViewHolder(View view, int position) {
fragment.setArguments(args); this.view = view;
return fragment; this.position = position;
} }
});
final CheckBox smoothScroll = (CheckBox) findViewById(R.id.view_pager_smooth_scroll);
Button switchTabsButton = (Button) findViewById(R.id.view_pager_switch_tabs_button);
switchTabsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(2, smoothScroll.isChecked());
Toast.makeText(view.getContext(), "Current item = " + viewPager.getCurrentItem(),
Toast.LENGTH_SHORT).show();
}
});
Button doubleSwitchTabsButton =
(Button) findViewById(R.id.view_pager_double_switch_tabs_button);
doubleSwitchTabsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(0, smoothScroll.isChecked());
viewPager.setCurrentItem(2, smoothScroll.isChecked());
Toast.makeText(view.getContext(), "Current item = " + viewPager.getCurrentItem(),
Toast.LENGTH_SHORT).show();
}
});
}
public static class DemoObjectFragment extends Fragment {
public static final String ARG_INDEX = "index";
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// The last two arguments ensure LayoutParams are inflated
// properly.
View rootView = inflater.inflate(R.layout.view_pager_tab, container, false);
Bundle args = getArguments();
int position = args.getInt(ARG_INDEX);
rootView.setBackgroundColor(PAGE_COLORS[position]);
((TextView) rootView).setText(Integer.toString(position));
return rootView;
} }
} }
} }