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

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

布局文件:activity_test.xml

 <?xml version="1.0" encoding="utf-8"?>
<com.example.testxinye.MyScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > </LinearLayout>
</com.example.testxinye.MyScrollView>

Activity类:TestActivity.java

 package com.example.testxinye;

 import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
/**
*
* @author xinye
*
*/
public class TestActivity extends Activity {
private LinearLayout mContainer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test); mContainer = (LinearLayout) findViewById(R.id.container); LayoutParams params = new LayoutParams(getWinWidth(), getWinHeight()); ImageView imageView1 = new ImageView(this);
imageView1.setLayoutParams(params);
imageView1.setImageResource(R.drawable.call_show_medal5);
imageView1.setScaleType(ScaleType.CENTER);
mContainer.addView(imageView1); ImageView imageView2 = new ImageView(this);
imageView2.setLayoutParams(params);
imageView2.setImageResource(R.drawable.call_show_medal1);
imageView2.setScaleType(ScaleType.CENTER);
imageView2.setBackgroundColor(Color.RED);
mContainer.addView(imageView2); ImageView imageView3 = new ImageView(this);
imageView3.setLayoutParams(params);
imageView3.setImageResource(R.drawable.call_show_medal2);
imageView3.setScaleType(ScaleType.CENTER);
imageView3.setBackgroundColor(Color.GRAY);
mContainer.addView(imageView3); ImageView imageView4 = new ImageView(this);
imageView4.setLayoutParams(params);
imageView4.setImageResource(R.drawable.call_show_medal3);
imageView4.setScaleType(ScaleType.CENTER);
imageView4.setBackgroundColor(Color.BLUE);
mContainer.addView(imageView4); ImageView imageView5 = new ImageView(this);
imageView5.setLayoutParams(params);
imageView5.setImageResource(R.drawable.call_show_medal4);
imageView5.setScaleType(ScaleType.CENTER);
imageView5.setBackgroundColor(Color.GREEN);
mContainer.addView(imageView5); } @Override
protected void onResume() {
// ((MyScrollView)mContainer.getParent()).init();
super.onResume();
} private int getWinWidth(){
DisplayMetrics dm = new DisplayMetrics();
//获取屏幕信息
getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
private int getWinHeight(){
DisplayMetrics dm = new DisplayMetrics();
//获取屏幕信息
getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.heightPixels;
}
}

重写的HorizontalScrollView:MyScrollView.java

 package com.example.testxinye;

 import java.util.ArrayList;

 import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
/**
*
* @author XINYE
*
*/
public class MyScrollView extends HorizontalScrollView {
private int subChildCount = 0;
private ViewGroup firstChild = null;
private int downX = 0;
private int currentPage = 0;
private ArrayList<Integer> pointList = new ArrayList<Integer>(); public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public MyScrollView(Context context) {
super(context);
init();
}
private void init() {
setHorizontalScrollBarEnabled(false);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
receiveChildInfo();
}
public void receiveChildInfo() { firstChild = (ViewGroup) getChildAt(0);
if(firstChild != null){
subChildCount = firstChild.getChildCount();
for(int i = 0;i < subChildCount;i++){
if(((View)firstChild.getChildAt(i)).getWidth() > 0){
pointList.add(((View)firstChild.getChildAt(i)).getLeft());
}
}
} }
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = (int) ev.getX();
break;
case MotionEvent.ACTION_MOVE:{ }break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:{
if( Math.abs((ev.getX() - downX)) > getWidth() / 4){
if(ev.getX() - downX > 0){
smoothScrollToPrePage();
}else{
smoothScrollToNextPage();
}
}else{
smoothScrollToCurrent();
}
return true;
}
}
return super.onTouchEvent(ev);
} private void smoothScrollToCurrent() {
smoothScrollTo(pointList.get(currentPage), 0);
} private void smoothScrollToNextPage() {
if(currentPage < subChildCount - 1){
currentPage++;
smoothScrollTo(pointList.get(currentPage), 0);
}
} private void smoothScrollToPrePage() {
if(currentPage > 0){
currentPage--;
smoothScrollTo(pointList.get(currentPage), 0);
}
}
/**
* 下一页
*/
public void nextPage(){
smoothScrollToNextPage();
}
/**
* 上一页
*/
public void prePage(){
smoothScrollToPrePage();
}
/**
* 跳转到指定的页面
* @param page
* @return
*/
public boolean gotoPage(int page){
if(page > 0 && page < subChildCount - 1){
smoothScrollTo(pointList.get(page), 0);
currentPage = page;
return true;
}
return false;
}
}

源代码:http://download.csdn.net/detail/wangwangheng/6537043

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

  1. Android重写HorizontalScrollView模仿ViewPager效果

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

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

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

  3. 自定义控件(视图)2期笔记09:自定义视图之继承自ViewGroup(仿ViewPager效果案例)

    1. 这里我们继承已有ViewGroup实现自定义控件,模拟出来ViewPager的效果,如下: (1)实现的效果图如下: (2)实现步骤: • 自定义view继承viewGroup • 重写onLa ...

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

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

  5. Android 使用HorizontalScrollView 实现Gallery效果

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

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

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

  7. Android控件-Fragment+ViewPager(高仿微信界面)

    什么是Fragment? Fragment是Android3.0后新增的概念,Fragment名为碎片,不过却和Activity十分相似,具有自己的生命周期,它是用来描述一些行为或一部分用户界面在一个 ...

  8. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  9. android 自定义scrollview 仿QQ空间效果 下拉伸缩顶部图片,上拉回弹 上拉滚动顶部title 颜色渐变

    首先要知道  自定义scrollview 仿QQ效果 下拉伸缩放大顶部图片 的原理是监听ontouch事件,在MotionEvent.ACTION_MOVE事件时候,使用不同倍数的系数,重置布局位置[ ...

随机推荐

  1. ie6幽灵文字及解决办法

    <!-- ie6 幽灵文字示例 --> <div style="width: 400px;"> <div style="float: lef ...

  2. 《C++程序设计教程——给予Visual Studio 2008》读书笔记1,2章

    double *p1;       //p1为指向double型的指针变量 POINT *p2;       //p2为指向POINT型(点类型)的指针变量 int (*p3)[6];     //p ...

  3. 安卓程序代写 网上程序代写[原]BluetoothSocket详解

    一. BluetoothSocket简介 1. 简介 客户端与服务端 : BluetoothSocket 和 BluetoothServerSocket 类似于Java中的套接字的 Socket 和 ...

  4. 安卓程序代写 网上程序代写[原]BluetoothDevice详解

    一. BluetoothDevice简介 1. 继承关系 public static Class BluetoothDevice extends Object implement Parcelable ...

  5. XmlnsDefinition for a Cool Namespace Mapping

    In XAML, when you want to reference a CLR type, you have to add a namespace mapping that maps the XM ...

  6. Java HashSet和HashMap源码剖析

    转自: Java HashSet和HashMap源码剖析 总体介绍 之所以把HashSet和HashMap放在一起讲解,是因为二者在Java里有着相同的实现,前者仅仅是对后者做了一层包装,也就是说Ha ...

  7. 【转】关于HTTP服务器每个客户端2个连接的限制

    http://www.cnblogs.com/lishenglyx/archive/2010/01/07/1641190.html 这两天猫在家里搞一个多线程的断点续传得C#程序,发现同时只能开2个线 ...

  8. thinkphp 伪静态 自定义后缀

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...

  9. MY_Selenium登录126邮箱,定位不到账号输入框解决办法

    转自:https://www.cnblogs.com/wuhl-89/p/7778463.html 查看元素发现id为动态,所以不选择以id定位. 使用xpath路径定位,每次获取元素都失败,最后网上 ...

  10. Linux 错误记录

    1.libmysqlclient.so.18: cannot open shared object file: No such file or directory 解决办法: [root@linux- ...