我在我的活動(我它們之間進行瀏覽使用片段交易)兩個片段:Android的碎片兩次
- 科目
- 主題詳細名單 - 名單等級
主題片段佈局:
<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);
它正常工作,但不是在這種情況下:
- 我拉來刷新數據
- 我瀏覽到主題細節(使用 碎片交易)
- setRefreshing方法在 碎片交易期間被調用
在這種情況下,主題片段只是掛在那裏,兩個片段都在活動中。
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);
}
我setRefreshing(false)
上refreshLayout和如何避免讓受試者片段兩次?
你可以張貼烏爾第一片段的onCreate()? –
@PhanDinhThai我更新了這個問題。 – cuddlecheek