Android ViewPager Lesson 1: androidviewpager
To learn about the new features of the new Android version, we should start from scratch. This is a new widget that Android3.0 has added. I have been familiar with it before, but I have not studied it well. Today I wrote a small program, study ViewPager.
This program supports views with sliding left and right, and the core is ViewPager. All the explanations are in the annotations.
The Code is as follows:
MainActivity. java:
Package com. android3; import android. annotation. suppressLint; import android. content. intent; import android. graphics. bitmapFactory; import android. graphics. matrix; import android. OS. bundle; import android. support. v4.view. viewPager; import android. support. v7.app. appCompatActivity; import android. support. v7.widget. toolbar; import android. util. displayMetrics; import android. view. gravity; import android. view. layo UtInflater; import android. view. view; import android. view. animation. animation; import android. view. animation. translateAnimation; import android. widget. button; import android. widget. imageView; import android. widget. linearLayout; import android. widget. textView; import java. util. arrayList; import java. util. list; public class MainActivity extends AppCompatActivity implements View. onClickListener, ViewPager. onPa GeChangeListener {private ViewPager viewPager; private ArrayList <View> viewList; private List <String> titleList; private MyPagerAdapter adapter; private ImageView cursor; private LinearLayout titleBar; private float cursorW = 0; private float offset = 0; private float currentIndex = 0; private float screenW = 0; private float currentX = 0; private float fScreenW; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initToolbar (); initViewPager ();}/*** ViewPager ensures that there are three views in the cache, that is, the gray on the right and the center on the left is destroy, * // @ SuppressLint ("InflateParams") private void initViewPager () {viewPager = (ViewPager) findViewById (R. id. viewpager); titleBar = (LinearLayout) findViewById (R. id. titleBar); LayoutInflater inflater = getLa YoutInflater (); // create four View view1 = inflater. inflate (R. layout. viewpage_01, null); View view2 = inflater. inflate (R. layout. viewpage_02, null); View view3 = inflater. inflate (R. layout. viewpage_03, null); View view4 = inflater. inflate (R. layout. viewpage_04, null); viewList = new ArrayList <> (); // load the View to be displayed by page into the viewList array. add (view1); viewList. add (view2); viewList. add (view3); viewList. add (view4); adap Ter = new MyPagerAdapter (viewList); titleList = new ArrayList <> (); titleList. add ("first page"); titleList. add ("second page"); titleList. add ("Third page"); titleList. add ("fourth page"); for (int I = 0; I <titleList. size (); I ++) {TextView textView = new TextView (this); LinearLayout. layoutParams params = new LinearLayout. layoutParams (LinearLayout. layoutParams. WRAP_CONTENT, LinearLayout. layoutParams. WRAP_CONTENT); params. wei Ght = 1; params. setMargins (5, 3, 5, 3); textView. setLayoutParams (params); textView. setText (titleList. get (I); textView. setTextSize (15); textView. setGravity (Gravity. CENTER); titleBar. addView (textView);} initCursorPos (); // initialize the position of the indicator viewPager. setAdapter (adapter); // bind the adapter viewPager. addOnPageChangeListener (this); // Note: setOnPageChangeListener obsolete}/*** unit px */public void initCursorPos () {// initialize the animation cu Rsor = (ImageView) findViewById (R. id. cursor); cursorW = BitmapFactory. decodeResource (getResources (), R. mipmap. cursor ). getWidth (); // obtain the image width DisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); screenW = dm. widthPixels; // get the resolution width fScreenW = screenW/viewList. size (); offset = (fScreenW-cursorW)/2; // calculate the offset Matrix matrix = new Matrix (); matrix. postTranslate (Offset, 0); cursor. setImageMatrix (matrix); // sets the initial animation position ### original position currentX = offset;} private void initToolbar () {Toolbar mToolbar = (Toolbar) findViewById (R. id. toolbar); Button btnRight = (Button) mToolbar. findViewById (R. id. btnRight); mToolbar. setTitle (""); mToolbar. setNavigationIcon (R. mipmap. back); setsuppactionactionbar (mToolbar); mToolbar. setNavigationOnClickListener (this); btnRight. setOnClickList Ener (this) ;}@ Override public void onClick (View view) {switch (view. getId () {case-1: finish (); break; case R. id. btnRight: Intent intent = new Intent (MainActivity. this, SecondActivity. class); startActivity (intent); break ;}@ Override public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {}@ Override public void onPageSelected (int position) {float X = fScree NW * position; // The distance between the slider position and the original position on this page // The distance between the current position and the original position of currentX: TranslateAnimation animation = new TranslateAnimation (currentX, X, 0, 0);/*** public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {* throw new RuntimeException ("Stub! "); *} * In the X axis direction: * fromXDelta animation starts from the original position + fromXDelta ** the toXDelta animation ends from the original position + toXDelta ** in the Y axis direction: * fromYDelta animation starts from the original position + fromYDelta ** the toYDelta animation ends from the original position + toYDelta ***/currentX = fScreenW * position; // update the current position currentIndex = position; // Save the previous View number animation. setFillAfter (true); // Save the animation. setDuration (300); // animation duration cursor. startAnimation (animation); // start} @ Override public void onPageScrollStateChanged (int state ){}}
MyPagerAdapter. java
Package com. android3; import android. support. v4.view. pagerAdapter; import android. view. view; import android. view. viewGroup; import java. util. list;/***** Created by Administrator on 2016/10/25. */public class MyPagerAdapter extends PagerAdapter {private List <View> list = null; // The constructor transfers the set containing views to the adapter MyPagerAdapter (List <View> list) {this. list = list;}/***** @ return the total number of views to be displayed */@ Override public int getCount () {return list. size ();}/***** this method determines whether the Key and View are correctly mapped ** and the same key cannot be input in the same container, otherwise, the behavior will be disordered * @ param view * @ param object * @ return */@ Override public boolean isViewFromObject (View view, Object object) {return view = object ;} /*** because the ViewPager container only saves the View at the current position and the adjacent View, when the number position of the View in the container exceeds 1 from the current position, * remove the View * @ param container * @ param position: View location to be lost * @ param object */@ Override public void destroyItem (ViewGroup container, int position, object object) {container. removeView (list. get (position);}/***** first, judge whether the left and right views of the current position are in the container. If not, call the instantiateItem method to add the adjacent view (not in the container before) to the container. * return this View, as the ing Key ** @ param container * @ param position starting from 0 * @ return view corresponding Key */@ Override public Object instantiateItem (ViewGroup container, int position) {container. addView (list. get (position); return list. get (position );}}
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.android3.MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> <TextView android:id="@+id/tv_toolbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/title" android:textColor="#ffffff" android:textSize="14sp" android:textStyle="bold" /> <Button android:id="@+id/btnRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:layout_marginRight="10dp" android:text="@string/submit" android:textColor="#ffffff" android:textSize="12sp" android:textStyle="bold" /> </android.support.v7.widget.Toolbar> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/toolbar" android:orientation="vertical"> <LinearLayout android:id="@+id/titleBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:orientation="horizontal" /> <ImageView android:id="@+id/cursor" android:layout_width="match_parent" android:layout_height="5dp" android:scaleType="matrix" android:src="@mipmap/cursor" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/> </LinearLayout></RelativeLayout>
ViewPager_01.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"></LinearLayout>
Viewpager_02.xml, viewpager_03.xml, and viewpager_04.xml (Omitted) only have different background colors.
By: naughty child
Source: http://www.cnblogs.com/xiaotaoqi/p/5996742.html/>
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent and give it clearly on the article page.