ViewPager FragmentPagerAdapter仅更新onNavigationItemSelected上的最后一个片段

时间:2013-04-26 15:00:13

标签: android listview android-viewpager fragmentpageradapter

我在一个片段中托管ViewPager,该片段托管两个ListFragments,这些Cursor由SQLite查询返回的ViewPager填充。我最近在我的Action Bar中添加了一个微调器,允许用户对ViewPager中的列表进行排序。但是,每次选择微调器时,即使当前显示第一个列表,也只对第二个列表进行排序。似乎ListFragment没有跟踪当前显示的getItemPosition

我尝试在我的适配器中覆盖otifyDataSetChanged并使用n onAttach(),但这没有帮助。我试图缓存OnSortChanged中当前活动片段的位置也无济于事。我还尝试使用我的适配器实现的ListFragments接口来尝试重新创建public class BeerMenuFragment extends SherlockFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.beer_menu_fragment, container, false); ViewPager pager = (ViewPager) view.findViewById(R.id.beer_pager); pager.setAdapter(buildAdapter()); return view; } private PagerAdapter buildAdapter() { return (new BeerMenuAdapter(getChildFragmentManager())); } } 。我真的在这里不知所措。这是我目前的代码:

我的主要片段:

FragmentPagerAdapter

我的public class BeerMenuAdapter extends FragmentPagerAdapter { private final int NUM_ITEMS = 2; public BeerMenuAdapter(FragmentManager fm) { super(fm); } /* * (non-Javadoc) * * @see android.support.v4.view.PagerAdapter#getPageTitle(int) */ @Override public CharSequence getPageTitle(int position) { return BeerListFragment.getTitle(position); } @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { return BeerListFragment.newInstance(position); } }

ListFragment

最后,public class BeerListFragment extends SherlockListFragment implements OnNavigationListener { private static final String KEY_POSITION = "position"; private static final String KEY_SPINNER = "ab_spinner"; private IconicAdapter adapter; private static final String[] LABELS = { "Newest First", "Oldest First", "Alphabetical" }; private Context ctxt; private int mShowItems; private Cursor data; private boolean synthetic = true; static BeerListFragment newInstance(int position) { BeerListFragment frag = new BeerListFragment(); Bundle args = new Bundle(); args.putInt(KEY_POSITION, position); frag.setArguments(args); return (frag); } static String getTitle(int position) { String header = "Draft"; switch (position) { case 0: header = "Draft"; break; case 1: header = "Bottle"; break; } return (header); } /* * (non-Javadoc) * * @see * android.support.v4.app.ListFragment#onCreateView(android.view.LayoutInflater * , android.view.ViewGroup, android.os.Bundle) */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View result = inflater.inflate(R.layout.menu_list_fragment, container, false); ctxt = getActivity().getApplicationContext(); SQLiteDatabase db = DatabaseHelper.getInstance(ctxt) .getReadableDatabase(); SherlockFragmentActivity activity = getSherlockActivity(); ArrayAdapter<String> show = null; ActionBar bar = activity.getSupportActionBar(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { show = new ArrayAdapter<String>(bar.getThemedContext(), android.R.layout.simple_spinner_item, LABELS); } else { show = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, LABELS); } show.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); bar.setListNavigationCallbacks(show, this); if (savedInstanceState != null) { bar.setSelectedNavigationItem(savedInstanceState .getInt(KEY_SPINNER)); } data = getBeerMenuDbData(db, mShowItems); // set up list adapter adapter = new IconicAdapter(ctxt, data); setListAdapter(adapter); return (result); } private Cursor getBeerMenuDbData(SQLiteDatabase db, int showItems) { String order; String WHERE; switch (showItems) { case 1: // Oldest First order = DbContract.BeerEntry.COLUMN_MODIFIED + " ASC"; break; case 2: // Alphabetical order = DbContract.BeerEntry.COLUMN_TITLE + " ASC"; break; default: // Newest first order = DbContract.BeerEntry.COLUMN_MODIFIED + " DESC"; break; } String[] COLUMNS = { DbContract.BeerEntry._ID, DbContract.BeerEntry.COLUMN_TITLE, DbContract.BeerEntry.COLUMN_MODIFIED }; WHERE = "serving='" + getTitle(getArguments().getInt(KEY_POSITION, -1)) + "'"; return db.query(DbContract.BeerEntry.TABLE_NAME, COLUMNS, WHERE, null, null, null, order); } class IconicAdapter extends CursorAdapter { LayoutInflater inflater; IconicAdapter(Context context, Cursor data) { super(context, data, 0); inflater = LayoutInflater.from(context); } @Override public void bindView(View view, Context context, Cursor cursor) { String dateString = cursor.getString(2); String[] dateArray = new String[3]; TextView beerName = (TextView) view.findViewById(R.id.beerName); TextView date = (TextView) view.findViewById(R.id.drinkDate); RatingBar rb = (RatingBar) view.findViewById(R.id.ratingBar1); beerName.setText(cursor.getString(1)); if (!dateString.isEmpty()) { dateArray = dateString.split("-"); date.setText(dateArray[1] + "/" + dateArray[2] + "/" + dateArray[0]); } else { date.setText(dateString); } rb.setRating((float) Math.random() * 5.0f); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return inflater .inflate(R.layout.beer_club_list_item, parent, false); } } /* * (non-Javadoc) * * @see android.support.v4.app.Fragment#onDestroy() */ @Override public void onDestroy() { super.onDestroy(); ((CursorAdapter) getListAdapter()).getCursor().close(); } @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { if (synthetic) { synthetic = false; return true; } mShowItems = itemPosition; SQLiteDatabase db = DatabaseHelper.getInstance(ctxt) .getReadableDatabase(); Cursor newData = getBeerMenuDbData(db, mShowItems); adapter.changeCursor(newData); return true; } @Override public void onSaveInstanceState(Bundle outState) { outState.putInt(KEY_SPINNER, getSherlockActivity() .getSupportActionBar().getSelectedNavigationIndex()); } }

{{1}}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

所以,您的问题是您在bar.setListNavigationCallbacks(show, this)内呼叫BeerListFragment。当您在BeerListFragment中创建第一个ViewPager时,它会将自己设置为导航选择的回调。然后,当您创建第二个BeerListFragment时(ViewPager恢复当前可见页面左侧和右侧的页面),它会将自身设置为导航选择的回调,并且覆盖< / strong>第一个BeerListFragment,现在将不再获得onNavigationItemSelected回调。

这一切都与您所看到的行为有关,最后BeerListFragment来电bar.setListNavigationCallbacks将是唯一来接收onNavigationItemSelected回调。这就是为什么只排序第二个BeerListFragment

要解决这个问题,我建议您做一些事情:

  1. 管理设置BeerMenuFragment中导航选择的回调,并将结果传递给处于“活着”(Fragment)中的子onResume。您需要使用其中一个answers here才能执行此操作。
  2. 存储应用的最新排序选项,并检查每个onResume的{​​{1}}以查看其排序是否匹配,如果不匹配,则应用排序。
  3. 这将保证您的所有排序都应用于BeerListFragment中的所有网页,无论是否在应用排序时显示这些网页。