【Android】Scrollview嵌套EditText,使其滑动

 <ScrollView
        android:id="@+id/sv_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fadingEdge="none"
        android:scrollbars="none" >

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >


<cn.ls.widget.ScrollviewEdit
                android:id="@+id/sv_feedback"
                android:layout_width="fill_parent"
                android:layout_height="100dip"
                android:layout_gravity="center"
                android:fadingEdge="none"
                android:scrollbars="none"
                android:visibility="visible" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="100dip"
                    android:gravity="center"
                    android:scrollbars="vertical" >

                    <EditText
                        android:id="@+id/et_feedback"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginLeft="13dip"
                        android:layout_marginRight="13dip"
                        android:clickable="true"
                        android:enabled="false"
                        android:focusable="false"
                        android:gravity="top"
                        android:scrollbars="vertical"
                        android:singleLine="false"
                        android:background="@null"
                        android:text="" />

                   
                </LinearLayout>
            </cn.ls.widget.ScrollviewEdit>

  </LinearLayout>
    </ScrollView>

这里是自定义的Scrollview,当然你也可以使用原生的。

因为我的整个页面是在一个Scrollview中,所以这又涉及到Scrollview嵌套Scrollview的问题,所以需要屏蔽父级的Scrollview.

public class ScrollviewEdit extends ScrollView {

private static final String TAG = "ScrollviewEdit";   
private ScrollView parent_scrollview; 
public ScrollView getParent_scrollview() {
return parent_scrollview;
}
public void setParent_scrollview(ScrollView parent_scrollview) {
this.parent_scrollview = parent_scrollview;
}

public ScrollviewEdit(Context context) {
super(context);
}

public ScrollviewEdit(Context context, AttributeSet attrs) {
super(context, attrs);
}

int currentY;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
LogUtils.i(TAG, "onInterceptTouchEvent--------");    
if (parent_scrollview == null) {
return super.onInterceptTouchEvent(ev);
} else {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
// 将父scrollview的滚动事件拦截
currentY = (int) ev.getY();
setParentScrollAble(false);
LogUtils.i(TAG, "将父scrollview的滚动事件拦截-----");    
return super.onInterceptTouchEvent(ev);  
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
// 把滚动事件恢复给父Scrollview
setParentScrollAble(true);
LogUtils.i(TAG, "把滚动事件恢复给父Scrollview-----");  
} else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
}
}
return super.onInterceptTouchEvent(ev);  
}
/**
 * 是否把滚动事件交给父scrollview
 * @param flag
 */
private void setParentScrollAble(boolean flag) {
parent_scrollview.requestDisallowInterceptTouchEvent(!flag);
}
}


其中private ScrollView parent_scrollview; 代表传递过来的父级Scrollview.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫叶思念

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值