【问题标题】:ListView and FrameLayout in ScrollView [BUG]ScrollView 中的 ListView 和 FrameLayout [BUG]
【发布时间】:2015-08-22 14:42:19
【问题描述】:

我有一个主布局,包括 2 个布局,一个包含一个 FrameLayout,里面有一个 Fragment。另一个带有包含 ListView 的 LinearLayout。这就是我的主要布局:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <include layout="@layout/map_layout" android:layout_height="wrap_content"  android:layout_width="fill_parent" android:layout_weight="1"></include>
            <include layout="@layout/list_layout" android:layout_height="wrap_content"  android:layout_width="fill_parent" android:layout_weight="6"></include>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

第一个 Fragment 包含一个地图,第二个包含一个带有自己的 listAdapter 的自定义 ListView。

所以这是我的问题,我知道将 ListView 放在 ScrollView 中有点棘手。但是在我的 ListFragment.java 的 onCreateView 中我放了这个:

        list.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

但 ListView 不断抓住滚动。一会儿,ScrollView的bug,把地图和文件列表放了一个无限的ScrollView。

我该怎么做才能让它发挥作用?

【问题讨论】:

  • 您正在尝试做一些平台不支持的事情,这是一个很好的限制(IT DEFO 不是 BUG)并且它不起作用。你的问题是什么?
  • 我该怎么做才能让它工作?
  • 您不要将 ScrollView 放在 ListVIew 中,平台不支持。请尝试使用 CoordinatorLayout。
  • 它是一个 ScrollView 内的 ListView。但我会看看如何使用 CoordinatorLayout。
  • ListView inside ScrollVIew 或 ScrollView inside ListView 没有区别。它们都不支持嵌套滚动,并且嵌套滚动不适用于它们。

标签: java android listview


【解决方案1】:

我认为你可以使用这样的东西!

yourListView.setOnTouchListener(new ListView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        // Disallow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        break;

                    case MotionEvent.ACTION_UP:
                        // Allow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }

                // Handle ListView touch events.
                v.onTouchEvent(event);
                return true;
            }
        });

【讨论】:

【解决方案2】:

我终于找到了我的问题的解决方案。

我需要扩展我的 listView,所以它只能由 ScrollView 滚动。这是我找到的代码:

Helper.java:

public class Helper {
    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);
    }
}

要实现它,你只需要这一行:

Helper.getListViewSize(myList);

我在这里找到了这个解决方案:http://www.androidhub4you.com/2012/12/listview-into-scrollview-in-android.html

【讨论】:

    猜你喜欢
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    相关资源
    最近更新 更多