赞
踩
参考网址:https://blog.csdn.net/wanghao200906/article/details/51084975
- package com.view;
-
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.widget.ListView;
-
- public class ListViewForScrollView extends ListView {
- public ListViewForScrollView(Context context) {
- super(context);
- }
- public ListViewForScrollView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public ListViewForScrollView(Context context, AttributeSet attrs,
- int defStyle) {
- super(context, attrs, defStyle);
- }
- @Override
- /**
- * 重写该方法,达到使ListView适应ScrollView的效果
- */
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
- MeasureSpec.AT_MOST);
- super.onMeasure(widthMeasureSpec, expandSpec);
- }
-
- // @Override
- // public boolean dispatchTouchEvent(MotionEvent ev) {
- // getParent().requestDisallowInterceptTouchEvent(true);
- // return super.dispatchTouchEvent(ev);
- // }
- }
- package com.view;
-
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.widget.ListView;
-
-
- public class MyListView extends ListView {
-
- private static final String TAG = "MyListView";
- public MyListView(Context context) {
- this(context, null);
- }
-
- public MyListView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
-
- public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
-
-
- @Override
- public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-
- int defaultsize=measureHight(Integer.MAX_VALUE >> 2, heightMeasureSpec);
- int expandSpec = MeasureSpec.makeMeasureSpec(defaultsize, MeasureSpec.AT_MOST);
-
- super.onMeasure(widthMeasureSpec, expandSpec);
- }
-
-
- private int measureHight(int size, int measureSpec) {
- int result = 0;
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
- if (specMode == MeasureSpec.EXACTLY) {
- Log.i(TAG, "exactly" );
-
- result = specSize;
- } else {
-
- result = size;//最小值是200px ,自己设定
- if (specMode == MeasureSpec.AT_MOST) {
- result = Math.min(result, specSize);
- }
- Log.i(TAG, "specMode:"+specMode+"--result:"+result );
- }
- return result;
- }
-
-
- /**
- * 改listview滑到底端了
- *
- * @return
- */
- public boolean isBottom() {
- int firstVisibleItem = getFirstVisiblePosition();//屏幕上显示的第一条是list中的第几条
- int childcount = getChildCount();//屏幕上显示多少条item
- int totalItemCount = getCount();//一共有多少条
- if ((firstVisibleItem + childcount) >=totalItemCount) {
- return true;
- }
- return false;
- }
-
- /**
- * 改listview在顶端
- *
- * @return
- */
- public boolean isTop() {
- int firstVisibleItem = getFirstVisiblePosition();
- if (firstVisibleItem ==0) {
- return true;
- }
- return false;
- }
-
- float down = 0;
- float y;
-
- @Override
- public boolean dispatchTouchEvent(MotionEvent event) {
-
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- down = event.getRawY();
-
- getParent().requestDisallowInterceptTouchEvent(true);
- break;
- case MotionEvent.ACTION_MOVE:
- y = event.getRawY();
- if (isTop()) {
- if (y - down > 1) {
- // 到顶端,向下滑动 把事件教给父类
- getParent().requestDisallowInterceptTouchEvent(false);
- } else {
- // 到顶端,向上滑动 把事件拦截 由自己处理
- getParent().requestDisallowInterceptTouchEvent(true);
- }
- }
-
- if (isBottom()) {
- if (y - down > 1) {
- // 到底端,向下滑动 把事件拦截 由自己处理
- getParent().requestDisallowInterceptTouchEvent(true);
- } else {
- // 到底端,向上滑动 把事件教给父类
- getParent().requestDisallowInterceptTouchEvent(false);
- }
- }
- break;
- default:
- break;
- }
-
- return super.dispatchTouchEvent(event);
- }
- }
-
禁止ListView条目点击(在Adapter中重写以下方法)
- @Override
- public boolean isEnabled(int position) {
- return false;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。