简体   繁体   中英

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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