ScrollView 嵌套WebView 的問題優化


一.布局樣式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/graywhite"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_pb"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone" >
<ProgressBar
android:layout_width="20dp"
android:layout_height="20dp" />
</LinearLayout>

<RelativeLayout
android:id="@+id/jg_rl_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/position3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@color/black" >
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/everyday_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:actualImageScaleType="centerCrop" />
<LinearLayout
android:id="@+id/ll_knowledge_head_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/reading_detail"
android:gravity="center"
android:orientation="horizontal" >

<TextView
android:id="@+id/everyday_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:textColor="@color/white"
android:textSize="@dimen/font_14" />

</LinearLayout>
<com.ldcang.customview.MyTxVideo
android:id="@+id/my_tx_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
</com.ldcang.customview.MyTxVideo>
<TextView
android:id="@+id/tv_middle_play"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/play_start"
android:layout_centerInParent="true"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_centerInParent="true"
android:visibility="gone"/>
</RelativeLayout>

<WebView
android:id="@+id/everyday_webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>

</LinearLayout>
<com.ldcang.utils.Listview
android:id="@+id/everyday_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/graywhite"
android:divider="@null">
</com.ldcang.utils.Listview>
<LinearLayout
android:id="@+id/ll_goods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center_vertical"
android:text="猜你喜歡"
android:textColor="@color/text333"
android:textSize="@dimen/font_14"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/graywhite"/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">

<LinearLayout
android:id="@+id/ll_collect_image_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:orientation="horizontal" />
</HorizontalScrollView>

</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/position3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_alignParentBottom="true"
android:orientation="vertical">

<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/line" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="43dp"
android:orientation="horizontal">

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:id="@+id/zm_tv_zan"
android:layout_width="19dp"
android:layout_height="19dp"
android:background="@drawable/liulan_list" />

<TextView
android:id="@+id/everyday_views"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="0"
android:textColor="@color/text666"
android:textSize="@dimen/font_14" />
</LinearLayout>

<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@color/line" />

<LinearLayout
android:id="@+id/ll_collect"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">

<ImageView
android:id="@+id/iv_collect"
android:layout_width="19dp"
android:layout_height="19dp"
android:background="@drawable/guanzhu" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="收藏"
android:textColor="@color/text666"
android:textSize="@dimen/font_14" />
</LinearLayout>

<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@color/line" />

<LinearLayout
android:id="@+id/everyday_share"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:id="@+id/zm_tv_share"
android:layout_width="17dp"
android:layout_height="17dp"
android:background="@drawable/jg_share" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="分享"
android:textColor="@color/text666"
android:textSize="@dimen/font_14" />
</LinearLayout>

<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@color/line" />

<LinearLayout
android:id="@+id/everyday_pinglun"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:id="@+id/textView3"
android:layout_width="18dp"
android:layout_height="18dp"
android:background="@drawable/comment_icon" />

<TextView
android:id="@+id/everyday_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="評論"
android:textColor="@color/text666"
android:textSize="@dimen/font_14" />
</LinearLayout>

</LinearLayout>
</LinearLayout>

<TextView
android:id="@+id/everyday_back"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/attention_back" />
</RelativeLayout>
</LinearLayout>

這個我布局我手機沒問題就沒注意,以為沒有問題,后面部分手機會有很大的空白區域,滑動控件沖突了。以后遇到兩個滑動控件嵌套都會有問題,必須自定義一個滑動控件,我認為最好是自定義嵌套的內部滑動控件,感覺比較簡單。

二。自定義WebView
public class MyWebView extends WebView {
public MyWebView(Context context) {
super(context);
}

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

public MyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

主要復寫onMeasure()方法 重新計算一下高度即可,然后在修改完的時候發現會滾動到WebView的位置
繼續修改貼上部分代碼
public class EveryDayDetailActivity extends BaseActivity implements OnClickListener {
@Override
protected void initView() {
// TODO Auto-generated method stub
setContentView(R.layout.everyday_detail);
jg_rl_parent = (RelativeLayout) findViewById(R.id.jg_rl_parent);
ll_pb = (LinearLayout) findViewById(R.id.ll_pb);
back = (TextView) findViewById(R.id.everyday_back);//返回

listView = (Listview) findViewById(R.id.everyday_listview);

/** 有視頻則顯示視頻播放 */

webView = (WebView) findViewById(R.id.everyday_webview);
title = (TextView) findViewById(R.id.everyday_title);
imageView = (SimpleDraweeView) findViewById(R.id.everyday_background);
/**
* 改為自定義控件自動滾動到了WebView,添加焦點不讓滾動到WebView
* */
imageView.setFocusable(true);
imageView.requestFocus();
imageView.setFocusableInTouchMode(true);

ll_knowledge_head_bottom = (LinearLayout) findViewById(R.id.ll_knowledge_head_bottom);
mMyTxVideo = (MyTxVideo) findViewById(R.id.my_tx_video);//視頻空間
tv_middle_play = (TextView) findViewById(R.id.tv_middle_play);//播放按鈕控件
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);//加載轉圈

popshare = getLayoutInflater().inflate(R.layout.popupwindow_share, null);//分享彈框布局

/** 商品和拍賣 */
ll_goods = (LinearLayout)findViewById(R.id.ll_goods);//商品和拍賣父控件
ll_collect_image_container = (LinearLayout)findViewById(R.id.ll_collect_image_container);//商品和拍賣控件

/** 底部 */
views = (TextView) findViewById(R.id.everyday_views);
share = (LinearLayout) findViewById(R.id.everyday_share);
pinglun = (LinearLayout) findViewById(R.id.everyday_pinglun);
everyday_comment = (TextView) findViewById(R.id.everyday_comment);
iv_collect = (ImageView) findViewById(R.id.iv_collect);
ll_collect = (LinearLayout) findViewById(R.id.ll_collect);
}

}
  把焦點給上面的任何一個控件就可以了
    /**
* 改為自定義控件自動滾動到了WebView,添加焦點不讓滾動到WebView
* */
imageView.setFocusable(true);
imageView.requestFocus();
imageView.setFocusableInTouchMode(true);
 
        




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM