ListView三层嵌套

本文介绍如何实现ListView的三层嵌套,包括自定义ScrollView、ListView及其适配器的详细步骤,通过设置布局和编写相关Java代码,最终达到ScrollView滑动时,三层ListView内容完整显示的效果。

ListView的三层嵌套,百度搜这个没有搜到结果,都是ListView嵌套ListView的例子,那就只好自己动手来试试了。三层ListView的滑动机制是ScrollView滑动,ListView的内容全部展示出来。所以ListView都要使用自定义的ListView(即:经过计算高度后的ListView)。下面来先看看效果图:



项目结构截图:



1.首先我们要来自定义一个ScrollView(阻尼效果的ScrollView,当然,你们可以用默认的也不影响。)

<span style="font-size:18px;color:#3333ff;"><strong>package com.xiaoxi.listviewthreelayersnested.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

public class CustomerScrollView extends ScrollView {

	Context mContext;
	private View mView;
	private float touchY;
	private int scrollY = 0;
	private boolean handleStop = false;
	private int eachStep = 0;

	private static final int MAX_SCROLL_HEIGHT = 300;// 最大滑动距离
	private static final float SCROLL_RATIO = 0.4f;// 阻尼系数,越小阻力就越大

	public CustomerScrollView(Context context) {
		super(context);
		this.mContext = context;
	}

	public CustomerScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
	}

	public CustomerScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
	}

	@Override
	protected void onFinishInflate() {
		if (getChildCount() > 0) {
			this.mView = getChildAt(0);
		}
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent arg0) {
		if (arg0.getAction() == MotionEvent.ACTION_DOWN) {
			touchY = arg0.getY();
		}
		return super.onInterceptTouchEvent(arg0);
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (mView == null) {
			return super.onTouchEvent(ev);
		} else {
			commonOnTouchEvent(ev);
		}
		return super.onTouchEvent(ev);
	}

	private void commonOnTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
		switch (action) {
		case MotionEvent.ACTION_UP:
			if (mView.getScrollY() != 0) {
				handleStop = true;
				animation();
			}
			break;
		case MotionEvent.ACTION_MOVE:
			float nowY = ev.getY();
			int deltaY = (int) (touchY - nowY);
			touchY = nowY;
			if (isNeedMove()) {
				int offset = mView.getScrollY();
				if (offset < MAX_SCROLL_HEIGHT && offset > -MAX_SCROLL_HEIGHT) {
					mView.scrollBy(0, (int) (deltaY * SCROLL_RATIO));
					handleStop = false;
				}
			}

			break;
		default:
			break;
		}
	}

	private boolean isNeedMove() {
		int viewHight = mView.getMeasuredHeight();
		int srollHight = getHeight();
		int offset = viewHight - srollHight;
		int scrollY = getScrollY();
		if (scrollY == 0 || scrollY == offset) {
			return true;
		}
		return false;
	}

	private void animation() {
		scrollY = mView.getScrollY();
		eachStep = scrollY / 10;
		resetPositionHandler.sendEmptyMessage(0);
	}

	Handler resetPositionHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (scrollY != 0 && handleStop) {
				scrollY -= eachStep;
				if ((eachStep < 0 && scrollY > 0) || (eachStep > 0 && scrollY < 0)) {
					scrollY = 0;
				}
				mView.scrollTo(0, scrollY);
				this.sendEmptyMessageDelayed(0, 5);
			}
		};
	};

}
</strong></span>


2.然后自定义ListView,让布局里面的三个ListView都引用这个类。

<span style="font-size:18px;color:#3333ff;"><strong>package com.xiaoxi.listviewthreelayersnested.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class CustomerListView extends ListView {
	public CustomerListView(Context context) {
        super(context);
    }
 
    public CustomerListView(Context conte
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值