Example usage for android.os Looper getMainLooper

List of usage examples for android.os Looper getMainLooper

Introduction

In this page you can find the example usage for android.os Looper getMainLooper.

Prototype

public static Looper getMainLooper() 

Source Link

Document

Returns the application's main looper, which lives in the main thread of the application.

Usage

From source file:com.alibaba.android.vlayout.example.VLayoutActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    mFirstText = (TextView) findViewById(R.id.first);
    mLastText = (TextView) findViewById(R.id.last);
    mCountText = (TextView) findViewById(R.id.count);
    mTotalOffsetText = (TextView) findViewById(R.id.total_offset);

    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_view);

    findViewById(R.id.jump).setOnClickListener(new View.OnClickListener() {
        @Override//from  ww  w.ja  v  a 2  s.  c om
        public void onClick(View v) {
            EditText position = (EditText) findViewById(R.id.position);
            if (!TextUtils.isEmpty(position.getText())) {
                try {
                    int pos = Integer.parseInt(position.getText().toString());
                    recyclerView.scrollToPosition(pos);
                } catch (Exception e) {
                    Log.e("VlayoutActivity", e.getMessage(), e);
                }
            } else {
                recyclerView.requestLayout();
            }
        }
    });

    final VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {

        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int i, int i2) {
            mFirstText.setText("First: " + layoutManager.findFirstVisibleItemPosition());
            mLastText.setText(
                    "Existing: " + MainViewHolder.existing + " Created: " + MainViewHolder.createdTimes);
            mCountText.setText("Count: " + recyclerView.getChildCount());
            mTotalOffsetText.setText("Total Offset: " + layoutManager.getOffsetToStart());
        }
    });

    recyclerView.setLayoutManager(layoutManager);

    // layoutManager.setReverseLayout(true);

    RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = ((LayoutParams) view.getLayoutParams()).getViewPosition();
            outRect.set(4, 4, 4, 4);
        }
    };

    final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();

    recyclerView.setRecycledViewPool(viewPool);

    // recyclerView.addItemDecoration(itemDecoration);

    viewPool.setMaxRecycledViews(0, 20);

    final DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager, true);

    recyclerView.setAdapter(delegateAdapter);

    final List<DelegateAdapter.Adapter> adapters = new LinkedList<>();

    if (BANNER_LAYOUT) {
        adapters.add(new SubAdapter(this, new LinearLayoutHelper(), 1) {

            @Override
            public void onViewRecycled(MainViewHolder holder) {
                if (holder.itemView instanceof ViewPager) {
                    ((ViewPager) holder.itemView).setAdapter(null);
                }
            }

            @Override
            public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                if (viewType == 1)
                    return new MainViewHolder(LayoutInflater.from(VLayoutActivity.this)
                            .inflate(R.layout.view_pager, parent, false));

                return super.onCreateViewHolder(parent, viewType);
            }

            @Override
            public int getItemViewType(int position) {
                return 1;
            }

            @Override
            protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

            }

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                if (holder.itemView instanceof ViewPager) {
                    ViewPager viewPager = (ViewPager) holder.itemView;

                    viewPager.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200));

                    // from position to get adapter
                    viewPager.setAdapter(new PagerAdapter(this, viewPool));
                }
            }
        });
    }

    //        {
    //            GridLayoutHelper helper = new GridLayoutHelper(10);
    //            helper.setAspectRatio(4f);
    //            helper.setGap(10);
    //            adapters.add(new SubAdapter(this, helper, 80));
    //        }

    if (FLOAT_LAYOUT) {
        FloatLayoutHelper layoutHelper = new FloatLayoutHelper();
        layoutHelper.setAlignType(FixLayoutHelper.BOTTOM_RIGHT);
        layoutHelper.setDefaultLocation(100, 400);
        LayoutParams layoutParams = new LayoutParams(150, 150);
        adapters.add(new SubAdapter(this, layoutHelper, 1, layoutParams));
    }

    if (LINEAR_LAYOUT) {
        LinearLayoutHelper layoutHelper1 = new LinearLayoutHelper();
        layoutHelper1.setAspectRatio(2.0f);
        LinearLayoutHelper layoutHelper2 = new LinearLayoutHelper();
        layoutHelper2.setAspectRatio(4.0f);
        layoutHelper2.setDividerHeight(10);
        layoutHelper2.setMargin(10, 30, 10, 10);
        layoutHelper2.setPadding(10, 30, 10, 10);
        layoutHelper2.setBgColor(0xFFF5A623);
        adapters.add(new SubAdapter(this, layoutHelper1, 1));
        adapters.add(new SubAdapter(this, layoutHelper2, 6) {

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                if (position % 2 == 0) {
                    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
                    layoutParams.mAspectRatio = 5;
                    holder.itemView.setLayoutParams(layoutParams);
                }
            }
        });
    }

    if (STICKY_LAYOUT) {
        StickyLayoutHelper layoutHelper = new StickyLayoutHelper();
        layoutHelper.setOffset(100);
        layoutHelper.setAspectRatio(4);
        adapters.add(new SubAdapter(this, layoutHelper, 1,
                new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
    }

    if (SINGLE_LAYOUT) {
        SingleLayoutHelper layoutHelper = new SingleLayoutHelper();
        layoutHelper.setBgColor(Color.rgb(135, 225, 90));
        layoutHelper.setAspectRatio(4);
        layoutHelper.setMargin(10, 20, 10, 20);
        layoutHelper.setPadding(10, 10, 10, 10);
        adapters.add(new SubAdapter(this, layoutHelper, 1,
                new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
    }

    if (COLUMN_LAYOUT) {
        ColumnLayoutHelper layoutHelper = new ColumnLayoutHelper();
        layoutHelper.setBgColor(0xff00f0f0);
        layoutHelper.setWeights(new float[] { 40.0f, Float.NaN, 40 });
        adapters.add(new SubAdapter(this, layoutHelper, 5) {

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                if (position == 0) {
                    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
                    layoutParams.mAspectRatio = 4;
                    holder.itemView.setLayoutParams(layoutParams);
                } else {
                    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
                    layoutParams.mAspectRatio = Float.NaN;
                    holder.itemView.setLayoutParams(layoutParams);
                }
            }

        });
    }

    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xff876384);
        helper.setAspectRatio(4.0f);
        helper.setColWeights(new float[] { 40f, 45f });
        helper.setMargin(10, 20, 10, 20);
        helper.setPadding(10, 10, 10, 10);
        adapters.add(new SubAdapter(this, helper, 2));
    }

    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xffef8ba3);
        helper.setAspectRatio(2.0f);
        helper.setColWeights(new float[] { 40f });
        helper.setRowWeight(30f);
        helper.setMargin(10, 20, 10, 20);
        helper.setPadding(10, 10, 10, 10);
        adapters.add(new SubAdapter(this, helper, 4) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                LayoutParams lp = (LayoutParams) holder.itemView.getLayoutParams();
                if (position == 0) {
                    lp.rightMargin = 1;
                } else if (position == 1) {

                } else if (position == 2) {
                    lp.topMargin = 1;
                    lp.rightMargin = 1;
                }
            }
        });
    }

    if (ONEN_LAYOUT) {
        adapters.add(new SubAdapter(this, new OnePlusNLayoutHelper(), 0));
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xff87e543);
        helper.setAspectRatio(1.8f);
        helper.setColWeights(new float[] { 33.33f, 50f, 40f });
        helper.setMargin(10, 20, 10, 20);
        helper.setPadding(10, 10, 10, 10);
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        adapters.add(new SubAdapter(this, helper, 3, lp) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                LayoutParams lp = (LayoutParams) holder.itemView.getLayoutParams();
                if (position == 0) {
                    lp.rightMargin = 1;
                }
            }
        });
    }

    if (COLUMN_LAYOUT) {
        adapters.add(new SubAdapter(this, new ColumnLayoutHelper(), 0));
        adapters.add(new SubAdapter(this, new ColumnLayoutHelper(), 4));
    }

    if (FIX_LAYOUT) {
        FixLayoutHelper layoutHelper = new FixLayoutHelper(10, 10);
        adapters.add(new SubAdapter(this, layoutHelper, 0));

        layoutHelper = new FixLayoutHelper(FixLayoutHelper.TOP_RIGHT, 20, 20);

        adapters.add(new SubAdapter(this, layoutHelper, 1) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                LayoutParams layoutParams = new LayoutParams(200, 200);
                holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }

    if (STICKY_LAYOUT) {
        StickyLayoutHelper layoutHelper = new StickyLayoutHelper(false);
        adapters.add(new SubAdapter(this, layoutHelper, 0));
        layoutHelper = new StickyLayoutHelper(false);
        layoutHelper.setOffset(100);
        adapters.add(new SubAdapter(this, layoutHelper, 1,
                new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
    }

    if (GRID_LAYOUT) {
        GridLayoutHelper layoutHelper = new GridLayoutHelper(2);
        layoutHelper.setMargin(7, 0, 7, 0);
        layoutHelper.setWeights(new float[] { 46.665f });
        layoutHelper.setHGap(3);
        adapters.add(new SubAdapter(this, layoutHelper, 2));

        layoutHelper = new GridLayoutHelper(4);
        layoutHelper.setWeights(new float[] { 20f, 26.665f });
        layoutHelper.setMargin(7, 0, 7, 0);
        layoutHelper.setHGap(3);
        adapters.add(new SubAdapter(this, layoutHelper, 8));
    }

    if (GRID_LAYOUT) {
        adapters.add(new SubAdapter(this, new GridLayoutHelper(4), 0));

        GridLayoutHelper helper = new GridLayoutHelper(4);
        helper.setAspectRatio(4f);
        //helper.setColWeights(new float[]{40, 20, 30, 30});
        // helper.setMargin(0, 10, 0, 10);
        helper.setGap(10);
        adapters.add(new SubAdapter(this, helper, 80) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                LayoutParams lp = (LayoutParams) holder.itemView.getLayoutParams();
                // lp.bottomMargin = 1;
                // lp.rightMargin = 1;
            }
        });
    }

    if (FIX_LAYOUT) {
        adapters.add(new SubAdapter(this, new ScrollFixLayoutHelper(20, 20), 1) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                LayoutParams layoutParams = new LayoutParams(200, 200);
                holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }

    if (LINEAR_LAYOUT)
        adapters.add(new SubAdapter(this, new LinearLayoutHelper(), 10));

    if (GRID_LAYOUT) {
        GridLayoutHelper helper = new GridLayoutHelper(3);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 3));
    }

    if (STAGGER_LAYOUT) {
        // adapters.add(new SubAdapter(this, new StaggeredGridLayoutHelper(2, 0), 0));
        final StaggeredGridLayoutHelper helper = new StaggeredGridLayoutHelper(2, 10);
        helper.setMargin(20, 10, 10, 10);
        helper.setPadding(10, 10, 20, 10);
        helper.setBgColor(0xFF86345A);
        adapters.add(new SubAdapter(this, helper, 27) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
                if (position % 2 == 0) {
                    layoutParams.mAspectRatio = 1.0f;
                } else {
                    layoutParams.height = 340 + position % 7 * 20;
                }
                holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }

    if (COLUMN_LAYOUT) {
        // adapters.add(new SubAdapter(this, new ColumnLayoutHelper(), 3));
    }

    if (GRID_LAYOUT) {
        // adapters.add(new SubAdapter(this, new GridLayoutHelper(4), 24));
    }

    delegateAdapter.setAdapters(adapters);

    final Handler mainHandler = new Handler(Looper.getMainLooper());

    trigger = new Runnable() {
        @Override
        public void run() {
            // recyclerView.scrollToPosition(22);
            // recyclerView.getAdapter().notifyDataSetChanged();
            // mainHandler.postDelayed(trigger, 1000);
            //List<DelegateAdapter.Adapter> newAdapters = new ArrayList<>();
            //newAdapters.add((new SubAdapter(VLayoutActivity.this, new ColumnLayoutHelper(), 3)));
            //newAdapters.add((new SubAdapter(VLayoutActivity.this, new GridLayoutHelper(4), 24)));
            //delegateAdapter.addAdapters(newAdapters);
            recyclerView.requestLayout();
        }
    };

    mainHandler.postDelayed(trigger, 1000);
    setListenerToRootView();
}

From source file:org.mycard.net.network.LoadListener.java

protected void handleEndData() {
    if (mCancelled)
        return;// w w  w  .  ja  v a2  s  .  co  m

    if (mStatusCode >= 400 && mStatusCode < 500 && mRetryCount < LoadListener.HTTP_MAX_RETRY_COUNT) {
        retry();
        return;
    }

    // modified by qiaozhi 2012.6.14 ?wml????
    // ?CMWAP???
    // if( mMimeType.equals("text/vnd.wap.wml") ){
    if (mMimeType.equals("text/vnd.wap.wml") && mRetryCount < LoadListener.HTTP_MAX_RETRY_COUNT) {
        // ???wml??,wml?CMWAP???
        retry();
        return;
    }
    // --------------- 2012.6.14 ----------------------

    switch (mStatusCode) {
    // added by zhangxm,
    case HTTP_OK:
        if (Looper.getMainLooper().getThread().equals(Thread.currentThread())) {
            // ?UI?
            HttpTaskMgr.instance(getContext()).setConnectionSetup();
        }
        mTaskListener.onHttpTaskEvent(m_taskid, HttpTaskListener.HTTPTASK_EVENT_END, null);
        break;
    case HTTP_MOVED_PERMANENTLY:
        // 301 - permanent redirect
        // mPermanent = true;
    case HTTP_FOUND:
    case HTTP_SEE_OTHER:
    case HTTP_TEMPORARY_REDIRECT:
        // 301, 302, 303, and 307 - redirect
        if (mStatusCode == HTTP_TEMPORARY_REDIRECT) {
            if (mRequestHandle != null && mRequestHandle.getMethod().equals("POST")) {
                sendMessageInternal(obtainMessage(MSG_LOCATION_CHANGED_REQUEST));
            } else if (mMethod != null && mMethod.equals("POST")) {
                sendMessageInternal(obtainMessage(MSG_LOCATION_CHANGED_REQUEST));
            } else {
                sendMessageInternal(obtainMessage(MSG_LOCATION_CHANGED));
            }
        } else {
            sendMessageInternal(obtainMessage(MSG_LOCATION_CHANGED));
        }
        return;
    case HTTP_AUTH:
    case HTTP_PROXY_AUTH:
        // According to rfc2616, the response for HTTP_AUTH must include
        // WWW-Authenticate header field and the response for
        // HTTP_PROXY_AUTH must include Proxy-Authenticate header field.
        HttpTaskEventArg arg = new HttpTaskEventArg();
        arg.mErrorId = HttpTaskListener.ERROR_AUTH;
        mTaskListener.onHttpTaskEvent(m_taskid, HttpTaskListener.HTTPTASK_EVENT_FAIL, arg);
        break; // use default
    case 206:
        if (!handlePartialContent()) {
            HttpTaskEventArg arg2 = new HttpTaskEventArg();
            arg2.mErrorId = HttpTaskListener.ERROR_BAD_URL;
            mTaskListener.onHttpTaskEvent(m_taskid, HttpTaskListener.HTTPTASK_EVENT_FAIL, arg2);
            break;
        } else {
            return;
        }
    default:
        HttpTaskEventArg arg1 = new HttpTaskEventArg();
        arg1.mErrorId = HttpTaskListener.ERROR;
        mTaskListener.onHttpTaskEvent(m_taskid, HttpTaskListener.HTTPTASK_EVENT_FAIL, arg1);
        break;
    }

    detachRequestHandle();
    tearDown();
}

From source file:com.vk.sdk.api.VKRequest.java

private void runOnLooper(Runnable block, int delay) {
    if (mLooper == null) {
        mLooper = Looper.getMainLooper();
    }//from www.  j ava2  s.  c o m
    if (delay > 0) {
        new Handler(mLooper).postDelayed(block, delay);
    } else {
        new Handler(mLooper).post(block);
    }
}

From source file:com.vk.sdk.api.VKRequest.java

private void runOnMainLooper(Runnable block) {

    new Handler(Looper.getMainLooper()).post(block);
}

From source file:android.support.animation.DynamicAnimation.java

/**
 * Starts an animation. If the animation has already been started, no op. Note that calling
 * {@link #start()} will not immediately set the property value to start value of the animation.
 * The property values will be changed at each animation pulse, which happens before the draw
 * pass. As a result, the changes will be reflected in the next frame, the same as if the values
 * were set immediately. This method should only be called on main thread.
 *
 * @throws AndroidRuntimeException if this method is not called on the main thread
 *//* www.j  ava2 s.  c  om*/
public void start() {
    if (Looper.myLooper() != Looper.getMainLooper()) {
        throw new AndroidRuntimeException("Animations may only be started on the main thread");
    }
    if (!mRunning) {
        startAnimationInternal();
    }
}

From source file:com.abiansoftware.lib.reader.AbianReaderData.java

public synchronized void addItem(AbianReaderItem newItem) {
    //Log.e(getClass().getName(), "Enter addItem");

    if (Looper.myLooper() != Looper.getMainLooper()) {
        //Log.e(getClass().getName(), "not on main thread, adding to the new item vector");

        m_newItemVector.add(newItem);//from   w  w w .  j a v a2s . c  om
    } else {
        //Log.e(getClass().getName(), "on main thread, adding to the item vector");

        m_itemVector.add(newItem);
    }

    //Log.e(getClass().getName(), "Leave addItem");
}

From source file:com.frostwire.android.gui.transfers.TransferManager.java

private void registerPreferencesChangeListener() {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        Engine.instance().getThreadPool().execute(() -> ConfigurationManager.instance()
                .registerOnPreferenceChange(onSharedPreferenceChangeListener));
    } else {/*from  www . j a  va  2s .c  o m*/
        ConfigurationManager.instance().registerOnPreferenceChange(onSharedPreferenceChangeListener);
    }
}

From source file:com.boko.vimusic.cache.ImageCache.java

private void waitUntilUnpaused() {
    synchronized (mPauseLock) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            while (mPauseDiskAccess) {
                try {
                    mPauseLock.wait();//  w w  w .  j  a  va  2s.  c o  m
                } catch (InterruptedException e) {
                    // ignored, we'll start waiting again
                }
            }
        }
    }
}

From source file:com.networking.ApiTestActivity.java

public void downloadImage(final View view) {
    String url = "http://i.imgur.com/AtbX9iX.png";
    AndroidNetworking.download(url, Utils.getRootDirPath(getApplicationContext()), "image1.png")
            .setPriority(Priority.MEDIUM).setTag(this).build().setAnalyticsListener(new AnalyticsListener() {
                @Override//from  w w  w . j av a 2  s . com
                public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived,
                        boolean isFromCache) {
                    Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
                    Log.d(TAG, " bytesSent : " + bytesSent);
                    Log.d(TAG, " bytesReceived : " + bytesReceived);
                    Log.d(TAG, " isFromCache : " + isFromCache);
                }
            }).startDownload(new DownloadListener() {
                @Override
                public void onDownloadComplete() {
                    Log.d(TAG, "Image download Completed");
                    Log.d(TAG, "onDownloadComplete isMainThread : "
                            + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
                }

                @Override
                public void onError(ANError error) {
                    if (error.getErrorCode() != 0) {
                        // received ANError from server
                        // error.getErrorCode() - the ANError code from server
                        // error.getErrorBody() - the ANError body from server
                        // error.getErrorDetail() - just a ANError detail
                        Log.d(TAG, "onError errorCode : " + error.getErrorCode());
                        Log.d(TAG, "onError errorBody : " + error.getErrorBody());
                        Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                    } else {
                        // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                        Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                    }
                }
            });
}

From source file:com.example.admin.activity.HomeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_view);

    //        findViewById(R.id.jump).setOnClickListener(new View.OnClickListener() {
    //            @Override
    //            public void onClick(View v) {
    //                EditText position = (EditText) findViewById(R.id.position);
    //                if (!TextUtils.isEmpty(position.getText())) {
    //                    try {
    //                        int pos = Integer.parseInt(position.getText().toString());
    //                        recyclerView.scrollToPosition(pos);
    //                    } catch (Exception e) {
    //                        Log.e("VlayoutActivity", e.getMessage(), e);
    //                    }
    //                } else {
    //                    recyclerView.requestLayout();
    //                }
    //            }
    //        });

    final VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override// w  w w . j a v  a  2  s . com
        public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {

        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int i, int i2) {
            //                mFirstText.setText("First: " + layoutManager.findFirstVisibleItemPosition());
            //                mLastText.setText("Existing: " + MainViewHolder.existing + " Created: " + MainViewHolder.createdTimes);
            //                mCountText.setText("Count: " + recyclerView.getChildCount());
            //                mTotalOffsetText.setText("Total Offset: " + layoutManager.getOffsetToStart());
        }
    });

    recyclerView.setLayoutManager(layoutManager);

    // layoutManager.setReverseLayout(true);

    RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = ((VirtualLayoutManager.LayoutParams) view.getLayoutParams()).getViewPosition();
            outRect.set(4, 4, 4, 4);
        }
    };

    final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();

    recyclerView.setRecycledViewPool(viewPool);

    // recyclerView.addItemDecoration(itemDecoration);

    viewPool.setMaxRecycledViews(0, 20);

    final DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager, true);

    recyclerView.setAdapter(delegateAdapter);

    List<DelegateAdapter.Adapter> adapters = new LinkedList<>();

    if (BANNER_LAYOUT) {
        adapters.add(new SubAdapter(this, new LinearLayoutHelper(), 1) {

            @Override
            public void onViewRecycled(MainViewHolder holder) {
                if (holder.itemView instanceof ViewPager) {
                    ((ViewPager) holder.itemView).setAdapter(null);
                }
            }

            @Override
            public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                if (viewType == 1)
                    return new MainViewHolder(
                            LayoutInflater.from(HomeActivity.this).inflate(R.layout.view_pager, parent, false));

                return super.onCreateViewHolder(parent, viewType);
            }

            @Override
            public int getItemViewType(int position) {
                return 1;
            }

            @Override
            protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

            }

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                if (holder.itemView instanceof ViewPager) {
                    ViewPager viewPager = (ViewPager) holder.itemView;

                    viewPager.setLayoutParams(
                            new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200));

                    // from position to get adapter
                    viewPager.setAdapter(new PagerAdapter(this, viewPool));
                }
            }
        });
    }

    //        {
    //            GridLayoutHelper helper = new GridLayoutHelper(10);
    //            helper.setAspectRatio(4f);
    //            helper.setGap(10);
    //            adapters.add(new SubAdapter(this, helper, 80));
    //        }

    if (FLOAT_LAYOUT) {
        FloatLayoutHelper layoutHelper = new FloatLayoutHelper();
        layoutHelper.setAlignType(FixLayoutHelper.BOTTOM_RIGHT);
        layoutHelper.setDefaultLocation(100, 400);
        VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(150, 150);
        adapters.add(new SubAdapter(this, layoutHelper, 1, layoutParams));
    }

    if (LINEAR_LAYOUT) {
        LinearLayoutHelper layoutHelper1 = new LinearLayoutHelper();
        layoutHelper1.setAspectRatio(2.0f);
        LinearLayoutHelper layoutHelper2 = new LinearLayoutHelper();
        layoutHelper2.setAspectRatio(4.0f);
        layoutHelper2.setDividerHeight(10);
        layoutHelper2.setMargin(10, 30, 10, 10);
        layoutHelper2.setPadding(10, 30, 10, 10);
        layoutHelper2.setBgColor(0xFFF5A623);
        adapters.add(new SubAdapter(this, layoutHelper1, 1));
        adapters.add(new SubAdapter(this, layoutHelper2, 6) {

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                if (position % 2 == 0) {
                    VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT, 300);
                    layoutParams.mAspectRatio = 5;
                    holder.itemView.setLayoutParams(layoutParams);
                }
            }
        });
    }

    if (STICKY_LAYOUT) {
        StickyLayoutHelper layoutHelper = new StickyLayoutHelper();
        layoutHelper.setOffset(100);
        layoutHelper.setAspectRatio(4);
        adapters.add(new SubAdapter(this, layoutHelper, 1,
                new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
    }

    if (SINGLE_LAYOUT) {
        SingleLayoutHelper layoutHelper = new SingleLayoutHelper();
        layoutHelper.setBgColor(Color.rgb(135, 225, 90));
        layoutHelper.setAspectRatio(4);
        layoutHelper.setMargin(10, 20, 10, 20);
        layoutHelper.setPadding(10, 10, 10, 10);
        adapters.add(new SubAdapter(this, layoutHelper, 1,
                new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
    }

    if (COLUMN_LAYOUT) {
        ColumnLayoutHelper layoutHelper = new ColumnLayoutHelper();
        layoutHelper.setBgColor(0xff00f0f0);
        layoutHelper.setWeights(new float[] { 40.0f, Float.NaN, 40 });
        adapters.add(new SubAdapter(this, layoutHelper, 5) {

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                if (position == 0) {
                    VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT, 300);
                    layoutParams.mAspectRatio = 4;
                    holder.itemView.setLayoutParams(layoutParams);
                }
            }

        });
    }

    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xff876384);
        helper.setAspectRatio(4.0f);
        helper.setColWeights(new float[] { 40f, 45f });
        helper.setMargin(10, 20, 10, 20);
        helper.setPadding(10, 10, 10, 10);
        adapters.add(new SubAdapter(this, helper, 2));
    }

    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xffef8ba3);
        helper.setAspectRatio(2.0f);
        helper.setColWeights(new float[] { 40f });
        helper.setRowWeight(30f);
        helper.setMargin(10, 20, 10, 20);
        helper.setPadding(10, 10, 10, 10);
        adapters.add(new SubAdapter(this, helper, 4) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                VirtualLayoutManager.LayoutParams lp = (VirtualLayoutManager.LayoutParams) holder.itemView
                        .getLayoutParams();
                if (position == 0) {
                    lp.rightMargin = 1;
                } else if (position == 1) {

                } else if (position == 2) {
                    lp.topMargin = 1;
                    lp.rightMargin = 1;
                }
            }
        });
    }

    if (ONEN_LAYOUT) {
        adapters.add(new SubAdapter(this, new OnePlusNLayoutHelper(), 0));
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xff87e543);
        helper.setAspectRatio(1.8f);
        helper.setColWeights(new float[] { 33.33f, 50f, 40f });
        helper.setMargin(10, 20, 10, 20);
        helper.setPadding(10, 10, 10, 10);
        VirtualLayoutManager.LayoutParams lp = new VirtualLayoutManager.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        adapters.add(new SubAdapter(this, helper, 3, lp) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                VirtualLayoutManager.LayoutParams lp = (VirtualLayoutManager.LayoutParams) holder.itemView
                        .getLayoutParams();
                if (position == 0) {
                    lp.rightMargin = 1;
                }
            }
        });
    }

    if (COLUMN_LAYOUT) {
        adapters.add(new SubAdapter(this, new ColumnLayoutHelper(), 0));
        adapters.add(new SubAdapter(this, new ColumnLayoutHelper(), 4));
    }

    if (FIX_LAYOUT) {
        FixLayoutHelper layoutHelper = new FixLayoutHelper(10, 10);
        adapters.add(new SubAdapter(this, layoutHelper, 0));

        layoutHelper = new FixLayoutHelper(FixLayoutHelper.TOP_RIGHT, 20, 20);

        adapters.add(new SubAdapter(this, layoutHelper, 1) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(200,
                        200);
                holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }

    if (STICKY_LAYOUT) {
        StickyLayoutHelper layoutHelper = new StickyLayoutHelper(false);
        adapters.add(new SubAdapter(this, layoutHelper, 0));
        layoutHelper = new StickyLayoutHelper(false);
        layoutHelper.setOffset(100);
        adapters.add(new SubAdapter(this, layoutHelper, 1,
                new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
    }

    if (GRID_LAYOUT) {
        GridLayoutHelper layoutHelper = new GridLayoutHelper(2);
        layoutHelper.setMargin(7, 0, 7, 0);
        layoutHelper.setWeights(new float[] { 46.665f });
        layoutHelper.setHGap(3);
        adapters.add(new SubAdapter(this, layoutHelper, 2));

        layoutHelper = new GridLayoutHelper(4);
        layoutHelper.setWeights(new float[] { 20f, 26.665f });
        layoutHelper.setMargin(7, 0, 7, 0);
        layoutHelper.setHGap(3);
        adapters.add(new SubAdapter(this, layoutHelper, 8));
    }

    if (GRID_LAYOUT) {
        adapters.add(new SubAdapter(this, new GridLayoutHelper(4), 0));

        GridLayoutHelper helper = new GridLayoutHelper(4);
        helper.setAspectRatio(4f);
        //helper.setColWeights(new float[]{40, 20, 30, 30});
        // helper.setMargin(0, 10, 0, 10);
        helper.setGap(10);
        adapters.add(new SubAdapter(this, helper, 80) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                VirtualLayoutManager.LayoutParams lp = (VirtualLayoutManager.LayoutParams) holder.itemView
                        .getLayoutParams();
                // lp.bottomMargin = 1;
                // lp.rightMargin = 1;
            }
        });
    }

    if (FIX_LAYOUT) {
        adapters.add(new SubAdapter(this, new ScrollFixLayoutHelper(20, 20), 1) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(200,
                        200);
                holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }

    if (LINEAR_LAYOUT)
        adapters.add(new SubAdapter(this, new LinearLayoutHelper(), 10));

    if (GRID_LAYOUT) {
        GridLayoutHelper helper = new GridLayoutHelper(3);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 3));
    }

    if (STAGGER_LAYOUT) {
        // adapters.add(new SubAdapter(this, new StaggeredGridLayoutHelper(2, 0), 0));
        final StaggeredGridLayoutHelper helper = new StaggeredGridLayoutHelper(2, 10);
        helper.setMargin(20, 10, 10, 10);
        helper.setPadding(10, 10, 20, 10);
        helper.setBgColor(0xFF86345A);
        adapters.add(new SubAdapter(this, helper, 27) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, 200);
                if (position % 2 == 0) {
                    layoutParams.mAspectRatio = 1.0f;
                } else {
                    layoutParams.height = 340 + position % 7 * 20;
                }
                holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }

    if (COLUMN_LAYOUT) {
        // adapters.add(new SubAdapter(this, new ColumnLayoutHelper(), 3));
    }

    if (GRID_LAYOUT) {
        // adapters.add(new SubAdapter(this, new GridLayoutHelper(4), 24));
    }

    delegateAdapter.setAdapters(adapters);

    final Handler mainHandler = new Handler(Looper.getMainLooper());

    trigger = new Runnable() {
        @Override
        public void run() {
            // recyclerView.scrollToPosition(22);
            // recyclerView.getAdapter().notifyDataSetChanged();
            recyclerView.requestLayout();
            // mainHandler.postDelayed(trigger, 1000);
        }
    };

    mainHandler.postDelayed(trigger, 1000);
}