1

我正在编写一个简单的新闻应用程序作为学习练习。显示由 SQLite 游标驱动的列表片段。单击并转到详细信息屏幕。现在尝试将详细信息屏幕转换为 ViewPager。我不想将光标从 listfragment 传递给 ViewPager 活动,所以我改为传递光标中的当前位置,然后创建一个新的光标。似乎无法获得正确的位置。如果我在 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;
   }
  }
4

0 回答 0