Android - ViewPager只翻转Layouts而不是Fragments

时间:2015-10-12 19:12:33

标签: android android-layout android-fragments xamarin android-viewpager

Android上是否有一种简单的方法可以在不同布局之间进行切换而无需使用片段。我的观点是静态的(那里没有真正的发生),但出于本地化的目的,我们不会使用图像,所以我会选择布局解决方案。

我很感激有一个很好的例子来展示这种技术。

修改

以下是基于以下答案的完整工作代码:

public class LayoutPagerAdapter : PagerAdapter
    {
        Context m_context;
        readonly int[] m_slideLayoutResourceIds;

        public LayoutPagerAdapter(Context context, int[] slideLayoutResourceIds)
        {
            m_context = context;
            m_slideLayoutResourceIds = slideLayoutResourceIds;
        }

        public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
        {
            var inflater = LayoutInflater.From(m_context);

            var view = inflater.Inflate(m_slideLayoutResourceIds[position], container, false);

            container.AddView(view);

            return view;
        }

        public override void DestroyItem(View container, int position, Java.Lang.Object objectValue)
        {
            base.DestroyItem(container, position, objectValue);
        }

        #region implemented abstract members of PagerAdapter

        public override bool IsViewFromObject(View view, Java.Lang.Object objectValue)
        {
            return view == objectValue;
        }

        public override int Count
        {
            get
            {
                return m_slideLayoutResourceIds.Length;
            }
        }

        #endregion
    }

1 个答案:

答案 0 :(得分:3)

您可以为此创建自己的简单适配器:

public static class ViewPagerAdapter extends PagerAdapter {

  private Context mContext;

  public ViewPagerAdapter(Context context) {
    mContext = context;
  }

  @Override
  public View instantiateItem(ViewGroup container, int position) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.your_layout, container, false);

    return view;
  }

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

  @Override
  public boolean isViewFromObject(View view, Object object) {
    return view == object;
  }

}