其他分享
首页 > 其他分享> > Android-ViewPager仅翻转布局而不是片段

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-fragments,android-layout,android-viewpager,xamarin,android
来源: https://codeday.me/bug/20191119/2037386.html