我找到了一些答案
SwipeRefreshLayout refresh animation doesn't stop
加上其他一些问答,但直到现在我还没有找到我的解决方案,即使我无法理解这个问题。
实际上这个类是pagerAdapter
我的代码是
@Override
public Object instantiateItem(ViewGroup container, int position) {
View listView = LayoutInflater.from(container.getContext()).inflate(R.layout.fragment_common_list, container, false);
swipeView = (SwipeRefreshLayout) listView.findViewById(R.id.nfs);
swipeView.setColorSchemeResources(R.color.colorPrimary,
R.color.colorAccent,
R.color.yes,
R.color.no);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
Log.d("SwipeView","Refrehing");
swipeView.setRefreshing(false);
Log.d("SwipeView ",swipeView.isRefreshing()+"");
}
}, 3000);
}
});
}
和我的fragment_common_list视图是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/nfs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/tab_height">
<ListView
android:id="@+id/commonList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
解:
代替:
swipeView = (SwipeRefreshLayout) listView.findViewById(R.id.nfs);
写:
swipeView = (SwipeRefreshLayout) findViewById(R.id.nfs);
如果它是Activity
,请在onCreate();
中声明
如果它在Fragment
中,那么在onCreateView()
中用视图对象声明它。
尝试一下,希望它有所帮助。
注意:这篇文章即将理解这个问题,所以我不希望得到任何投票上/下,我会删除一旦问题澄清。
如下所示更改您的代码,如果您仍然遇到相同问题,请告诉我们。
swipeView.setRefreshing(false);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeView.setRefreshing(true);
loadItems();
}
});
private void loadItems() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeView.setRefreshing(false);
}
}, 3000);
}