如何在android中触发swiperrefreshlayout? [英] How to trigger swiperefreshlayout in android?

查看:53
本文介绍了如何在android中触发swiperrefreshlayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 MainFragment 的事件 onCreateView 中触发我的 SwipeRefreshLayout.我想要做的是开始下载事件 onCreateView 中的信息,同时我显示 SwipeRefreshLayout 刷新.有什么想法吗?

I want to trigger my SwipeRefreshLayout in the event onCreateView of my MainFragment. What I want to do is start downloading the information in the event onCreateView and at the same time I show the SwipeRefreshLayout refreshing. Any ideas?

这是我在 MainFragment

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        SwipeRefreshLayout swpRfrshLyt= (SwipeRefreshLayout) rootView.findViewById(R.id.swpRfrshLyt);

        swpRfrshLyt.setColorSchemeResources(R.color.holo_blue_light,
                R.color.holo_green_light,
                R.color.holo_orange_light,
                R.color.holo_red_light);

        swpRfrshLyt.setOnRefreshListener(this);
        swpRfrshLyt.setRefreshing(true);
        return rootView;
    }

这是我的fragment_main.xml

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

    <ListView
        android:id="@+id/lstvw_items"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

我正在使用 Android Studio,这是我的构建 gradle

I am using Android Studio, here is my build gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:support-v4:+'
    compile 'com.mcxiaoke.volley:library:1.0.+'
}

我已经安装了Android SDK Build Tools 21.0.2

推荐答案

有一个更好的解决方案可以使用以下方法来实现:

There is a better solution to achieve this by using the following:

mSwipeRefreshLayout.post(new Runnable() {
    @Override public void run() {
        mSwipeRefreshLayout.setRefreshing(true);
    }
});

这里的 mSwipeRefreshLayout 是 SwipeRefreshLayout.如果你只是调用:

Here mSwipeRefreshLayout is the SwipeRefreshLayout. If you simply call:

 mSwipeRefreshLayout.setRefreshing(true);

它不会触发圆圈动画,所以通过添加上面的行,你只是在 UI 线程中延迟,以便它在 UI 线程内显示圆圈动画.

it wont trigger the circle to animate, so by adding the above line you just make a delay in the UI thread so that it shows the circle animation inside the UI thread.

通过调用 mSwipeRefreshLayout.setRefreshing(true) OnRefreshListener 也将被执行,所以在任务结束后只需添加 mSwipeRefreshLayout.setRefreshing(false) 以停止圆形动画.

By calling mSwipeRefreshLayout.setRefreshing(true) the OnRefreshListener will also get executed so after the task end just add mSwipeRefreshLayout.setRefreshing(false) to stop the circle animation.

这篇关于如何在android中触发swiperrefreshlayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆