您的位置:首页 > 其它

ScrollView与EditText滑动冲突解决办法

2015-06-27 12:16 330 查看
在ScrollView中有EditText,当EditText部分内容被隐藏的时候,需要滑动EditText,让隐藏的内容显示出来,但是这个时候EditText滑动事件会和ScrollView的滑动事件冲突。为了解决这个问题可以自定义EditText

1.自定义EditText

public class ScrollviewEditText : EditText
{

private ScrollView parentScrollView;

public ScrollView GetParentScrollView() {

return parentScrollView;

}
public void SetParentScrollView(ScrollView parent_scrollview) {

this.parentScrollView = parent_scrollview;

}
public ScrollviewEditText(Context context, IAttributeSet attrs, int defStyle) :base(context,attrs,defStyle){

}

public ScrollviewEditText(Context context, IAttributeSet attrs):base(context,attrs) {
}

public ScrollviewEditText(Context context) :base(context){

}
public override bool OnTouchEvent (MotionEvent e)
{
if (parentScrollView == null) {
return base.OnTouchEvent(e);
} else {
if (e.Action == MotionEventActions.Down) {
// 将父scrollview的滚动事件拦截
SetParentScrollAble(false);
//将父scrollview的滚动事件拦截
return base.OnTouchEvent(e);
} else if (e.Action == MotionEventActions.Up) {
// 把滚动事件恢复给父Scrollview
SetParentScrollAble(true);
//把滚动事件恢复给父Scrollview-----
} else if (e.Action == MotionEventActions.Move) {

}
}
return base.OnTouchEvent (e);
}
//是否把滚动事件交给父scrollview
private void SetParentScrollAble(bool flag) {
parentScrollView.RequestDisallowInterceptTouchEvent (!flag);
}
}


2.主界面关键代码

private ScrollviewEditText et_content;
private ScrollView scrollview;
et_content = FindViewById<ScrollviewEditText> (Resource.Id.et_content);
scrollview = FindViewById<ScrollView> (Resource.Id.scrollview);
et_content.SetParentScrollView (scrollview);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: