我找到了一些答案,比如
轻扫刷新布局刷新动画不会停止
加上一些其他的问答,但直到现在我还没有找到我的解决方案,甚至我也无法理解这个问题。
实际上该类是pageAdapter
我的代码是
@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
中,则用 view 对象在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);
}