xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Second">
<com.yifei.myapplication.MyGroupView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/a"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/b"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/c"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/d"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/e"
/>
</com.yifei.myapplication.MyGroupView>
</LinearLayout>
写一个类继承ViewGroup
package com.yifei.myapplication;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;
public class MyGroupView extends ViewGroup {
private int mscreenHeight;
private Scroller scroller;
private int lastY;
private int start;
private int end;
public MyGroupView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);//初始化
}
private void initView(Context context) {
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(dm);//获得
mscreenHeight = dm.heightPixels;//得到高度
scroller = new Scroller(context);//初始化
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int count = getChildCount();//获取所有子控件 的长度
//遍历每一个子控件
for (int i = 0; i < count; i++) {
View childView = getChildAt(i);//获取到每一个子控件measureChild(childView,widthMeasureSpec,heightMeasureSpec);//测量子控件
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastY = y;
start = getScrollY();
break;
case MotionEvent.ACTION_MOVE:
if(!scroller.isFinished()){
scroller.abortAnimation();
}
int dy = lastY-y;
if(getScaleY()<0){
dy =0;
}
if(getScaleY()>getHeight()-mscreenHeight){
dy = 0;
}
scrollBy(0,dy);
lastY =y;
break;
case MotionEvent.ACTION_UP:
end = getScrollY();
int dscrolly = end -start;
if(dscrolly>0){
if(dscrolly<mscreenHeight/3){
scroller.startScroll(0,getScrollY(),0,-dscrolly);
}else {
scroller.startScroll(0,getScrollY(),0,mscreenHeight-dscrolly);
}
}else {
if(-dscrolly<mscreenHeight/3){
scroller.startScroll(0,getScrollY(),0,-dscrolly);
}else {
scroller.startScroll(0,getScrollY(),0,-mscreenHeight);
}
}
break;
default:
break;
}
postInvalidate();
return true;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int clildCount = getChildCount();
// 设置ViewGroup的高度
MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
mlp.height = mscreenHeight * clildCount;
setLayoutParams(mlp);
for (int i = 0; i < clildCount; i++) {
View clild = getChildAt(i);
if (clild.getVisibility() != View.GONE) {//
clild.layout(1, i * mscreenHeight, r, (i + 1) * mscreenHeight);
}
}
}
@Override
public void computeScroll() {
super.computeScroll();
if(scroller.computeScrollOffset()){
scrollTo(0,scroller.getCurrY());
}
postInvalidate();
}
}