使用没有片段的页面适配器

时间:2013-08-08 04:08:56

标签: android android-viewpager

是否可以在没有片段的情况下使用ViewPager? 我可以找到所有示例,将几个片段加载到ViewPager。

我想和ListView类似地使用它。我想创建一个自定义适配器,根据寻呼机所处的位置来扩展布局。

如果有可能,有人能指出我的一个例子吗?

3 个答案:

答案 0 :(得分:11)

我遇到了类似的问题,我花了一些时间才弄明白。在此处发布我的适配器以供将来参考。

/**
 * A page adapter which works with a large data set by reusing views.
 */
public abstract class ListPageAdapter<T> extends PagerAdapter {

  // Views that can be reused.
  private final List<View> mDiscardedViews = new ArrayList<View>();
  // Views that are already in use.
  private final SparseArray<View> mBindedViews = new SparseArray<View>();

  private final ArrayList<T> mItems;
  private final LayoutInflater mInflator;
  private final int mResourceId;

  public ListPageAdapter(Context context, int viewRes) {
    mItems = new ArrayList<T>();
    mInflator = LayoutInflater.from(context);
    mResourceId = viewRes;
  }

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

  @Override
  public boolean isViewFromObject(View v, Object obj) {
    return v == mBindedViews.get(mItems.indexOf(obj));
  }

  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
    View view = mBindedViews.get(position);
    if (view != null) {
      mDiscardedViews.add(view);
      mBindedViews.remove(position);
      container.removeView(view);
    }
  }

  @Override
  public Object instantiateItem(ViewGroup container, int position) {
    View child = mDiscardedViews.isEmpty() ?
        mInflator.inflate(mResourceId, container, false) :
          mDiscardedViews.remove(0);

    T data = mItems.get(position);
    initView(child, data, position);

    mBindedViews.append(position, child);
    container.addView(child, 0);
    return data;
  }

  public void add(T item) {
    mItems.add(item);
  }

  public T remove(int position) {
    return mItems.remove(position);
  }

  public void clear() {
    mItems.clear();
  }

  /**
   * Initiate the view here
   */
  public abstract void initView(View v, T item, int position);
}

在适配器中更改数据时,viewPager不会自动更新。为此我用了

pager.setAdapter(null);
pager.setAdapter(myAdapter);
pager.setCurrentItem(0);

答案 1 :(得分:7)

是的,完全有可能。由于ViewPager.setAdapter()接受任何实例PageAdapter,您只需要创建自己的PagerAdapter子类(它有点类似于您在ListView中使用的BaseAdapter或ArrayAdapter的子类)。请记住,对于PAgerAdapter,文档说:

  

实施PagerAdapter时,必须覆盖以下内容   方法至少:

希望这有帮助

答案 2 :(得分:0)

这是Kotlin解决方案。将此类的对象设置为viewPager-

的适配器
class PageAdapter : PagerAdapter() {
    private val pageLayouts = listOf(R.layout.home_page_1, R.layout.home_page_2)

    override fun instantiateItem(container: ViewGroup, position: Int): Any =
        LayoutInflater.from(container.context).inflate(pageLayouts[position], container, false).apply {
            container.addView(this)
        }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) =
        container.removeView(`object` as View)

    override fun getCount(): Int = 2

    override fun isViewFromObject(view: View, `object`: Any): Boolean = (view == `object`)
}