ListView中的Android WebView应该缩放和平移

时间:2014-09-24 14:51:48

标签: android android-layout listview webview scrollview

所以我做了一个看起来像这样的布局:

|----------------|
|                |
|    WebView     |
|                |
|----------------|
|  List element  |
|----------------|
|  List element  |
|----------------|
|  List element  |
|----------------|
|  List element  |
|----------------|

我想让我的WebView与List元素一起向上滚动,所以我创建了一个ListView,其中第一个元素是webview。主要布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/overview_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

WebView元素布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <WebView
        android:id="@+id/overview_cell_webview"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
</LinearLayout>

到目前为止,这么好。它显示了webview,但平移和缩放不能很好地工作。它的迟缓&#34;。在手势掉落之前,您可以平移或缩放非常小的距离,并且缩放/平移停止。你需要一遍又一遍地捏,以达到很大的缩放级别。

我试图主要使用requestDisallowInterceptTouchEvent()来解决这个问题,但到目前为止还没有成功。我应该在webview上设置它吗?包含webview的LinearLayout?或完全不同的东西?

实现这一目标的正确方法是什么,我的webview是可缩放的,但仍然与列表元素一起滚动?

1 个答案:

答案 0 :(得分:0)

试试这个:

main.xml中

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollView"
    android:layout_gravity="center_horizontal" >

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

        <WebView
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:id="@+id/webView"
            android:layout_weight="0" />

        <ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/listView"
            android:layout_weight="1" />

    </LinearLayout>

</ScrollView>

mainactivity:

    public class Test extends Activity {

        static String[] month ={
                "January", "February", "March", "April",
                "May", "June", "July", "August",
                "September", "October", "November", "December"};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test);
            ListView listView = (ListView)findViewById(R.id.listView);
            listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, month));
            getListViewSize(listView);
        }

        public static void getListViewSize(ListView myListView) {
            ListAdapter myListAdapter = myListView.getAdapter();
            if (myListAdapter == null) {
                //do nothing return null
                return;
            }
            //set listAdapter in loop for getting final size
            int totalHeight = 0;
            for (int size = 0; size < myListAdapter.getCount(); size++) {
                View listItem = myListAdapter.getView(size, null, myListView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            //setting listview item in adapter
            ViewGroup.LayoutParams params = myListView.getLayoutParams();
            params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
            myListView.setLayoutParams(params);
        }

    }