屏幕冻结 - 滚动视图 android 中的列表视图

发布于 2024-11-16 17:02:28 字数 1439 浏览 6 评论 0原文

我阅读了几篇博客\so\论坛帖子,发现将 ListView 放入 ScrollView 中显然存在问题。但在某个地方,我发现如果我在滚动视图中放置线性布局(作为列表),它应该可以工作,现在它可以工作。下面是我的xml。 但列表不会向上\向下滚动,它被冻结了...... 知道为什么吗?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" android:fillViewport="true">
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:orientation="vertical">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:orientation="horizontal">

            <ListView android:id="@+id/listView1" android:layout_width="100dp"
                android:layout_height="wrap_content" android:background="@color/white"
                android:cacheColorHint="#00000000" />
            <HorizontalScrollView android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ListView android:id="@+id/listView2" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:background="@color/white"
                    android:cacheColorHint="#00000000" />
            </HorizontalScrollView>
        </LinearLayout>

    </LinearLayout>
</ScrollView>

i read several blogs\so\forum posts and see that there is clearly an issue with putting a ListView in a ScrollView. But somewhere i found that it should work if i put a linear layout(which as the list) in the scrollview and now it will work. below is my xml.
but the lists do not scroll up\down, its frozen...
any idea why?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" android:fillViewport="true">
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:orientation="vertical">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:orientation="horizontal">

            <ListView android:id="@+id/listView1" android:layout_width="100dp"
                android:layout_height="wrap_content" android:background="@color/white"
                android:cacheColorHint="#00000000" />
            <HorizontalScrollView android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ListView android:id="@+id/listView2" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:background="@color/white"
                    android:cacheColorHint="#00000000" />
            </HorizontalScrollView>
        </LinearLayout>

    </LinearLayout>
</ScrollView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陈甜 2024-11-23 17:02:28

首先ListView不应该插入到ScrollView中。将 ListView 放入 LinearLayout 中可以显示它,但 ScrollView 会吃掉所有垂直滚动事件,因此您的 ListView 永远不会收到任何触摸事件。

您可能想玩 ViewGroup.html #onInterceptTouchEvent(android.view.MotionEvent)

First of all ListView should not be inserted into the ScrollView. Putting your ListView inside the LinearLayout allows you to display it, but ScrollView eats all vertical scrolling events, so your ListView never receives any touch events.

You may want to play with ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent).

清醇 2024-11-23 17:02:28

把它们放进去

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


</LinearLayout>

put them inside

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


</LinearLayout>
雨落星ぅ辰 2024-11-23 17:02:28

我修改了 xml 布局,还修改了 onScroll 侦听器。现在工作得很好。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent"
    android:id="@+id/mainLayout" android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ListView android:id="@+id/listView1" android:layout_width="100dp"
        android:layout_height="wrap_content" android:background="@color/white"
        android:cacheColorHint="#00000000" android:smoothScrollbar="true"
        android:scrollbars="none" />
    <HorizontalScrollView android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ListView android:id="@+id/listView2" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:background="@color/white"
            android:cacheColorHint="#00000000" android:smoothScrollbar="true"
            android:scrollbarStyle="outsideOverlay" />
    </HorizontalScrollView>
</LinearLayout>

lv1.setOnScrollListener(new OnScrollListener() {
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            int index = firstVisibleItem;
            View v = view.getChildAt(0);
            int top = (null == v) ? 0 : v.getTop();
            Log.i("lv1","index:"+index+" top:"+top);
            lv1.setSelection(index);
            lv2.setSelectionFromTop(index, top);
        }

        public void onScrollStateChanged(AbsListView view, int scrollState) {
            String sState;
            switch (scrollState) {
            case OnScrollListener.SCROLL_STATE_FLING:
                sState = "Fling";
                break;
            case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                sState = "Touch Scroll";
                break;
            case OnScrollListener.SCROLL_STATE_IDLE:
                sState = "Idle";
                break;
            default:
                sState = "Unknown";
                break;
            }
                      }
    });

I modified the xml layout and also modified the onScroll listener. It works perfectly fine now.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent"
    android:id="@+id/mainLayout" android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ListView android:id="@+id/listView1" android:layout_width="100dp"
        android:layout_height="wrap_content" android:background="@color/white"
        android:cacheColorHint="#00000000" android:smoothScrollbar="true"
        android:scrollbars="none" />
    <HorizontalScrollView android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ListView android:id="@+id/listView2" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:background="@color/white"
            android:cacheColorHint="#00000000" android:smoothScrollbar="true"
            android:scrollbarStyle="outsideOverlay" />
    </HorizontalScrollView>
</LinearLayout>

lv1.setOnScrollListener(new OnScrollListener() {
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            int index = firstVisibleItem;
            View v = view.getChildAt(0);
            int top = (null == v) ? 0 : v.getTop();
            Log.i("lv1","index:"+index+" top:"+top);
            lv1.setSelection(index);
            lv2.setSelectionFromTop(index, top);
        }

        public void onScrollStateChanged(AbsListView view, int scrollState) {
            String sState;
            switch (scrollState) {
            case OnScrollListener.SCROLL_STATE_FLING:
                sState = "Fling";
                break;
            case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                sState = "Touch Scroll";
                break;
            case OnScrollListener.SCROLL_STATE_IDLE:
                sState = "Idle";
                break;
            default:
                sState = "Unknown";
                break;
            }
                      }
    });
终难愈 2024-11-23 17:02:28

如果您在 Scrollview 中使用 Listview - 滚动冻结

listview.setEnabled(false);

如果您在 Scrollview 中使用 Recyclerview - 滚动冻结

为什么屏幕冻结:Recyclerview 具有滚动行为和滚动行为你试图放入一个Scrollview[已经具有滚动行为]

所以它会混淆要跟随哪个滚动

使用此代码禁用recyclerview的滚动,以便它仅使用SCROLLVIEW滚动

yourScrollview.setNestedScrollingEnabled(false);

注意:如果您使用多个recyclerview /listviews 在同一活动中
您需要禁用所有 recyclerviews/listviews 的滚动

If you use Listview inside Scrollview - Scroll freezes

listview.setEnabled(false);

If you use Recyclerview inside Scrollview - Scroll freezes

Why screen freezes : Recyclerview has scroll behaviour & your trying to put into one Scrollview[Which already has the scroll behaviour]

So it confuses which scroll to follow

Use this code to disable scroll for recyclerview so it uses only SCROLLVIEW scrolls

yourScrollview.setNestedScrollingEnabled(false);

Note : If you use multiple recyclerviews/listviews in same activity
you need to disable scrolls for all recyclerviews/listviews

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文