我的活动中有两个片段(我使用片段事务在它们之间导航):
主题片段布局:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refresher"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
swipeRefreshLayout的监听器是活动。它会触发AsyncTask,它会下载新数据,通知片段数据已更改并隐藏swipeRefreshLayout。
onPostExecute方法的问题部分:
...
try {
formatData();
} catch (JSONException e) {
e.printStackTrace();
}
View refresher = findViewById(R.id.refresher);
((SwipeRefreshLayout) refresher).setRefreshing(false);
marksFragment.notifyUpdate(activity);
subjectsFragment.notifyUpdate(activity);
它通常有效,但不是在这种情况下:
在这种情况下,主题片段只会挂在那里,两个片段都在活动中。
SubjectFragment代码:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((SwipeRefreshLayout) view.findViewById(R.id.refresher)).setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.subjects_fragment, null);
}
如何在refreshLayout上setRefreshing(false)
并避免让主题片段两次?
答案 0 :(得分:1)
我在SO上发现了一个类似的问题:
我使用appcompat-v7:23.3.0。这个答案对我有用:
这个问题似乎仍然发生在appcompat 22.1.1中。包皮 FrameLayout中的SwipeRefreshLayout为我解决了这个问题。
所以我的主题片段布局如下所示:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refresher"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
答案 1 :(得分:0)
更改您的代码,参考答案stack overflow
public class YourFragment extends Fragment {
SwipeRefreshLayout swipeLayout;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View",
"Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View",
"Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
final SwipeRefreshLayout swipeView = (SwipeRefreshLayout)view. findViewById(R.id.refresher);
swipeView.setEnabled(false);
ListView lView = (ListView) view.findViewById(R.id.your_list_view);
ArrayAdapter<String> adp = new ArrayAdapter<String>(view.getContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, values);
lView.setAdapter(adp);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeView.setRefreshing(true);
( new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
swipeView.setRefreshing(false);
}
}, 3000);
}
});
lView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == 0)
swipeView.setEnabled(true);
else
swipeView.setEnabled(false);
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.subjects_fragment, null);
}
}
答案 2 :(得分:0)
我面临同样的问题cout << ((zs & X) != 0) << endl;
cout << ((zs & Y) != 0) << endl;
cout << ((zs & Z) != 0) << endl;
cout << (zs & NUM) << endl;
两次来电。
基本上原因不在于swipelayout.setOnRefresh
当我的recyclerview到达终点时我正在加载一些数据而在swipelayout.setOnRefreshListener上我正在做同样的事情。
所以我做的是删除这个bug,我只是从swipelayout.setOnRefreshLisetener
注释掉了加载方法。
swipelayout.setOnRefreshListener
这就是我在recyclelerview中所做的事情
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if (mLatestAdapter.getItemCount() > 0) {
mLatestAdapter.removeAllItems();
mPresenter.resetPagination();
// mPresenter.loadMoreItems(false);//commentwed this line
}
}
});