简体   繁体   English

Android - ViewPager只翻转Layouts而不是Fragments

[英]Android - ViewPager flipping just Layouts instead of Fragments

Is there a simple way on Android how to flip between different layouts without the need of using fragments. 在Android上有一个简单的方法,如何在不需要使用片段的情况下在不同的布局之间进行切换。 My views are static (nothing really happens there) but for localization purposes we won't be using images so I would go for layout solution. 我的观点是静态的(在那里没有真正发生),但出于本地化目的,我们不会使用图像,所以我会选择布局解决方案。

I would be grateful for a good example showing this technique. 我将很高兴看到一个展示这种技术的好例子。

EDIT: 编辑:

Here is fully working code based on the answer below: 以下是基于以下答案的完整工作代码:

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
    }

You can create your own simple adapter for that: 您可以为此创建自己的简单适配器:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM