自定义HorizontalScrollView视图实现仿ViewPager效果
来源:互联网 发布:龙门镖局知乎 编辑:程序博客网 时间:2024/05/20 04:13
先展示效果图:
自定义HorizontalScrollView类:AppHorizontalScrollView
package com.eg.lyx.demo;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;/*** * 应用详情页截图 自定义HorizontalScrollView视图 ( 仿ViewPager效果) * */public class AppHorizontalScrollView extends HorizontalScrollView { /** * 数据定义 */ private int subChildCount = 0; private ViewGroup firstChild = null; private int downX = 0; private int currentPage = 0; private ArrayList<Integer> viewList = new ArrayList<Integer>(); /** * 构造方法 */ public AppHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public AppHorizontalScrollView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public AppHorizontalScrollView(Context context) { super(context); init(); } private void init() { setHorizontalScrollBarEnabled(false);//设置原有的滚动无效 } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); getChildInfo(); } /** * 获取子视图信息 */ public void getChildInfo() { firstChild = (ViewGroup) getChildAt(0); if (firstChild != null) { subChildCount = firstChild.getChildCount(); for (int i = 0; i < subChildCount; i++) { if (((View) firstChild.getChildAt(i)).getWidth() > 0) { viewList.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(viewList.get(currentPage)-10, 0); } /** * 滑动到下一页 */ private void smoothScrollToNextPage() { if (currentPage < subChildCount - 1) { currentPage++; smoothScrollTo(viewList.get(currentPage)-10, 0); } } /** * 滑动到上一页 */ private void smoothScrollToPrePage() { if (currentPage > 0) { currentPage--; smoothScrollTo(viewList.get(currentPage)-10, 0); } } /** * 滑动到下一页 * @author caizhiming */ public void nextPage() { smoothScrollToNextPage(); } /** * 滑动到上一页 */ public void prePage() { smoothScrollToPrePage(); } /** * 跳转到指定的页面 */ public boolean gotoPage(int page) { if (page > 0 && page < subChildCount - 1) { smoothScrollTo(viewList.get(page), 0); currentPage = page; return true; } return false; }}
布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.eg.lyx.demo.MainActivity"> <com.eg.lyx.demo.AppHorizontalScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" android:scrollbars="none" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <include layout="@layout/item_one"/> <include layout="@layout/item_one"/> <include layout="@layout/item_one"/> <include layout="@layout/item_one"/> </LinearLayout> </com.eg.lyx.demo.AppHorizontalScrollView></RelativeLayout>
item_one:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="360dp" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_marginTop="20dp" android:background="@drawable/bg" android:orientation="vertical" android:paddingLeft="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="景点玩乐" android:textColor="#99000000" android:textStyle="bold"/> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginBottom="20dp" android:layout_marginTop="20dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:scaleType="fitXY" android:src="@drawable/aa"/> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="6dp" android:layout_weight="1" android:gravity="center_vertical" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:gravity="center_vertical" android:text="POI名称" android:textSize="13sp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginBottom="4dp" android:layout_marginTop="4dp" android:layout_weight="1" android:gravity="center_vertical" android:orientation="horizontal" android:visibility="visible"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4.9分" android:textColor="#e69e0e" android:textSize="11sp"/> <View android:layout_width="1px" android:layout_height="match_parent" android:layout_marginBottom="2dp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_marginTop="2dp" android:background="#000000" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="距离市中心10.2km" android:textSize="11sp"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:gravity="center_vertical" android:text="234起" android:textColor="#e69e0e" android:textSize="11sp"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:layout_marginRight="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#1c57c4" android:text="预订" android:textColor="#ffffff"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginBottom="20dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:scaleType="fitXY" android:src="@drawable/aa"/> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="6dp" android:layout_weight="1" android:gravity="center_vertical" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:gravity="center_vertical" android:text="POI名称" android:textSize="13sp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginBottom="4dp" android:layout_marginTop="4dp" android:layout_weight="1" android:gravity="center_vertical" android:orientation="horizontal" android:visibility="visible"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4.9分" android:textColor="#e69e0e" android:textSize="11sp"/> <View android:layout_width="1px" android:layout_height="match_parent" android:layout_marginBottom="2dp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_marginTop="2dp" android:background="#000000" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="距离市中心10.2km" android:textSize="11sp"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:gravity="center_vertical" android:text="234起" android:textColor="#e69e0e" android:textSize="11sp"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:layout_marginRight="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#1c57c4" android:text="预订" android:textColor="#ffffff"/> </LinearLayout> </LinearLayout></LinearLayout>
阅读全文
1 0
- 自定义HorizontalScrollView视图实现仿ViewPager效果
- HorizontalScrollView+viewpager实现仿天天动听ios版和QQ侧边菜单效果
- android中HorizontalScrollView实现viewpager的效果
- 自定义tab,viewpager实现仿tablayout切换效果
- Android 实现横向标题栏滚动效果(HorizontalScrollView + GridView + Viewpager + 自定义适配器)
- Android 自定义 HorizontalScrollView 实现ViewPager效果,打造再多图片(控件)也不怕 OOM
- ViewPager使用2-ViewPager+顶部HorizontalScrollView+Fragment-仿GooglePlay 顶部可滑动效果
- 仿zaker最新版本引导界面的视图联动效果(修改viewpager实现)
- 实现android左右滑动效果:ViewFlipper、ViewPager、HorizontalScrollView
- HorizontalScrollView和ViewPager联动效果
- 自定义ViewPager、HorizontalScrollView、Indicator实现可滑动的头部
- 自定义 viewpager, 实现竖直viewpager效果
- 仿网易新闻APP(五)——无限横向滑动菜单(自定义HorizontalScrollView+ViewPager)
- 自定义HorizontalScrollView模仿RadioGroup+Fragment+Viewpager的翻页与点击效果,效果完全一样
- 仿ViewPager相册(使用HorizontalScrollView)
- 自定义控件:仿ViewPager实现左右滑动
- ViewPager和Fragment结合,利用(HorizontalScrollView)实现指示器与ViewPager同时滑动的动态效果
- 用ListView+自定义的HorizontalScrollView实现滑动删除效果
- 12345
- 异常(捕获异常)
- C. Beautiful Set----枚举
- L2-020. 功夫传人
- Chrome最新版(53-55)再次爆出BUG!
- 自定义HorizontalScrollView视图实现仿ViewPager效果
- Vue学习笔记
- 异常(抛出异常:throws)
- File
- HDU
- Redis配置文件之redis.windows.conf
- Android Studio为Class跟Activity等添加注解
- IO流(字节输出流)
- 【深入理解Java虚拟机读书笔记】垃圾收集器与内存分配策略