如果ListView中嵌套HorizontalScrollView怎么解决滑动错乱
2个回答
2014-12-23 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:25897
获赞数:1464984
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。
向TA提问 私信TA
关注
展开全部
最近在开发中遇到HorizontalScrollView嵌套ScrollView导致滑动卡的情况,急横向水平滑动的View和垂直水平滑动的View,都在接收处理滑动时间,但是这种情况下触摸事件就会发生冲突。导致滑动非常卡,甚至出现程序停止响应。这种情况下我们需要重写view。下面是两个简单的例子,重写水平滑动View只接收水平方向上滑动的事件。我们使用手势GestureDetector来作区分。
<em>import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
public class CustomHScrollView extends HorizontalScrollView{
private GestureDetector mGestureDetector;
private View.OnTouchListener mGestureListener;
private static final String TAG = "CustomHScrollView";
/**
* @function CustomHScrollView constructor
* @param context Interface to global information about an application environment.
*
*/
public CustomHScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(new HScrollDetector());
setFadingEdgeLength(0);
}
/**
* @function CustomHScrollView constructor
* @param context Interface to global information about an application environment.
* @param attrs A collection of attributes, as found associated with a tag in an XML document.
*/
public CustomHScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(new HScrollDetector());
setFadingEdgeLength(0);
}
/**
* @function CustomHScrollView constructor
* @param context Interface to global information about an application environment.
* @param attrs A collection of attributes, as found associated with a tag in an XML document.
* @param defStyle style of view
*/
public CustomHScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(new HScrollDetector());
setFadingEdgeLength(0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
}
// Return false if we're scrolling in the y direction
class HScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if(Math.abs(distanceX) > Math.abs(distanceY)) {
return true;
}
return false;
}
}
} </em>
<em>import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
public class CustomHScrollView extends HorizontalScrollView{
private GestureDetector mGestureDetector;
private View.OnTouchListener mGestureListener;
private static final String TAG = "CustomHScrollView";
/**
* @function CustomHScrollView constructor
* @param context Interface to global information about an application environment.
*
*/
public CustomHScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(new HScrollDetector());
setFadingEdgeLength(0);
}
/**
* @function CustomHScrollView constructor
* @param context Interface to global information about an application environment.
* @param attrs A collection of attributes, as found associated with a tag in an XML document.
*/
public CustomHScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(new HScrollDetector());
setFadingEdgeLength(0);
}
/**
* @function CustomHScrollView constructor
* @param context Interface to global information about an application environment.
* @param attrs A collection of attributes, as found associated with a tag in an XML document.
* @param defStyle style of view
*/
public CustomHScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(new HScrollDetector());
setFadingEdgeLength(0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
}
// Return false if we're scrolling in the y direction
class HScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if(Math.abs(distanceX) > Math.abs(distanceY)) {
return true;
}
return false;
}
}
} </em>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询