1. 程式人生 > >Android控制元件-ScrollView 和WebView之見滑動衝突解決

Android控制元件-ScrollView 和WebView之見滑動衝突解決

   需求:

              最近在做一個webView載入網頁的頁面,最外層是一個scrollView,因為還有標題等其他資料是需要單獨獲取載入,所以scrollview中是包含一個其他資訊的頭部佈局和一個載入網頁資訊的WebView,當滑動的時候,頭部和WebView一起滑動。

   出現bug

               只有WebView可以滑動。

  bug解決思路

              重寫父控制元件scrollView,攔截WebView的onTouch事件

  自定義scrollview程式碼定義:

<span style="font-size:18px;">package dianshi.matchtrader.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * Created by Administrator on 2016/5/20 0020.
 */
public class MyScrollView extends ScrollView{

    private GestureDetector mGestureDetector;

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //初始化手勢
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
    }

    /**
     * touch事件的攔截函式
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //根據手勢決定是否攔截子控制元件的onTouch事件
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }

    /**
     * 控制元件的手勢監聽
     */
    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            //當縱向滑動的距離大於橫向滑動的距離的時候,返回true
            if (Math.abs(distanceY) > Math.abs(distanceX)) {
                return true;
            }
            return false;
        }
    }
}</span>

  佈局中使用:
<span style="font-size:18px;">    <dianshi.matchtrader.view.MyScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/scrollView">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#f0f0f0"
                android:padding="20dp"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/txt_noticeDetail_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textStyle="bold"
                    android:textSize="22sp"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginTop="10dp">
                    <TextView
                        android:id="@+id/txt_noticeDetail_dayTime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#a3a3a3"
                        android:textSize="16sp"/>
                    <TextView
                        android:id="@+id/txt_noticeDetail_hourTime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/lightGray"
                        android:textSize="16sp"
                        android:layout_marginLeft="20dp"
                        />

                </LinearLayout>





            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:padding="5dp">

                <WebView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/txt_noticeDetail_content"
                    android:layout_margin="0dp"
                    android:padding="0dp"
                    android:layout_gravity="bottom"
                    />

            </LinearLayout>


        </LinearLayout>

    </dianshi.matchtrader.view.MyScrollView></span>

注意事項:

      使用的時候 scrollview不要新增android:fillViewport="true"屬性,否則沒有效果,原因暫時還不知道具體是為什麼。

      百度到的android:fillViewport="true"的作用是:

         當ScrollView沒有fillViewport=“true”時, 裡面的元素(比如LinearLayout)會按照wrap_content來計算(不論它是否設了"fill_parent"),而如果LinearLayout的元素設定了fill_parent,那麼也是不管用的,因為LinearLayout依賴裡面的元素,而裡面的元素又依賴LinearLayout,這樣自相矛盾.所以裡面元素設定了fill_parent,也會當做wrap_content來計算。

     但是好像和touch事件並沒有什麼直接關係,很費解,以後有時間再詳細研究一下。