Merge "Test app for ViewPager." into mnc-ub-dev

This commit is contained in:
Kirill Grouchnikov
2015-10-28 21:04:34 +00:00
committed by Android (Google) Code Review
5 changed files with 201 additions and 0 deletions

View File

@@ -359,6 +359,14 @@
</intent-filter>
</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"
android:label="Graphics/RoundedBitmapDrawable">
<intent-filter>

View File

@@ -0,0 +1,57 @@
<?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

@@ -0,0 +1,21 @@
<?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.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="32sp"
android:gravity="center"/>

View File

@@ -195,4 +195,9 @@
<string name="drawable_compat_color_tint">Color tint</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>

View File

@@ -0,0 +1,110 @@
/*
* 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.
*/
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 java.lang.Override;
import java.lang.Runnable;
public class ViewPagerActivity extends FragmentActivity {
private static int[] PAGE_COLORS = { 0xFF700000, 0xFF500020, 0xFF300030, 0xFF200050,
0xFF000070};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager_sample);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return PAGE_COLORS.length;
}
@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
args.putInt(DemoObjectFragment.ARG_INDEX, position);
fragment.setArguments(args);
return fragment;
}
});
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;
}
}
}