關於ScrollView中嵌套listview焦點滑動問題 解決


(第三種,第四種簡單推薦使用)

在這里我要提出的是,listview能滾動的前提是:當listview本身的高度小於listview里的子view。

第一種方法

只需在MainActivity中 找到listview 和 scrollview

然后給listview設置監聽事件

復制代碼
listView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(event.getAction() == MotionEvent.ACTION_UP){ scrollView.requestDisallowInterceptTouchEvent(false); }else{ scrollView.requestDisallowInterceptTouchEvent(true); } return false; } });
復制代碼

第二種方法

只需重寫listview即可

復制代碼
package com.bawei.day06;

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

public class ListViewForScrollView extends ListView { int mLastMotionY; boolean bottomFlag; public ListViewForScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub  } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (bottomFlag) { getParent().requestDisallowInterceptTouchEvent(true); } return super.onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub int y = (int) ev.getRawY(); switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mLastMotionY = y; break; case MotionEvent.ACTION_MOVE: int deltaY = y - mLastMotionY; if (deltaY < 0) { View child = getChildAt(0); if (child != null) { if (getLastVisiblePosition() == (getChildCount()-1) && child.getBottom() == (getChildCount()-1)) { bottomFlag = true; getParent().requestDisallowInterceptTouchEvent(true); } int bottom = child.getBottom(); int padding = getPaddingTop(); if (getLastVisiblePosition() == (getChildCount()-1) && Math.abs(bottom - padding) >= 20) { bottomFlag = true; getParent().requestDisallowInterceptTouchEvent(true); } } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: break; } return super.onTouchEvent(ev); } public void setBottomFlag(boolean flag) { bottomFlag = flag; } }
復制代碼

 

第三種方法

只需重寫listview即可

復制代碼
package com.bawei.day06;

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

public class ListViewForScrollView extends ListView { int mLastMotionY; boolean bottomFlag; public ListViewForScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub  }  @Override public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub getParent().requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); } }
復制代碼

 

第四種方法

只需在MainActivity中 找到listview 

然后給listview設置監聽事件

復制代碼
listView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub listView.getParent().requestDisallowInterceptTouchEvent(true); return false; } });
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM