【问题标题】:How to use ChrisBanes' ActionBar-PullToRefresh library with Fragments having GridViews如何将 ChrisBanes 的 ActionBar-PullToRefresh 库与具有 GridViews 的片段一起使用
【发布时间】:2013-11-20 19:29:56
【问题描述】:

我正在尝试使用 Chris Banes 的库 Actionbar-PullToRefresh。可以找到here

我在我的应用中使用 Tabs + ViewPager + Fragments。

我面临的问题是我的片段有一个GridView,我不知道如何使用这个库来处理它。

我通读了示例代码。他说您所要做的就是将可刷新的视图包装在 PullToRefreshLayout 中,如下所示:

<uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ptr_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- Your content, here we're using a ScrollView -->

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </ScrollView>

</uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>

这适用于ListViewScrollViewGridView 等内容。但是,显然这不适用于片段(标签和视图页面)。现在,在示例代码中,他用ScrollView INSTEADPullToRefreshLayout 包装了可刷新片段。

我不能这样做,因为我的 Fragment 1(在标签 1 下)有一个 GridView。现在我无法将GridView 添加到ScrollView,因为这没有任何意义。

例如,如果我将GridView 放入ScrollView 中,如下所示,那就没有意义了:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ptr_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:scrollbarStyle="outsideInset" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFF000" >

        <!-- MY GRID VIEW -->

        <GridView
        ...
        />

    </RelativeLayout>
</ScrollView>

上面的代码有效。有点。 (我需要禁用我的 GridView 的滚动并使用 ExpandableGridView 使其正常工作......这似乎有点矫枉过正,我什至不确定这是否会工作)

如果我将 ScrollView 包装器替换为 PullToRefreshLayout 或任何其他布局,刷新不起作用。

怎么办?如何让我的布局与这个库一起工作而不用 ScrollView 围绕它?

我希望我已经足够清楚了。如果您需要更多信息,请告诉我。我尽力解释它。

谢谢!

【问题讨论】:

    标签: android android-layout gridview android-fragments pull-to-refresh


    【解决方案1】:

    我在尝试让它与我的一个选项卡中的 ListView 一起工作时遇到了类似的问题。

    我通过使用 PullToRefreshAttacher 而不是使用布局解决了我的问题。

    在控制片段的 ViewPager 的 Activity 中,在 onCreate 或 init 方法中初始化 PullToRefreshAttacher。

    mPullToRefreshAttacher = PullToRefreshAttacher.get(this);
    

    接下来创建一个公共方法,允许访问您刚刚初始化的附加程序。

    public PullToRefreshAttacher getPullToRefreshAttacher() {
        return mPullToRefreshAttacher;
    }
    

    然后在你想要刷新功能的片段中。

    mPullToRefreshAttacher = ((MainTabActivity) getActivity())
                .getPullToRefreshAttacher();
    
    mPullToRefreshAttacher.addRefreshableView(activeListView, this);
    

    除了在您的情况下,activeListView 将是对您的 GridView 而不是 ListView 的引用。

    然后确保您的片段实现 OnRefreshListener 以便您可以处理刷新。

    我还没有用 GridView 实际测试过,所以请告诉我它是否有效。

    祝你好运!

    【讨论】:

    • 哇!那解决了!我从未尝试过这样做:mPullToRefreshAttacher.addRefreshableView(myGridView, this); 我一直使用 PullToRefreshLayout 而不是 GridView。您的解决方案解决了它。非常感谢,DejanRistic :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 2014-06-16
    • 1970-01-01
    相关资源
    最近更新 更多