HorizontalScrollView实现多页左右滑动

先上图看看效果:

上代码:

PageView是封装后的一个类,继承了HorizontalScrollView。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.miquan;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.DisplayMetrics;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.widget.HorizontalScrollView;  
  9. import android.widget.LinearLayout;  
  10.   
  11. /** 
  12.  * 具体看博客:http://blog.csdn.net/qiantujava/article/details/42392127 
  13.  */  
  14. public class PageView extends HorizontalScrollView {  
  15.     private int mBaseScrollX;//滑动基线。也就是点击并滑动之前的x值,以此值计算相对滑动距离。  
  16.     private int mScreenWidth;  
  17.     private int mScreenHeight;  
  18.       
  19.     private LinearLayout mContainer;  
  20.     private boolean flag;  
  21.     private int mPageCount;//页面数量  
  22.       
  23.     private int mScrollX = 200;//滑动多长距离翻页  
  24.       
  25.     public PageView(Context context, AttributeSet attrs) {  
  26.         super(context, attrs);  
  27.   
  28.         DisplayMetrics dm = context.getApplicationContext().getResources()  
  29.                 .getDisplayMetrics();  
  30.         mScreenWidth = dm.widthPixels;  
  31.         mScreenHeight = dm.heightPixels;  
  32.     }  
  33.       
  34.     /** 
  35.      * 添加一个页面到最后。 
  36.      * @param page 
  37.      */  
  38.     public void addPage(View page) {  
  39.         addPage(page, -1);  
  40.     }  
  41.       
  42.     /** 
  43.      * 添加一个页面。 
  44.      * @param page 
  45.      */  
  46.     public void addPage(View page, int index) {  
  47.         if(!flag) {  
  48.             mContainer = (LinearLayout) getChildAt(0);  
  49.             flag = true;  
  50.         }  
  51.         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mScreenWidth, mScreenHeight);  
  52.         if(index == -1) {  
  53.             mContainer.addView(page, params);  
  54.         } else {  
  55.             mContainer.addView(page, index, params);  
  56.         }  
  57.         mPageCount++;  
  58.     }  
  59.       
  60.     /** 
  61.      * 移除一个页面。 
  62.      * @param index 
  63.      */  
  64.     public void removePage(int index) {  
  65.         if(mPageCount < 1) {  
  66.             return;  
  67.         }  
  68.         if(index<0 || index>mPageCount-1) {  
  69.             return;  
  70.         }  
  71.         mContainer.removeViewAt(index);  
  72.         mPageCount--;  
  73.     }  
  74.       
  75.     /** 
  76.      * 移除所有的页面 
  77.      */  
  78.     public void removeAllPages() {  
  79.         if(mPageCount > 0) {  
  80.             mContainer.removeAllViews();  
  81.         }  
  82.     }  
  83.       
  84.     /** 
  85.      * 获取页面数量 
  86.      * @return 
  87.      */  
  88.     public int getPageCount() {  
  89.         return mPageCount;  
  90.     }  
  91.       
  92.     /** 
  93.      * 获取相对滑动位置。由右向左滑动,返回正值;由左向右滑动,返回负值。 
  94.      * @return 
  95.      */  
  96.     private int getBaseScrollX() {  
  97.         return getScrollX() - mBaseScrollX;  
  98.     }  
  99.       
  100.     /** 
  101.      * 使相对于基线移动x距离。 
  102.      * @param x x为正值时右移;为负值时左移。 
  103.      */  
  104.     private void baseSmoothScrollTo(int x) {  
  105.         smoothScrollTo(x + mBaseScrollX, 0);  
  106.     }  
  107.   
  108.     @Override  
  109.     public boolean onTouchEvent(MotionEvent ev) {  
  110.         int action = ev.getAction();  
  111.         switch (action) {  
  112.         case MotionEvent.ACTION_UP:  
  113.             int scrollX = getBaseScrollX();  
  114.             //左滑,大于一半,移到下一页  
  115.             if (scrollX > mScrollX) {  
  116.                 baseSmoothScrollTo(mScreenWidth);  
  117.                 mBaseScrollX += mScreenWidth;  
  118.             }   
  119.             //左滑,不到一半,返回原位  
  120.             else if (scrollX > 0) {  
  121.                 baseSmoothScrollTo(0);  
  122.             }   
  123.             //右滑,不到一半,返回原位  
  124.             else if(scrollX > -mScrollX) {  
  125.                 baseSmoothScrollTo(0);  
  126.             }   
  127.             //右滑,大于一半,移到下一页  
  128.             else {  
  129.                 baseSmoothScrollTo(-mScreenWidth);  
  130.                 mBaseScrollX -= mScreenWidth;  
  131.             }  
  132.             return true;  
  133.         }  
  134.         return super.onTouchEvent(ev);  
  135.     }  
  136. }  


接下来是布局,fragment_main.xml:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout    
  2.     xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="fill_parent"    
  5.     >  
  6.     <!-- pageview里面必须有LinearLayout,这个写死了。 -->  
  7.     <com.example.testandrid.PageView   
  8.         android:id="@+id/pageview"  
  9.         android:layout_width="wrap_content"    
  10.         android:layout_height="fill_parent"    
  11.         android:scrollbars="none" >    
  12.             <LinearLayout  
  13.                 android:layout_width="wrap_content"    
  14.                 android:layout_height="fill_parent"    
  15.                 android:orientation="horizontal" >  
  16.             </LinearLayout>  
  17.     </com.example.testandrid.PageView>   
  18. </LinearLayout>  

最后在Activity里面调用就行。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.testandrid;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.widget.LinearLayout;  
  8.   
  9. public class MainActivity extends Activity {  
  10.     private LayoutInflater inflater;  
  11.     private PageView mPageView;  
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.fragment_main);  
  17.           
  18.         inflater = LayoutInflater.from(this);  
  19.         mPageView = (PageView) findViewById(R.id.pageview);  
  20.           
  21.         //增加几个页面  
  22.         LinearLayout layout = new LinearLayout(this);  
  23.         layout.setBackgroundColor(Color.BLUE);  
  24.         mPageView.addPage(layout);  
  25.           
  26.         LinearLayout layout2 = new LinearLayout(this);  
  27.         layout2.setBackgroundColor(Color.YELLOW);  
  28.         mPageView.addPage(layout2);  
  29.           
  30.         //这里就是个普通的xml布局文件  
  31.         LinearLayout view = (LinearLayout) inflater.inflate(R.layout.page1, null);  
  32.         mPageView.addPage(view);  
  33.           
  34.         //删除一个页面  
  35. //      mPageView.removePage(1);  
  36.     }  
  37. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值