我正在尝试滑动刷新。但是,一旦加载了rss feed,刷新视图就会出现一个片段。此代码不会使应用程序崩溃,但刷新视图会无限期保留,并且不会采用我提供的任何属性。
View v = getLayoutInflater().inflate(R.layout.fragment_rss, null);
SwipeRefreshLayout refresh = (SwipeRefreshLayout)
v.findViewById(R.id.swiperefresh);
refresh.setEnabled(true);
refresh.setColorSchemeColors(getResources().getColor(R.color.colorAccent),
getResources().getColor(R.color.colorPrimary),
getResources().getColor(R.color.white));
refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
{
@Override
public void onRefresh() {
new refresh().execute("");
}
});
new refresh().execute("");
调用异步动画,然后在动画完成后调用refresh方法。
private class refresh extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
return "Interrupted";
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
refreshRssFragment();
}
}
这是XML
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:visibility="gone"
android:id="@+id/newsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
这是refreshRssFragment()方法
private void refreshRssFragment(){
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
RssFragment fragment = new RssFragment();
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
refresh.setRefreshing(false);
}`