package cl.monsoon.s1next.view.adapter;

import android.app.Activity;
import android.content.res.Resources;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import java.util.List;

import cl.monsoon.s1next.R;
import cl.monsoon.s1next.widget.EmoticonFactory;

public final class EmoticonPagerAdapter extends PagerAdapter {

    private final Activity mActivity;

    private final float mEmoticonWidth;
    private final int mEmoticonGridPadding;

    private final EmoticonFactory mEmoticonFactory;
    private final List<String> mEmoticonTypeTitles;

    public EmoticonPagerAdapter(Activity activity) {
        this.mActivity = activity;

        Resources resources = activity.getResources();
        mEmoticonWidth = resources.getDimension(R.dimen.minimum_touch_target_size);
        mEmoticonGridPadding = resources.getDimensionPixelSize(R.dimen.emoticon_padding);

        mEmoticonFactory = new EmoticonFactory(activity);
        mEmoticonTypeTitles = mEmoticonFactory.getEmotionTypeTitles();
    }

    @Override
    public int getCount() {
        return mEmoticonTypeTitles.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mEmoticonTypeTitles.get(position);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        RecyclerView recyclerView = new RecyclerView(mActivity);
        recyclerView.setHasFixedSize(true);
        recyclerView.setClipToPadding(false);
        recyclerView.setPadding(0, mEmoticonGridPadding, 0, mEmoticonGridPadding);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mActivity, 1);
        recyclerView.setLayoutManager(gridLayoutManager);
        RecyclerView.Adapter recyclerAdapter = new EmoticonGridRecyclerAdapter(mActivity,
                mEmoticonFactory.getEmoticonsByIndex(position));
        recyclerView.setAdapter(recyclerAdapter);

        // auto fit grid
        // forked from https://stackoverflow.com/questions/26666143/recyclerview-gridlayoutmanager-how-to-auto-detect-span-count#answer-27000759
        recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    @Override
                    @SuppressWarnings("deprecation")
                    public void onGlobalLayout() {
                        recyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        int measuredWidth = recyclerView.getMeasuredWidth();
                        int spanCount = (int) Math.floor(measuredWidth / mEmoticonWidth);
                        gridLayoutManager.setSpanCount(spanCount);
                        gridLayoutManager.requestLayout();
                    }
                });

        container.addView(recyclerView);

        return recyclerView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

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

@@ -101,16 +101,41 @@
 ources;%0A
+import android.os.Build;%0A
 import a
@@ -2537,24 +2537,247 @@
 lLayout() %7B%0A
+                        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN) %7B%0A                            recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);%0A                        %7D else %7B%0A    
             
@@ -2851,32 +2851,58 @@
 Listener(this);%0A
+                        %7D%0A
                 

Use `removeOnGlobalLayoutListener` if API >= 16