ViewPager光标位置

时间:2013-10-07 19:30:50

标签: android android-viewpager

我正在编写一个简单的新闻应用程序作为学习练习。显示由SQLite游标驱动的listfragment。单击并转到详细信息屏幕。现在尝试将详细信息屏幕转换为ViewPager。我不想将Cursor从listfragment传递给ViewPager活动,所以我在游标中传递当前位置,然后创建一个新的Cursor。似乎无法获得正确的位置。如果我在Activity中设置它而不是getItem(),它会转到正确的位置,但永远不会改变。如果我在getItem中设置它,它总是从位置0开始。关键行是

// cp.getCursor().moveToPosition(mStartPosition);
// cp.notifyDataSetChanged();

感谢您的帮助。

  public class ViewPagerActivity extends FragmentActivity {

    private static final int NUM_PAGES = 5;

    private static String mSectionSelected = "";
    private static int mStartPosition;

    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_pager);

        // Instantiate a ViewPager and a PagerAdapter.

        mSectionSelected = (String)this.getIntent().getSerializableExtra("SECTION");

        mPager = (ViewPager) this.findViewById(R.id.pager);
        CursorPagerAdapter cp = new CursorPagerAdapter( getSupportFragmentManager(), mSectionSelected);
        mPager.setAdapter(cp);
       // cp.getCursor().moveToPosition(mStartPosition);
       // cp.notifyDataSetChanged();
    }

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            // If the user is currently looking at the first step, allow the system to handle the
            // Back button. This calls finish() on this activity and pops the back stack.
            super.onBackPressed();
        } else {
            // Otherwise, select the previous step.
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }
 }

这是适配器 - 关键行是cursor.moveToPosition(position);

 public class CursorPagerAdapter extends FragmentStatePagerAdapter  {

private Cursor cursor;
private static String mSectionSelected;

public CursorPagerAdapter(FragmentManager fm,  String mSection) {
    super(fm);

    mSectionSelected = mSection;
    this.cursor = NewsManager.get(ApplicationContext.getInstance()).queryNews(mSectionSelected);

}


@Override
public PagerDetailFragment getItem(int position) {
    if (cursor == null) // shouldn't happen
        return null;

    cursor.moveToPosition(position);

    PagerDetailFragment frag;
    try {
        frag = PagerDetailFragment.newInstance();
        String mTitle = cursor.getString(NewsDatabaseHelper.COLNUM_TITLE);
        String mSection = cursor.getString(NewsDatabaseHelper.COLNUM_CATEGORY);
        String mSource = cursor.getString(NewsDatabaseHelper.COLNUM_SOURCE);
        String mPublishedDate = cursor.getString(NewsDatabaseHelper.COLNUM_PUBLISHDATE);
        String mThumbnail = cursor.getString(NewsDatabaseHelper.COLNUM_THUMBNAIL);
        String mBody = cursor.getString(NewsDatabaseHelper.COLNUM_CONTENT);
        Bundle args = new Bundle();
        args.putString(NewsDatabaseHelper.COLUMN_TITLE, mTitle);
        args.putString(NewsDatabaseHelper.COLUMN_CATEGORY, mSection);
        args.putString(NewsDatabaseHelper.COLUMN_SOURCE, mSource);

        args.putString(NewsDatabaseHelper.COLUMN_PUBLISHDATE, mPublishedDate);
        args.putString(NewsDatabaseHelper.COLUMN_THUMBNAIL, mThumbnail);
        args.putString(NewsDatabaseHelper.COLUMN_CONTENT, mBody);
        frag.setArguments(args);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    return frag;
}

 @Override
 public int getCount() {
    if (cursor == null)
        return 0;
    else
        return cursor.getCount();
}

  public void swapCursor(Cursor c) {
    if (cursor == c)
        return;

    this.cursor = c;
      notifyDataSetChanged();
}

  public Cursor getCursor() {
    return cursor;
   }
  }

0 个答案:

没有答案