HorizontalScrollView实现多页左右滑动
来源:互联网 发布:word mac 章节目录 编辑:程序博客网 时间:2024/06/05 20:00
先上图看看效果:
上代码:
PageView是封装后的一个类,继承了HorizontalScrollView。
- package com.miquan;
- 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.LinearLayout;
- /**
- * 具体看博客:http://blog.csdn.net/qiantujava/article/details/42392127
- */
- public class PageView extends HorizontalScrollView {
- private int mBaseScrollX;//滑动基线。也就是点击并滑动之前的x值,以此值计算相对滑动距离。
- private int mScreenWidth;
- private int mScreenHeight;
- private LinearLayout mContainer;
- private boolean flag;
- private int mPageCount;//页面数量
- private int mScrollX = 200;//滑动多长距离翻页
- public PageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- DisplayMetrics dm = context.getApplicationContext().getResources()
- .getDisplayMetrics();
- mScreenWidth = dm.widthPixels;
- mScreenHeight = dm.heightPixels;
- }
- /**
- * 添加一个页面到最后。
- * @param page
- */
- public void addPage(View page) {
- addPage(page, -1);
- }
- /**
- * 添加一个页面。
- * @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++;
- }
- /**
- * 移除一个页面。
- * @param index
- */
- public void removePage(int index) {
- if(mPageCount < 1) {
- return;
- }
- if(index<0 || index>mPageCount-1) {
- return;
- }
- mContainer.removeViewAt(index);
- mPageCount--;
- }
- /**
- * 移除所有的页面
- */
- public void removeAllPages() {
- if(mPageCount > 0) {
- mContainer.removeAllViews();
- }
- }
- /**
- * 获取页面数量
- * @return
- */
- public int getPageCount() {
- return mPageCount;
- }
- /**
- * 获取相对滑动位置。由右向左滑动,返回正值;由左向右滑动,返回负值。
- * @return
- */
- private int getBaseScrollX() {
- return getScrollX() - mBaseScrollX;
- }
- /**
- * 使相对于基线移动x距离。
- * @param x x为正值时右移;为负值时左移。
- */
- private void baseSmoothScrollTo(int x) {
- smoothScrollTo(x + mBaseScrollX, 0);
- }
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- int action = ev.getAction();
- switch (action) {
- case MotionEvent.ACTION_UP:
- int scrollX = getBaseScrollX();
- //左滑,大于一半,移到下一页
- if (scrollX > mScrollX) {
- baseSmoothScrollTo(mScreenWidth);
- mBaseScrollX += mScreenWidth;
- }
- //左滑,不到一半,返回原位
- else if (scrollX > 0) {
- baseSmoothScrollTo(0);
- }
- //右滑,不到一半,返回原位
- else if(scrollX > -mScrollX) {
- baseSmoothScrollTo(0);
- }
- //右滑,大于一半,移到下一页
- else {
- baseSmoothScrollTo(-mScreenWidth);
- mBaseScrollX -= mScreenWidth;
- }
- return true;
- }
- return super.onTouchEvent(ev);
- }
- }
接下来是布局,fragment_main.xml:
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <!-- pageview里面必须有LinearLayout,这个写死了。 -->
- <com.example.testandrid.PageView
- android:id="@+id/pageview"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:scrollbars="none" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:orientation="horizontal" >
- </LinearLayout>
- </com.example.testandrid.PageView>
- </LinearLayout>
最后在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;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.fragment_main);
- inflater = LayoutInflater.from(this);
- mPageView = (PageView) findViewById(R.id.pageview);
- //增加几个页面
- LinearLayout layout = new LinearLayout(this);
- layout.setBackgroundColor(Color.BLUE);
- mPageView.addPage(layout);
- LinearLayout layout2 = new LinearLayout(this);
- layout2.setBackgroundColor(Color.YELLOW);
- mPageView.addPage(layout2);
- //这里就是个普通的xml布局文件
- LinearLayout view = (LinearLayout) inflater.inflate(R.layout.page1, null);
- mPageView.addPage(view);
- //删除一个页面
- // mPageView.removePage(1);
- }
- }
0 0
- HorizontalScrollView实现多页左右滑动
- HorizontalScrollView实现多页左右滑动
- HorizontalScrollView实现多页左右滑动
- Android 实例讲解HorizontalScrollView实现左右滑动
- 用HorizontalScrollView实现listview的左右滑动
- Android HorizontalScrollView左右滑动
- 实现android左右滑动效果:ViewFlipper、ViewPager、HorizontalScrollView
- RadioButton组也可通过添加上HorizontalScrollView 实现左右滑动
- RadioButton组也可通过添加上HorizontalScrollView 实现左右滑动
- HorizontalScrollView包裹GridView左右滑动但是GridView前面需要有一个imageview怎么实现
- HorizontalScrollView 嵌套listview实现列表左右、上下滑动、控件复用等功能
- HorizontalScrollView实现listView 横向滑动
- 结合HorizontalScrollView横向滑动实现多列项ListView
- ViewPager+Fragment实现左右滑动页
- android实现多界面左右滑动切换
- viewpager 实现左右滑动
- android 实现左右滑动
- 左右滑动---viewflipper实现
- RAID技术详解
- Mobiscroll的介绍【一款兼容PC和移动设备的滑动插件】
- Ubuntu下编译 ardupilot
- 窗口机制
- hdu 1171 Big Event in HDU
- HorizontalScrollView实现多页左右滑动
- Solr查询参数QueryParam
- jquery $(document).ready() 与window.onload的区别
- PHP开发环境配置
- 基于HIVE数据库出报表的后台相关工具介绍
- SSL/TLS 加密新纪元 - Let's Encrypt
- RAID 磁盘阵列 详解
- Python3.x那些事儿:[27]更多List操作
- 关于 Android 5.0 无法安装应用问题