带有多个 ListView 的 SwipeRefreshLayout

mak*_*sim 5 android listview android-layout swiperefreshlayout

我有一个非常复杂的结构,其中FrameLayout包含 2 个LinearLayout(一次只显示一个)。

其中一个LinearLayout需要下拉刷新。

所以我的布局 XML 看起来像这样:

<FrameLayout>
    [...]
    <SwipeRefreshLayout>
        <LinearLayout>
            <TextView/>
            <TextView/>
            <ListView/>
            <TextView/>
            <ListView/>
        </LinearLayout>
    </SwipeRefreshLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

我希望能够在整体上进行下拉刷新Linearlayout,这就是为什么我的想法是用SwipeRefreshLayout. 但是TextView现在拉动s 不起作用,有时行为真的很奇怪(我看到气泡但它立即消失了)。

只有当我拉上其中一个时ListView,事情才能按预期工作。

我什至尝试导入MultiSwiperefreshLayout(从这里)和设置:

swipeLayout.setSwipeableChildren(R.id.xxx); // Id of my linearlayout
Run Code Online (Sandbox Code Playgroud)

您知道如何制作Refreshlayout像我这样的复杂布局并在那里正常工作吗?

小智 0

我解决了\xef\xbc\x81\n我遇到了同样的问题,并解决了它。\n我的问题如下:

\n\n
<MultiSwipeRefreshLayout>\n    <LinearLayout>\n        <RelativeLayout>\n            <ListView  @+id/list_1 />\n            <TextView  @+id/list_1_empty_view />\n        </RelativeLayout>\n\n        <RelativeLayout>\n            <ListView  @+id/list_2 />\n            <TextView  @+id/list_2_empty_view />\n        </RelativeLayout>\n    </LinearLayout>\n</MultiSwipeRefreshLayout>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我解决了它参考:https ://developer.android.com/samples/SwipeRefreshMultipleViews/index.html

\n\n

在这个例子的思想背后,我重写了 SwipeRefreshLayout,但略有不同。

\n\n

1)首先,我新建了一个MultiSwipeRefreshLayout.java类,因为我的修改,除了扩展SwipeRefreshLayout类之外,我还必须这样做;

\n\n

2)其次,我将SwipeRefreshLayout.java的所有内容复制到MultiSwipeRefreshLayout.java,并添加以下代码:

\n\n
private View[] mSwipeableChildren;\n\n/**\n * \xe8\xae\xb0\xe5\xbd\x95\xe5\xad\x90\xe8\xa7\x86\xe5\x9b\xbe\xe4\xb8\xad\xe6\x98\xaf\xe5\x90\xa6\xe6\x9c\x89\xe6\x8e\xa7\xe4\xbb\xb6\xe5\x8f\xaf\xe4\xbb\xa5\xe4\xb8\x8a\xe6\xbb\x91\xe7\x9a\x84\xef\xbc\x9b\xe8\xaf\xa5\xe5\x80\xbc\xe5\x9c\xa8\xe6\xaf\x8f\xe6\xac\xa1\xe7\x9a\x84ACTION_DOWN\xe6\x97\xb6\xe8\xae\xa1\xe7\xae\x97\xe4\xb8\x80\xe6\xac\xa1\xe3\x80\x82\n */\nprivate boolean canChildScrollUp = true;\n/**\n * Set the children which can trigger a refresh by swiping down when they are visible. These\n * views need to be a descendant of this view.\n */\npublic void setSwipeableChildren(final int... ids) {\n    assert ids != null;\n\n    // Iterate through the ids and find the Views\n    mSwipeableChildren = new View[ids.length];\n    for (int i = 0; i < ids.length; i++) {\n        mSwipeableChildren[i] = findViewById(ids[i]);\n    }\n}\n/**\n * This method controls when the swipe-to-refresh gesture is triggered. By returning false here\n * we are signifying that the view is in a state where a refresh gesture can start.\n *\n * <p>As {@link android.support.v4.widget.SwipeRefreshLayout} only supports one direct child by\n * default, we need to manually iterate through our swipeable children to see if any are in a\n * state to trigger the gesture. If so we return false to start the gesture.\n */\npublic boolean canChildScrollUp(MotionEvent ev) {\n    if (mSwipeableChildren != null && mSwipeableChildren.length > 0\n            && ev.getActionMasked() == MotionEvent.ACTION_DOWN) {\n        // Iterate through the scrollable children and check if any of them can not scroll up\n        // \xe6\x89\xbe\xe5\x88\xb0\xe9\x82\xa3\xe4\xb8\xaa\xe8\x83\xbd\xe6\x8e\xa5\xe5\x8f\x97\xe5\x88\xb0 ev \xe7\x9a\x84\xe6\x8e\xa7\xe4\xbb\xb6\n        View target = null;\n        for (View view : mSwipeableChildren) {\n            if (view != null && view.isShown() && isInViewRect(ev, view)) {\n                target = view;\n            }\n        }\n\n        // \xe5\xbd\x93\xe5\x89\x8d\xe6\x8e\xa5\xe5\x8f\x97\xe6\xbb\x91\xe5\x8a\xa8\xe4\xba\x8b\xe4\xbb\xb6\xe7\x9a\x84ListView\xe6\x98\xaf\xe5\x90\xa6\xe5\x8f\xaf\xe4\xbb\xa5\xe5\x90\x91\xe4\xb8\x8a\xe6\xbb\x91\xe5\x8a\xa8\n        if (target != null && canViewScrollUp(target) ) {\n            canChildScrollUp = true;\n        }else{\n            canChildScrollUp = false;\n        }\n    }\n    return canChildScrollUp;\n}\n\n/**\n * \xe5\x88\xa4\xe6\x96\xad\xe7\x82\xb9\xe5\x87\xbb\xe4\xba\x8b\xe4\xbb\xb6\xe6\x98\xaf\xe5\x90\xa6\xe5\x9c\xa8\xe7\x9b\xae\xe6\xa0\x87\xe6\x8e\xa7\xe4\xbb\xb6\xe5\x86\x85\n * @param ev\n * @param view\n * @return\n */\nprivate static boolean isInViewRect(MotionEvent ev, View view){\n    //\xe8\xae\xa1\xe7\xae\x97View\xe7\x9a\x84\xe5\x9d\x90\xe6\xa0\x87\n    int location[] = new int[2];\n    view.getLocationOnScreen(location);\n    RectF viewRectF = new RectF(\n            location[0], location[1],\n            location[0]+view.getWidth(), location[1] + view.getHeight()\n    );\n\n    float xOnScreen = ev.getRawX();\n    float yOnScreen = ev.getRawY();\n\n    return viewRectF.contains(xOnScreen, yOnScreen);\n}\n\n/**\n * Utility method to check whether a {@link View} can scroll up from it\'s current position.\n * Handles platform version differences, providing backwards compatible functionality where\n * needed.\n */\nprivate static boolean canViewScrollUp(View view) {\n    if (android.os.Build.VERSION.SDK_INT >= 14) {\n        // For ICS and above we can call canScrollVertically() to determine this\n        return ViewCompat.canScrollVertically(view, -1);\n    } else {\n        if (view instanceof AbsListView) {\n            // Pre-ICS we need to manually check the first visible item and the child view\'s top\n            // value\n            final AbsListView listView = (AbsListView) view;\n            return listView.getChildCount() > 0 &&\n                    (listView.getFirstVisiblePosition() > 0\n                            || listView.getChildAt(0).getTop() < listView.getPaddingTop());\n        } else {\n            // For all other view types we just check the getScrollY() value\n            return view.getScrollY() > 0;\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

更改原始方法:canChildScrollUp() ->> canChildScrollUp(MotionEvent ev),\n并添加一个字段:canChildScrollUp。其余部分与上面参考中的谷歌示例相同。

\n\n

好的,然后我们可以添加多个ListView或ScrollView或TextView或其他小部件。我的意思是我们可以在一个SwipeRefreshLayout中包含多个ListView。

\n\n

我是中国人,所以请原谅我的英语水平。

\n