SwipeRefreshLayout无法工作

6

我正在使用支持库中的SwipeRefreshListener。我在我的AsyncTaskonPreExecuteonPostExecute中调用了setRefresh。然而,没有任何变化,也没有动画。我是否遗漏了什么?我是否需要设置某些参数以使其正常工作?

2个回答

14

这对我有用,更多信息请访问:http://antonioleiva.com/swiperefreshlayout/

SwipeRefreshLayout:布局您只需使用此新布局装饰可滑动的内容(可能是整个布局)。此视图必须是可滚动的,例如 ScrollView 或 ListView。以下是一个简单的示例:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:text="@string/hello_world"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="center"/>
    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

代码 我们只需要获取布局,指定一些颜色和监听器。刷新的监听器是一个后延迟处理程序。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright, 
                android.R.color.holo_green_light, 
                android.R.color.holo_orange_light, 
                android.R.color.holo_red_light);
    }


    @Override public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                swipeLayout.setRefreshing(false);
            }
        }, 5000);
    }

我不知道SwipeRefreshLayout只能作为可滑动视图的父级容器,谢谢。 - MJ Studio

5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接