Android override HorizontalScrollView to simulate ViewPager effect, androidviewpager
The ViewPager class provided by Android is too complex and sometimes unnecessary. Therefore, rewrite a HorizontalScrollView to achieve similar results. It can also be used as a Gallery.
The idea is simple. It is to rewrite the onTouchEvent event and perform the smoothScroll operation when the finger is lifted or canceled. For details, refer to the Code:
Layout file: activity_test.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <com.example.testxinye.MyScrollView 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <LinearLayout 8 android:id="@+id/container" 9 android:layout_width="match_parent"10 android:layout_height="match_parent"11 android:orientation="horizontal" >12 13 </LinearLayout>14 </com.example.testxinye.MyScrollView>
Activity Class: TestActivity. java
1 package com. example. testxinye; 2 3 import android. app. activity; 4 import android. graphics. color; 5 import android. OS. bundle; 6 import android. util. displayMetrics; 7 import android. widget. imageView; 8 import android. widget. imageView. scaleType; 9 import android. widget. linearLayout; 10 import android. widget. linearLayout. layoutParams; 11/** 12*13 * @ author xinye14 * 15 */16 public class TestActivity extends Activity {17 private LinearLayout mContainer = null; 18 @ Override19 protected void onCreate (Bundle savedInstanceState) {20 // TODO Auto-generated method stub21 super. onCreate (savedInstanceState); 22 setContentView (R. layout. activity_test); 23 24 mContainer = (LinearLayout) findViewById (R. id. container); 25 26 LayoutParams params = new LayoutParams (getWinWidth (), getWinHeight (); 27 28 ImageView imageView1 = new ImageView (this); 29 imageView1.setLayoutParams (params ); 30 imageView1.setImageResource (R. drawable. call_show_medal5); 31 imageView1.setScaleType (ScaleType. CENTER); 32 mContainer. addView (imageView1); 33 34 ImageView imageView2 = new ImageView (this); 35 imageView2.setLayoutParams (params); 36 imageView2.setImageResource (R. drawable. call_show_medal1); 37 imageView2.setScaleType (ScaleType. CENTER); 38 imageView2.setBackgroundColor (Color. RED); 39 mContainer. addView (imageView2); 40 41 ImageView imageView3 = new ImageView (this); 42 imageView3.setLayoutParams (params); 43 imageView3.setImageResource (R. drawable. call_show_medal); 44 imageView3.setScaleType (ScaleType. CENTER); 45 imageView3.setBackgroundColor (Color. GRAY); 46 mContainer. addView (imageView3); 47 48 49 ImageView imageView4 = new ImageView (this); 50 imageView4.setLayoutParams (params); 51 imageView4.setImageResource (R. drawable. call_show_med_3); 52 imageView4.setScaleType (ScaleType. CENTER); 53 imageView4.setBackgroundColor (Color. BLUE); 54 mContainer. addView (imageView4); 55 56 57 ImageView imageView5 = new ImageView (this); 58 imageView5.setLayoutParams (params); 59 imageView5.setImageResource (R. drawable. call_show_medal4); 60 imageView5.setScaleType (ScaleType. CENTER); 61 imageView5.setBackgroundColor (Color. GREEN); 62 mContainer. addView (imageView5); 63 64 65 66} 67 68 @ Override69 protected void onResume () {70 // (MyScrollView) mContainer. getParent ()). init (); 71 super. onResume (); 72} 73 74 private int getWinWidth () {75 DisplayMetrics dm = new DisplayMetrics (); 76 // get screen information 77 getWindowManager (). getdefadisplay display (). getMetrics (dm); 78 return dm. widthPixels; 79} 80 private int getWinHeight () {81 DisplayMetrics dm = new DisplayMetrics (); 82 // get screen information 83 getWindowManager (). getdefadisplay display (). getMetrics (dm); 84 return dm. heightPixels; 85} 86}
Rewritten HorizontalScrollView: MyScrollView. java
1 package com. example. testxinye; 2 3 import java. util. arrayList; 4 5 import android. content. context; 6 import android. util. attributeSet; 7 import android. view. motionEvent; 8 import android. view. view; 9 import android. view. viewGroup; 10 import android. widget. horizontalScrollView; 11/** 12*13 * @ author XINYE 14*15 */16 public class MyScrollView extends HorizontalScrollView {17 private int subC HildCount = 0; 18 private ViewGroup firstChild = null; 19 private int downX = 0; 20 private int currentPage = 0; 21 private ArrayList <Integer> pointList = new ArrayList <Integer> (); 22 23 public MyScrollView (Context context, AttributeSet attrs, int defStyle) {24 super (context, attrs, defStyle); 25 init (); 26} 27 28 29 public MyScrollView (Context context, AttributeSet attrs) {30 super (context, Trs); 31 init (); 32} 33 34 public MyScrollView (Context context) {35 super (context); 36 init (); 37} 38 private void init () {39 setHorizontalScrollBarEnabled (false); 40} 41 @ Override 42 protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {43 super. onMeasure (widthMeasureSpec, heightMeasureSpec); 44 receiveChildInfo (); 45} 46 public void receiveChildInfo () {47 48 firstChild = (ViewGroup) getChildAt (0); 49 if (firstChild! = Null) {50 subChildCount = firstChild. getChildCount (); 51 for (int I = 0; I <subChildCount; I ++) {52 if (View) firstChild. getChildAt (I )). getWidth ()> 0) {53 pointList. add (View) firstChild. getChildAt (I )). getLeft (); 54} 55} 56} 57 58} 59 @ Override 60 public boolean onTouchEvent (MotionEvent ev) {61 switch (ev. getAction () {62 case MotionEvent. ACTION_DOWN: 63 downX = (int) ev. getX (); 64 break; 65 case MotionEvent. ACTION_MOVE: {66 67} break; 68 case MotionEvent. ACTION_UP: 69 case MotionEvent. ACTION_CANCEL: {70 if (Math. abs (ev. getX ()-downX)> getWidth ()/4) {71 if (ev. getX ()-downX> 0) {72 smoothScrollToPrePage (); 73} else {74 smoothScrollToNextPage (); 75} 76} else {77 smoothScrollToCurrent (); 78} 79 return true; 80} 81} 82 return super. onTouchEvent (ev); 83} 84 85 private void smoothScrollToCurrent () {86 smoothScrollTo (pointList. get (currentPage), 0); 87} 88 89 private void smoothScrollToNextPage () {90 if (currentPage <subChildCount-1) {91 currentPage ++; 92 smoothScrollTo (pointList. get (currentPage), 0); 93} 94} 95 96 private void smoothScrollToPrePage () {97 if (currentPage> 0) {98 currentPage --; 99 smoothScrollTo (pointList. get (currentPage), 0); 100} 101} 102/** 103 * Next page 104 */105 public void nextPage () {106 smoothScrollToNextPage (); 107} 108/** 109 * Previous page 110 */111 public void prePage () {112 smoothScrollToPrePage (); 113} 114/** 115 * jump to the specified page 116 * @ param page117 * @ return118 */119 public boolean gotoPage (int page) {120 if (page> 0 & page <subChildCount-1) {121 smoothScrollTo (pointList. get (page), 0); 122 currentPage = page; 123 return true; 124} 125 return false; 126} 127}