问题描述
我想在我的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,这是我的构建包
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中触发swiperefreshlayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!