在我的应用程序中,我使用视图寻呼机使用导航选项卡.我可以使用以下代码成功绘制标签:
https://github.com/codepath/android_guides/wiki/Sliding-Tabs-with-PagerSlidingTabStrip
在这里,我已经使用滑动选项卡和查看寻呼机两者来获取导航选项卡.一切都正常,但当我移动到另一个选项卡时,列表没有得到更新.即使对象列表变量在调试时也会更新,但视觉列表没有被更新,OnResume()也被调用.
这里我有一些代码片段:
For Tab1 : which in my case is ACTIVE tab
@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 } }
On Tab2 : Which is Archive tab
@Override public void onResume() { super.onResume(); new GetUsersArchivedFromServerTask().execute(); // Network calls }
主要活动:
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); } } }
解决方法
PLease使用FragmentStatePagerAdapter.