自定义滚的CustomScrollView
来源:互联网 发布:织带打版软件 编辑:程序博客网 时间:2024/05/01 20:16
1、常见的滚动视图控件
Android中常用的滚动控件有ScrollView、ListView、GridView、ExpandListView等。
2、视图滚动原理
在android中,视图模式是层叠卡片式的,但又和现实中的层叠卡片有些区别。例如,把屏幕当然一张卡片,每一个view控件都是直接或者是间接地层叠在屏幕之上的,如下面这张图。
screen处于卡片的底部,container叠在screen之上,在container这张卡片中,又贴有childview这张卡片。
如上示例图,container的明显比childview要小,如果完全按物理显示的话,childview就将与container同一个等级的视图卡片挡住。
在android中,子view的显示大小,由其所在的父view决定。
因此,虽然childview比container大小要大很多,但是在屏幕中也只求显示container大小的部分,超过container大小的部分,已经被系统遮盖住了。
这时候,为了将childview被遮盖的内容显示出来,视图滚动就因此而生了。
我们可以想像这样的一个情景:把childview一点一点的往上挪动,这时候,childview被遮盖住的内容是不是就可以显示出来了?
答案是肯定的。
android中的滚动,正是基于此原理而形成的。
实现滚动的例子
布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000" android:orientation="vertical" > <com.example.customscroller.CustomScrollerView android:id="@+id/container" android:layout_width="match_parent" android:layout_height="300dp" android:background="#0000FF" android:orientation="vertical" > <LinearLayout android:id="@+id/customscrollview" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#123456" android:gravity="center" android:text="001" android:textColor="#FFFFFF" android:textSize="40sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFF" /> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#123456" android:gravity="center" android:text="002" android:textColor="#FFFFFF" android:textSize="40sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFF" /> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#123456" android:gravity="center" android:text="003" android:textColor="#FFFFFF" android:textSize="40sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFF" /> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#123456" android:gravity="center" android:text="004" android:textColor="#FFFFFF" android:textSize="40sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFF" /> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#123456" android:gravity="center" android:text="005" android:textColor="#FFFFFF" android:textSize="40sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFF" /> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#123456" android:gravity="center" android:text="006" android:textColor="#FFFFFF" android:textSize="40sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFF" /> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#123456" android:gravity="center" android:text="007" android:textColor="#FFFFFF" android:textSize="40sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFF" /> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#123456" android:gravity="center" android:text="008" android:textColor="#FFFFFF" android:textSize="40sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFF" /> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#123456" android:gravity="center" android:text="009" android:textColor="#FFFFFF" android:textSize="40sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFF" /> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#123456" android:gravity="center" android:text="010" android:textColor="#FFFFFF" android:textSize="40sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFFFFF" /> </LinearLayout> </com.example.customscroller.CustomScrollerView> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#987654" android:gravity="center" android:text="区外" android:textColor="#FFFFFF" android:textSize="40sp" /></LinearLayout>
CustomScrollerView.java
package com.example.customscroller;import android.annotation.SuppressLint;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.LinearLayout;/*** @author winway-laohw* @time 2017年11月21日 下午2:29:26*/public class CustomScrollerView extends LinearLayout { public CustomScrollerView(Context context) { super(context); } @SuppressLint("NewApi") public CustomScrollerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); // TODO Auto-generated constructor stub } public CustomScrollerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public CustomScrollerView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } private View childview; private MarginLayoutParams params; private float scrollY = 0; private float downY = 0; int maxHeight = 0; @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (null == childview) { childview = getChildAt(0); maxHeight = childview.getHeight(); } if (ev.getAction() == MotionEvent.ACTION_DOWN) { downY = ev.getY(); } if (ev.getAction() == MotionEvent.ACTION_MOVE) { if (null == params) { params = (MarginLayoutParams) childview.getLayoutParams(); } float move = ev.getY() - downY; params.topMargin = (int) (scrollY + move); childview.setLayoutParams(params); } if (ev.getAction() == MotionEvent.ACTION_UP) { scrollY = scrollY + ev.getY() - downY; } return true; }}
MainActivity.java
package com.example.customscroller;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
阅读全文
0 0
- 自定义滚的CustomScrollView
- iOS实战-自定义的横向滚动控件CustomScrollView
- cocos2dx自定义的滑动滚条
- 自定义控件--数字的滚轮式增加
- android CustomScrollView上下左右同时滑动
- 滚屏 asp.net 使用自定义控件实现滚屏
- 前滚和回滚的区别
- 前滚和回滚的区别
- 前滚和回滚的区别 .
- EJB3.0 中事务的处理自定义异常类并回滚
- 数据库移植中怎样让自定义的移植操作支持回滚
- hibernate的回滚
- 回滚的研究
- 滚粗的HNOI2015
- svn的回滚
- Basic MSI如何添加自定义回滚?
- Android 仿 iPhone 自定义滚条视图
- 嵌套事务的回滚
- 【Scikit-Learn 中文文档】使用 scikit-learn 介绍机器学习 | ApacheCN
- linux 文件系统构建之初步了解yaffs
- 自定义注解实现日志管理
- 面向对象
- 踩了阿里云播放器的一个坑
- 自定义滚的CustomScrollView
- JavaFX API详解之Window,Stage,PopupWindow
- JAVA深复制(深克隆)与浅复制(浅克隆)
- 加减器(自定义view)
- 【鸟哥的Linux私房菜】Linux的压缩与打包
- spring注解源码分析-解析和注入注解配置的资源
- Celery-4.1 用户指南: Monitoring and Management Guide
- 从日志到双十一大屏只要一步:LOG/SLS+DataV 打通
- fastDFS图片服务器的一些常见错误