Android提供的ViewPager类太复杂,有时候没有必要使用,所以重写一个HorizontalScrollView来实现类似的效果,也可以当做Gallery来用

思路很简单,就是重写onTouchEvent事件,在手指抬起或者取消的时候,进行smoothScroll的操作,具体请看代码:

布局文件:activity_test.xml

1
2
3
4
5
6
1 <!--?xml version=1.0 encoding=utf-8?-->
 2 <com.example.testxinye.myscrollview 3="" 4="" 5="" 6="" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
 7     <linearlayout 10="" 11="" 8="" 9="" android:id="@+id/container" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="horizontal">
12        
13     </linearlayout>
14 </com.example.testxinye.myscrollview>

Activity类:TestActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 xinye
14  *
15  */
16 public class TestActivity extends Activity {
17     private LinearLayout mContainer = null;
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         // TODO Auto-generated method stub
21         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_medal2);
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_medal3);
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     @Override
69     protected void onResume() {
70 //        ((MyScrollView)mContainer.getParent()).init();
71         super.onResume();
72     }
73    
74     private int getWinWidth(){
75         DisplayMetrics dm = new DisplayMetrics();
76         //获取屏幕信息
77         getWindowManager().getDefaultDisplay().getMetrics(dm);
78         return dm.widthPixels;
79     }
80     private int getWinHeight(){
81         DisplayMetrics dm = new DisplayMetrics();
82         //获取屏幕信息
83         getWindowManager().getDefaultDisplay().getMetrics(dm);
84         return dm.heightPixels;
85     }
86 }

重写的HorizontalScrollView:MyScrollView.java

1 package com.example.testxinye;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  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 subChildCount = 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, attrs);
 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      * 下一页
104      */
105     public void nextPage(){
106         smoothScrollToNextPage();
107     }
108     /**
109      * 上一页
110      */
111     public void prePage(){
112         smoothScrollToPrePage();
113     }
114     /**
115      * 跳转到指定的页面
116      * @param page
117      * @return
118      */
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 }</integer></integer>
 

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

Android重写HorizontalScrollView模仿ViewPager效果的更多相关文章

  1. Android重写HorizontalScrollView仿ViewPager效果

    Android提供的ViewPager类太复杂,有时候没有必要使用,所以重写一个HorizontalScrollView来实现类似的效果,也可以当做Gallery来用 思路很简单,就是重写onTouc ...

  2. android自定义控件(5)-实现ViewPager效果

    对于系统的ViewGroup我们已经是十分熟悉了,最常用的LinearLayout和RelativeLayout几乎是天天要打交道,下面我们就来看看,如何一步一步将其实现: 一.首先当然也是最通常的新 ...

  3. Android 使用HorizontalScrollView 实现Gallery效果

    Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息:Gallery还可以和ImageSwitcher组件结合使用来实现一个通过缩略图来浏览图 ...

  4. Android 自定义View修炼-自定义HorizontalScrollView视图实现仿ViewPager效果

    开发过程中,需要达到 HorizontalScrollView和ViewPager的效果,于是直接重写了HorizontalScrollView来达到实现ViewPager的效果. 实际效果图如下: ...

  5. Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38140505 自从Gallery被谷歌废弃以后,Google推荐使用ViewPa ...

  6. Android实现图片轮显效果——自定义ViewPager控件

    一.问题概述 使用ViewPager控件实现可横向翻页.水平切换图片等效果,但ViewPager需要手动滑动才能切换页面,图片轮显效果的效果本质上就是在ViewPager控件的基础上让它能自动的进行切 ...

  7. Android重写ViewPager改动滑动灵敏度

        使用ViewPager作为一个页面进行切换.里面能够存放非常多View,但有时在操作View时不小心滑动一下就有可能跳到下一页,这并非我们想要的,这里就须要重写ViewPager改动它的滑动条 ...

  8. android: 结合BottomNavigationView、ViewPager和Fragment 实现左右滑动的效果

    主界面:MainActivity package com.yongdaimi.android.androidapitest; import android.os.Bundle; import andr ...

  9. Android 用HorizontalScrollView实现ListView的Item滑动删除 ,滑动错乱 冲突

    用HorizontalScrollView实现类似微信的滑动删除 测试于:Android2.2+ 对于Android来说按键操作已经在减少,越来越多的手势操作层出不穷,今天介绍一款LIstView的I ...

随机推荐

  1. Bicolored RBS CodeForces - 1167D (括号)

    建树, 然后高度最大值的最小值显然为$\lceil \frac{dep}{2}\rceil$, 将$>\frac{dep}{2}$的全部分出去即可. #include <sstream&g ...

  2. leecode刷题(31) -- 回文数

    leecode刷题(31) -- 回文数 回文数 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输 ...

  3. Design Support库中的控件

    1.NavigationView滑动菜单 2.FloatIngActionButton悬浮按钮 3.Snackbar二次交互提示的按钮 4.Coordinatorlayout,监听子控件的各种事件(加 ...

  4. Get MySQL这5个优化技巧

    一个成熟的数据库架构并不是一开始设计就具备高可用.高伸缩等特性的,它是随着用户量的增加,基础架构才逐渐完善.这篇文章主要谈谈MySQL数据库在发展周期中所面临的问题及优化方案,暂且抛开前端应用不说,大 ...

  5. Hive编程指南读书笔记(1):

    1.Mapreduce是一种计算模型,将计算任务分割成多个可以在服务器集群中并行执行的任务,然后分散到一群家用的或者服务器级别的硬件机器上,从而降低成本并提供水平可伸缩性. 2.mapreduce的两 ...

  6. cube-ui indexList的正确使用

    demo地址:https://github.com/zphtown/cube-ui-bug 上拉和下拉核心代码: onPullingDown () { this.isNoMore = false th ...

  7. Echarts常见问题汇总

    关于echarts使用的常见问题总结  来源:李文杨 关于echarts使用的问题总结1.legend图例不显示的问题:在legend中的data为一个数组项,数组项通常为一个字符串,每一项需要对应一 ...

  8. 002-loganalyzer装完报错no syslog records found

    1.登录mysql查看库Syslog中的表SystemEvents;是否有返回数据 # select * from Syslog.SystemEvents;  #又返回数据说明rsyslog配置正确, ...

  9. cmd拷贝文件夹时,处理提示

    xcopy 若目标盘上不存在此子目录,而在目标盘的结束符又不以"\"为结束,则将提示: does destination specify a file name or direct ...

  10. shell_hive

    (1)获取参数:从shell文件传来参数,调用:$1,$2,$3 load_date=$1 clearn_date=`date -d"$2 day ago $load_date" ...