ScrollView和listview的冲突问题,关于宽度,和滑动

本文介绍如何在ListView中实现下拉刷新功能,并在特定条件下阻止父级元素拦截触摸事件,确保下拉刷新的正常进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只需要重新listview即可

package com.exmple.listscrow;

import java.util.logging.LogManager;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

public class MyListView extends ListView {

	int mLastMotionY;
	boolean bottomFlag;

	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// 对height重新赋值
		heightMeasureSpec = MeasureSpec.makeMeasureSpec(
		/* Integer.MAX_VALUE>> 2 */300, MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		if (bottomFlag) {
			getParent().requestDisallowInterceptTouchEvent(true);
		}
		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		int y = (int) ev.getRawY();
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			// 妫f牕鍘涢幏锔藉焻down娴滃娆�,鐠佹澘缍峺閸ф劖鐖�
			mLastMotionY = y;
			break;
		case MotionEvent.ACTION_MOVE:
			// deltaY > 0 閺勵垰鎮滄稉瀣箥閸旓拷,< 0閺勵垰鎮滄稉濠呯箥閸旓拷
			int deltaY = y - mLastMotionY;
			if (deltaY < 0) {
				View child = getChildAt(0);
				if (child != null) {
					if (getLastVisiblePosition() == (getChildCount() - 1)
							
							) {
						bottomFlag = true;
						getParent().requestDisallowInterceptTouchEvent(true);
					}

					
				}
			}
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			break;
		}
		return super.onTouchEvent(ev);
	}


}

  注意:ScrollView只能有一个子类

转载于:https://www.cnblogs.com/jsonfan/p/5388858.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值