android ScrollView 中嵌套Scrollview 怎么办?

本文介绍如何处理在Android中ScrollView内嵌套ScrollView时,子ScrollView监听并处理父ScrollView的触摸事件,实现当子ScrollView滑动到顶部时,父ScrollView可以继续滚动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[java]  view plain  copy
 print ?
  1. public class ChildScrollview extends ScrollView {      
  2.     private ScrollView F_scrollview; 
  3.    int current_Y; 
  4.     public ChildScrollview(Context context) {  
            super(context);  
        }  
  5.     public ChildScrollview(Context context, AttributeSet attrs) {  
  6.         super(context, attrs);  
  7.     }   
  8.     @Override  
  9.     public boolean onInterceptTouchEvent(MotionEvent ev) {      
  10.         if (F_scrollview == null) {  
  11.             return super.onInterceptTouchEvent(ev);  
  12.         } else {  
  13.             if (ev.getAction() == MotionEvent.ACTION_DOWN) {  
  14.                 // 将父scrollview的滚动事件拦截  
  15.                 current_Y= (int) ev.getY();  
  16.                 setFatherScrollAble(false);    
  17.                 return super.onInterceptTouchEvent(ev);    
  18.             } else if (ev.getAction() == MotionEvent.ACTION_UP) {  
  19.                 // 把滚动事件恢复给父Scrollview  
  20.                 setFatherScrollAble(true);    
  21.             } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {  
  22.             }  
  23.         }  
  24.         return super.onInterceptTouchEvent(ev);    
  25.     }  
  26.     /** 
  27.      * 是否把滚动事件交给父scrollview 
  28.      */  
  29.     private void setFatherScrollAble(boolean flag) {  
  30.         F_scrollview.requestDisallowInterceptTouchEvent(!flag);  
  31.     } 
     
  32.     public void setF_Scrollview(Scrollview f_view){
  33. this.F_scrollview=f_view;
  34.     }
  35.     public Scrollview  getF_Scrollview(){
  36.     return this.F_scrollview;
  37.     }
  38. }  
[html]  view plain  copy
 print ?
  1.  <ScrollView  
  2.        android:id="@+id/scrollview_f"  
  3.        android:layout_width="fill_parent"  
  4.        android:layout_height="fill_parent"  
  5.        android:fadingEdge="none"  
  6.        android:scrollbars="none" >  
  7.   
  8. <LinearLayout  
  9.            android:layout_width="fill_parent"  
  10.            android:layout_height="fill_parent"  
  11.            android:orientation="vertical" >  
  12.   
  13.   
  14. <com.example.view.ChildScrollview  
  15.                android:id="@+id/scrollview_c"  
  16.                android:layout_width="fill_parent"  
  17.                android:layout_height="80dp"  
  18.                android:layout_gravity="center"  
  19.                android:fadingEdge="none"  
  20.                android:scrollbars="none"  
  21.                android:visibility="visible" >  
  22.   
  23.                <LinearLayout  
  24.                    android:layout_width="fill_parent"  
  25.                    android:layout_height="80dp"  
  26.                    android:gravity="center"  
  27.                    android:scrollbars="vertical" >  
  28.   
  29.                    <TextView  
  30.                        android:id="@+id/tv_test"  
  31.                        android:layout_width="fill_parent"  
  32.                        android:layout_height="wrap_content"  
  33.                        android:layout_gravity="center"  
  34.                        android:gravity="top"  
  35.                        android:scrollbars="vertical"  
  36.                        android:singleLine="false"  
  37.                        android:background="@null"  
  38.                        android:text="Test\nTest\nTest\nTest\nTest\nTest\nTest\nTest\nTest\nTest\nTest\nTest\nTest\nTest\nTest\nTest\nTest\nTest\n" />  
  39.   
  40.                     
  41.                </LinearLayout>  
  42.            </com.example.view.ChildScrollview >  
  43.   
  44.     </LinearLayout>  
  45.    </ScrollView>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值