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
    }

解决方法:

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

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;
  }

}
上一篇:android-缩放ImageView以适合屏幕宽度/高度,同时保持宽高比


下一篇:c#-SQLite Net Extensions OneToMany关系未插入/保存/更新子级