【问题标题】:Android WebView inside ListView should zoom and panListView 内的 Android WebView 应该缩放和平移
【发布时间】:2014-09-24 14:51:47
【问题描述】:

所以我正在制作一个看起来像这样的布局:

|----------------|
|                |
|    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>

到目前为止,一切都很好。它显示 web 视图,但平移和缩放效果不佳。它的“迟钝”。在放下手势之前,您可以平移或缩放非常小的距离,并且缩放/平移会停止。您需要一遍又一遍地捏合,以实现较大的缩放级别。

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

实现这一点的正确方法是什么,我的 web 视图可以缩放但仍会随着列表元素滚动?

【问题讨论】:

    标签: android android-layout listview webview scrollview


    【解决方案1】:

    试试这个:

    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>
    

    主要活动:

        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);
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 2015-03-15
      • 1970-01-01
      相关资源
      最近更新 更多