在我的應用程序我使用導航選項卡使用視圖尋呼機。我已經能夠成功地繪製使用代碼標籤張貼在:
如何更新列表,每當選項卡更改視圖尋呼機
https://github.com/codepath/android_guides/wiki/Sliding-Tabs-with-PagerSlidingTabStrip
在這裏,我都用了,以獲得導航tabs.Everything工作正常的滑動標籤和查看傳呼機,但名單不能當我移動到另一個選項卡時得到更新。 OnResume()被調用,即使對象列表變量也在調試時得到更新,但視覺列表沒有得到更新。
我在這裏的代碼的一些片段:
對於TAB1:這在我的情況是活動選項卡
@Override
public void onResume() {
super.onResume();
if(!internetUtil.isConnectedToInternet(getActivity())){
mSwipeRefreshLayout.setEnabled(false);
}else{
mSwipeRefreshLayout.setEnabled(true);
new GetUsersFromServerTask().execute(); // Here i am making the network calls
}
}
在TAB2:這是存檔選項卡
@Override
public void onResume() {
super.onResume();
new GetUsersArchivedFromServerTask().execute(); // Network calls
}
在MainActivity:
public class MaterialTab extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.material_main_sample);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager()));
PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabsStrip.setViewPager(viewPager);
tabsStrip.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if(position == 0){
ActiveFragment activeFragment = new ActiveFragment();
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(activeFragment);
ft.attach(activeFragment);
ft.commit();
} if(position == 1){
ArchiveFragment archiveFragment = new ArchiveFragment();
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(archiveFragment);
ft.attach(archiveFragment);
ft.commit();
}
}
});
}
}
另外的事情是,當我做SwipeRefresh然後將列表得到更新。我現在完全搞不懂這種行爲。當我滑動選項卡時,相同的方法被調用,並且列表沒有得到更新,但是當我正在執行SwipeRefresh時,它將得到更新。
這是活性片段:
public class ActiveFragment extends Fragment {
public void updateFragment(){
new GetUsersArchivedFromServerTask().execute();
}
public class GetUsersArchivedFromServerTask extends AsyncTask<User, Void, String> {
@Override
protected String doInBackground(Shipment... parmas) {
// Log.d(TAG, String.valueOf(shipmentDbHandler.getAllActiveShipments().size()));
_userList1 = userDbHandler.getAllActiveUserByToday(DateTimeUtil.getCurrentTime());
return "";
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String str) {
// Set the refresh Listener to false after the list has been loaded with new set of data
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
if(_userList1.size() == 0){
ViewGroup parentOne = (ViewGroup) viewOne.getParent();
if(parentOne != null){
parentOne.removeView(viewOne);
}
}
if(_shipmentList1.size() > 0){
mShipmentAdapter = new ShipmentAdapter(getActivity(),_userList1,1);
shipmentListView1.setAdapter(mShipmentAdapter);
setListViewHeightBasedOnChildren(shipmentListView1);
ViewGroup parentOne = (ViewGroup) viewOne.getParent();
if(parentOne == null){
mainLayoutOne.addView(viewOne);
}
mShipmentAdapter.notifyDataSetChanged();
}
mSwipeRefreshLayout.setClickable(true);
}
}
}
在我的代碼ViewPagerAdapter同時擴展FragmentStatePagerAdapter .. – anand
視圖尋呼機負荷側片段,這樣的onResume方法已經調用,所以當你刷到的片段,然後沒有的onResume再次調用 –