使用ViewPager动态加载下一个片段

时间:2011-08-01 19:01:21

标签: android

我正在使用新的android compatibility library创建一个演示应用。我想用ViewPager显示两个listfragments。第一个显示根类别,第二个显示子类别。在有人点击根类别之后,viewpager会移动到子类别,但它不起作用,因为始终显示根类别元素。 我花了几分钟进行调试,并且我意识到ViewPager在第一个片段之后创建了第二个片段。(我认为寻呼机缓存了下一个视图或片段。)

是否可以在onListItemClick之后加载第二个片段?

以下是代码:

public class ViewPagerDemoActivity extends FragmentActivity implements OnBookSelectedListener {

private static int mSelectedCategoryId;
private MyAdapter mMyAdapter;
private ViewPager mPager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setupPager();
}

private void setupPager() {
    mPager = (ViewPager) findViewById(R.id.pager);
    mMyAdapter = new MyAdapter(getSupportFragmentManager());
    mPager.setAdapter(mMyAdapter);
}

@Override
public void onBookSelected(int categoryId) {
    mSelectedCategoryId = categoryId;
    if (mSelectedCategoryId == 0) {
        mPager.setCurrentItem(1);
    } else {
        mPager.setCurrentItem(0);
    }
}

public static class MyAdapter extends FragmentPagerAdapter {
    private static final int NUMBER_OF_FRAGMENTS = 2;

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return BookListFragment.newInstance(mSelectedCategoryId);
    }

    @Override
    public int getCount() {
        return NUMBER_OF_FRAGMENTS;
    }
}

}

public class BookListFragment extends ListFragment {
private static final String ARGUMENT_KEY = "categoryId";
private OnBookSelectedListener mOnBookSelectedListener;
private int mCategoryId;

public static BookListFragment newInstance(int categoryId) {
    BookListFragment bookListFragment = new BookListFragment();
    Bundle argument = new Bundle();
    argument.putInt(ARGUMENT_KEY, categoryId);
    bookListFragment.setArguments(argument);
    return bookListFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCategoryId = getArguments() != null ? getArguments().getInt(ARGUMENT_KEY) : 0;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Books.getBooks(mCategoryId)));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.pager_list, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView) tv).setText("Category :" + mCategoryId);
    return v;
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (mCategoryId == 0) {
        mOnBookSelectedListener.onBookSelected(1);
    } else {
        mOnBookSelectedListener.onBookSelected(0);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mOnBookSelectedListener = (OnBookSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + "Must Implement OnBookSelectedListener.");
    }
}

public interface OnBookSelectedListener {
    public void onBookSelected(int categoryId);
}

}

public class Books {
private static final String[] fruits = new String[] { "Apple", "Banana", "Orange" };
private static final String[] category = new String[] { "Lorem", "Dolor", "Ipsum" };

public static String[] getBooks(int categoryId) {
    return categoryId == 0 ? category : fruits;
}

}

2 个答案:

答案 0 :(得分:7)

我解决了这个问题,我需要将List添加到适配器,然后getItem不创建新实例,只从列表中获取项目。

这是新代码。

public class ViewPagerDemoActivity extends FragmentActivity implements OnBookSelectedListener {

private MyAdapter mMyAdapter;
private ViewPager mPager;
private BookOnPageChangeListener mBookOnPageChangeListener;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setupPager();
}

private void setupPager() {
    mPager = (ViewPager) findViewById(R.id.pager);
    mMyAdapter = new MyAdapter(getSupportFragmentManager());
    mPager.setAdapter(mMyAdapter);
    mBookOnPageChangeListener = new BookOnPageChangeListener();
    mPager.setOnPageChangeListener(mBookOnPageChangeListener);
}

@Override
public void onBookSelected(int categoryId) {
    mMyAdapter.addPage(categoryId);
    mPager.setCurrentItem(mBookOnPageChangeListener.getCurrentPage() + 1);
}

public static class MyAdapter extends FragmentPagerAdapter {
    private List<Fragment> pages;

    public MyAdapter(FragmentManager fm) {
        super(fm);
        initPages();
    }

    /**
     * Create the list and add the first ListFragment to the ViewPager.
     */
    private void initPages() {
        pages = new ArrayList<Fragment>();
        addPage(0);
    }

    /**
     * Add new BookListFragment to the ViewPager.
     * 
     * @param categoryId
     *            - the category id
     */
    public void addPage(int categoryId) {
        pages.add(BookListFragment.newInstance(categoryId));
    }

    @Override
    public Fragment getItem(int position) {
        return pages.get(position);
    }

    @Override
    public int getCount() {
        return pages.size();
    }
}

/**
 * Get the current view position from the ViewPager.
 */
public static class BookOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
    private int currentPage;

    @Override
    public void onPageSelected(int position) {
        // current page from the actual position
        currentPage = position;
    }

    public int getCurrentPage() {
        return currentPage;
    }
}

}

答案 1 :(得分:0)

您是否尝试过使用setOnItemClickListener()?