1. 程式人生 > >Android中scrollview巢狀webview的實現

Android中scrollview巢狀webview的實現

Android中,預設情況下,scrollview中如果再巢狀一個webview,會出現webview無法展示的以及這個scrollview無法滑動的問題,因為scrollview需要根據內部的內容計算高度,webview也需要更具內部的內容計算高度,而且他們都是可以滑動的,就存在滑動的衝突,然而,現實情況下,總是就會存在這樣的需求,需要去這樣子去巢狀來完成需求。怎麼辦?當然是,解決衝突,巢狀使用,實現需求呀。

其實實現起來,也是相當的簡單,這和scrollview巢狀listview是完全類似的原因和操作,包括現象都一樣啊,那麼就擼程式碼:

/**
 * @author jakezhang
 * @date
2016年4月26日 */
public class NoScrollWebView extends WebView { @SuppressLint("NewApi") public NoScrollWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public NoScrollWebView(Context context, AttributeSet attrs, int
defStyleAttr) { super(context, attrs, defStyleAttr); } public NoScrollWebView(Context context, AttributeSet attrs) { super(context, attrs); } public NoScrollWebView(Context context) { super(context); } @Override protected void onMeasure(int widthMeasureSpec, int
heightMeasureSpec) { int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, mExpandSpec); } }

xml中:

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
                    <com.freedev.tool.view.NoScrollWebView
                        android:id="@+id/webViewNet"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="15dp" />
            </LinearLayout>
        </ScrollView>

在activity中:

webView.loadData(url, "text/html; charset=UTF-8", "UTF-8");

這樣一來,就可以了,scrollview中可以巢狀webview,也可以加入其它的東西一起在這個scrollview中滑動了。