HorizontalScrollView enables sliding between multiple pages

Source: Internet
Author: User

HorizontalScrollView enables sliding between multiple pages

First look at the effect:

Code:

PageView is an encapsulated class that inherits the HZ runtime? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcml6b250YWxTY3JvbGxWaWV3oaM8YnI + cda-vcd4kpha + PHByZSBjbGFzcz0 =" brush: java; "> package com. example. testandrid; import android. content. context; import android. util. attributeSet; import android. util. displayMetrics; import android. view. motionEvent; import android. view. view; import android. widget. horizontalScrollView; import android. widget. linearLa Yout; public class PageView extends HorizontalScrollView {private int mBaseScrollX; // sliding baseline. That is, the x value before clicking and sliding is used to calculate the relative sliding distance. Private int mScreenWidth; private int mScreenHeight; private LinearLayout mContainer; private boolean flag; private int mPageCount; // Number of pages public PageView (Context context, AttributeSet attrs) {super (context, attrs); DisplayMetrics dm = context. getApplicationContext (). getResources (). getDisplayMetrics (); mScreenWidth = dm. widthPixels; mScreenHeight = dm. heightPixels;}/*** Add a page to the end. * @ Param page */public void addPage (View page) {addPage (page,-1);}/*** Add a page. * @ Param page */public void addPage (View page, int index) {if (! Flag) {mContainer = (LinearLayout) getChildAt (0); flag = true;} LinearLayout. layoutParams params = new LinearLayout. layoutParams (mScreenWidth, mScreenHeight); if (index =-1) {mContainer. addView (page, params);} else {mContainer. addView (page, index, params);} mPageCount ++;}/*** removes a page. * @ Param index */public void removePage (int index) {if (mPageCount <1) {return;} if (index <0 | index> mPageCount-1) {return ;} mContainer. removeViewAt (index); mPageCount --;}/*** get the number of pages * @ return */public int getPageCount () {return mPageCount;}/*** get the relative slide position. Slide from right to left to return a positive value. Slide from left to right to return a negative value. * @ Return */private int getBaseScrollX () {return getScrollX ()-mBaseScrollX;}/*** move the x distance from the baseline. * @ Param x shifts right when x is a positive value. If it is a negative value, it shifts left. */Private void baseSmoothScrollTo (int x) {smoothScrollTo (x + mBaseScrollX, 0);} @ Overridepublic boolean onTouchEvent (MotionEvent ev) {int action = ev. getAction (); switch (action) {case MotionEvent. ACTION_UP ;} // Slide left, less than half, return in-situ else if (scrollX> 0) {baseSmoothScrollTo (0);} // slide right, less than half, returns the original else if (scrollX>-mScreenWidth/2) {baseSmoothScrollTo (0);} // slide right, more than half, move to the next else {baseSmoothScrollTo (-mScreenWidth ); mBaseScrollX-= mScreenWidth;} return true;} return super. onTouchEvent (ev );}}
Next is the layout, fragment_main.xml:

     
  
        
       
   
   
 

Finally, you can call it in the Activity.

Package com. example. testandrid; import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. view. layoutInflater; import android. widget. linearLayout; public class MainActivity extends Activity {private LayoutInflater inflater; private PageView mPageView; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. fragment_main); inflater = LayoutInflater. from (this); mPageView = (PageView) findViewById (R. id. pageview); // Add several pages LinearLayout layout = new LinearLayout (this); layout. setBackgroundColor (Color. BLUE); mPageView. addPage (layout); LinearLayout layout2 = new LinearLayout (this); layout2.setBackgroundColor (Color. YELLOW); mPageView. addPage (layout2); // here is a common xml layout file LinearLayout view = (LinearLayout) inflater. inflate (R. layout. page1, null); mPageView. addPage (view); // delete a page // mPageView. removePage (1 );}}

The comments in the Code are basically clear.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.