Example usage for android.graphics Rect set

List of usage examples for android.graphics Rect set

Introduction

In this page you can find the example usage for android.graphics Rect set.

Prototype

public void set(int left, int top, int right, int bottom) 

Source Link

Document

Set the rectangle's coordinates to the specified values.

Usage

From source file:com.flexible.flexibleadapter.common.DividerItemDecoration.java

/**
 * @since 5.0.0-b4/*from   w  ww.ja  v a 2 s  .  co  m*/
 */
@SuppressWarnings({ "ConstantConditions", "unchecked", "SuspiciousNameCombination" })
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state) {
    int offset = (mDivider != null && withOffset ? mDivider.getIntrinsicHeight() : 0);
    if (mSectionOffset > 0 && recyclerView.getAdapter() instanceof FlexibleAdapter) {
        FlexibleAdapter flexibleAdapter = (FlexibleAdapter) recyclerView.getAdapter();
        int position = recyclerView.getChildAdapterPosition(view);

        //Only ISectionable items can finish with a gap and only if next item is a IHeader item
        if (flexibleAdapter.getItem(position) instanceof ISectionable
                && (flexibleAdapter.isHeader(flexibleAdapter.getItem(position + 1))
                        || position >= recyclerView.getAdapter().getItemCount() - 1)) {

            offset += mSectionOffset;
        }
    }
    if (Utils.getOrientation(recyclerView.getLayoutManager()) == RecyclerView.VERTICAL) {
        outRect.set(0, 0, 0, offset);
    } else {
        outRect.set(0, 0, offset, 0);
    }
}

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   w w w.j a v a 2s .  c o  m
        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.chromium.chrome.browser.compositor.CompositorViewHolder.java

/**
 * Note that the returned rect is reused for other calls.
 *//*ww  w  .  j  ava  2s  .  com*/
@Override
public Rect getVisibleViewport(Rect rect) {
    if (rect == null)
        rect = new Rect();
    rect.set(0, (int) mLastVisibleContentOffset, getWidth(), getHeight());
    return rect;
}

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

public void drawCompareImage(Canvas canvas, Bitmap image) {
    MasterImage master = MasterImage.getImage();
    boolean showsOriginal = master.showsOriginal();
    if (!showsOriginal && !mTouchShowOriginal)
        return;/*  w  w w  .ja  va2  s .  c o m*/
    canvas.save();
    if (image != null) {
        if (mShowOriginalDirection == 0) {
            if (Math.abs(mTouch.y - mTouchDown.y) > Math.abs(mTouch.x - mTouchDown.x)) {
                mShowOriginalDirection = UNVEIL_VERTICAL;
            } else {
                mShowOriginalDirection = UNVEIL_HORIZONTAL;
            }
        }

        int px = 0;
        int py = 0;
        if (mShowOriginalDirection == UNVEIL_VERTICAL) {
            px = mImageBounds.width();
            py = mTouch.y - mImageBounds.top;
        } else {
            px = mTouch.x - mImageBounds.left;
            py = mImageBounds.height();
            if (showsOriginal) {
                px = mImageBounds.width();
            }
        }

        Rect d = new Rect(mImageBounds.left, mImageBounds.top, mImageBounds.left + px, mImageBounds.top + py);
        if (mShowOriginalDirection == UNVEIL_HORIZONTAL) {
            if (mTouchDown.x - mTouch.x > 0) {
                d.set(mImageBounds.left + px, mImageBounds.top, mImageBounds.right, mImageBounds.top + py);
            }
        } else {
            if (mTouchDown.y - mTouch.y > 0) {
                d.set(mImageBounds.left, mImageBounds.top + py, mImageBounds.left + px, mImageBounds.bottom);
            }
        }
        canvas.clipRect(d);
        Matrix m = master.computeImageToScreen(image, 0, false);
        canvas.drawBitmap(image, m, mPaint);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);

        if (mShowOriginalDirection == UNVEIL_VERTICAL) {
            canvas.drawLine(mImageBounds.left, mTouch.y, mImageBounds.right, mTouch.y, paint);
        } else {
            canvas.drawLine(mTouch.x, mImageBounds.top, mTouch.x, mImageBounds.bottom, paint);
        }

        Rect bounds = new Rect();
        paint.setAntiAlias(true);
        paint.setTextSize(mOriginalTextSize);
        paint.getTextBounds(mOriginalText, 0, mOriginalText.length(), bounds);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        canvas.drawText(mOriginalText, mImageBounds.left + mOriginalTextMargin,
                mImageBounds.top + bounds.height() + mOriginalTextMargin, paint);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        paint.setColor(Color.WHITE);
        canvas.drawText(mOriginalText, mImageBounds.left + mOriginalTextMargin,
                mImageBounds.top + bounds.height() + mOriginalTextMargin, paint);
    }
    canvas.restore();
}

From source file:com.example.rx.ui.VLayoutActivity.java

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

    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    ;/*from   ww  w  . j  a v  a2  s . c o  m*/
    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
        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)));
    }

    {
        RangeGridLayoutHelper layoutHelper = new RangeGridLayoutHelper(4);
        layoutHelper.setBgColor(Color.GREEN);
        layoutHelper.setWeights(new float[] { 20f, 26.665f });
        layoutHelper.setPadding(15, 15, 15, 15);
        layoutHelper.setMargin(15, 15, 15, 15);
        layoutHelper.setHGap(10);
        layoutHelper.setVGap(10);
        GridRangeStyle rangeStyle = new GridRangeStyle();
        rangeStyle.setBgColor(Color.RED);
        rangeStyle.setSpanCount(2);
        rangeStyle.setWeights(new float[] { 46.665f });
        rangeStyle.setPadding(15, 15, 15, 15);
        rangeStyle.setMargin(15, 15, 15, 15);
        rangeStyle.setHGap(5);
        rangeStyle.setVGap(5);
        layoutHelper.addRangeStyle(4, 7, rangeStyle);
        GridRangeStyle rangeStyle1 = new GridRangeStyle();
        rangeStyle1.setBgColor(Color.YELLOW);
        rangeStyle1.setSpanCount(2);
        rangeStyle1.setWeights(new float[] { 46.665f });
        rangeStyle1.setPadding(15, 15, 15, 15);
        rangeStyle1.setMargin(15, 15, 15, 15);
        rangeStyle1.setHGap(5);
        rangeStyle1.setVGap(5);
        layoutHelper.addRangeStyle(8, 11, rangeStyle1);
        adapters.add(new SubAdapter(this, layoutHelper, 16));

    }

    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.addAdapter(0, new SubAdapter(VLayoutActivity.this, new ColumnLayoutHelper(), 3));
            //delegateAdapter.addAdapter(1, new SubAdapter(VLayoutActivity.this, new GridLayoutHelper(4), 24));
            //delegateAdapter.notifyDataSetChanged();
        }
    };

    mainHandler.postDelayed(trigger, 1000);

    mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            mainHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }, 2000L);
        }
    });
    setListenerToRootView();
}

From source file:com.example.android.supportv7.widget.vlayout.VLayoutActivity.java

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

    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    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   w  ww .  j a v  a2  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());
        }
    });

    //layoutManager.setRecycleOffset(300);

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

    {
        SingleLayoutHelper layoutHelper = new SingleLayoutHelper();
        layoutHelper.setBgColor(Color.BLUE);
        layoutHelper.setMargin(0, 30, 0, 200);
        adapters.add(new SubAdapter(this, layoutHelper, 1,
                new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
    }

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

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

        final GridLayoutHelper helper = new GridLayoutHelper(3, 4);
        helper.setBgColor(0xFF86345A);
        adapters.add(new SubAdapter(this, helper, 4) {
            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
                holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }

    {
        RangeGridLayoutHelper layoutHelper = new RangeGridLayoutHelper(4);
        layoutHelper.setBgColor(Color.GREEN);
        layoutHelper.setWeights(new float[] { 20f, 26.665f });
        layoutHelper.setPadding(15, 15, 15, 15);
        layoutHelper.setMargin(15, 15, 15, 15);
        layoutHelper.setHGap(10);
        layoutHelper.setVGap(10);
        GridRangeStyle rangeStyle = new GridRangeStyle();
        rangeStyle.setBgColor(Color.RED);
        rangeStyle.setSpanCount(2);
        rangeStyle.setWeights(new float[] { 46.665f });
        rangeStyle.setPadding(15, 15, 15, 15);
        rangeStyle.setMargin(15, 15, 15, 15);
        rangeStyle.setHGap(5);
        rangeStyle.setVGap(5);
        layoutHelper.addRangeStyle(4, 7, rangeStyle);
        GridRangeStyle rangeStyle1 = new GridRangeStyle();
        rangeStyle1.setBgColor(Color.YELLOW);
        rangeStyle1.setSpanCount(2);
        rangeStyle1.setWeights(new float[] { 46.665f });
        rangeStyle1.setPadding(15, 15, 15, 15);
        rangeStyle1.setMargin(15, 15, 15, 15);
        rangeStyle1.setHGap(5);
        rangeStyle1.setVGap(5);
        layoutHelper.addRangeStyle(8, 11, rangeStyle1);
        adapters.add(new SubAdapter(this, layoutHelper, 16));

    }

    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(ViewGroup.LayoutParams.MATCH_PARENT, 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.addAdapter(0, new SubAdapter(VLayoutActivity.this, new ColumnLayoutHelper(), 3));
            //delegateAdapter.addAdapter(1, new SubAdapter(VLayoutActivity.this, new GridLayoutHelper(4), 24));
            //delegateAdapter.notifyDataSetChanged();
        }
    };

    mainHandler.postDelayed(trigger, 1000);

    mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            mainHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }, 2000L);
        }
    });
    setListenerToRootView();
}

From source file:org.chromium.chrome.browser.ntp.NewTabPageView.java

/**
 * Get the bounds of the search box in relation to the top level NewTabPage view.
 *
 * @param bounds The current drawing location of the search box.
 * @param translation The translation applied to the search box by the parent view hierarchy up
 *                    to the NewTabPage view.
 *//*from   ww w  . j av a2s.c  om*/
void getSearchBoxBounds(Rect bounds, Point translation) {
    int searchBoxX = (int) mSearchBoxView.getX();
    int searchBoxY = (int) mSearchBoxView.getY();

    bounds.set(searchBoxX + mSearchBoxView.getPaddingLeft(), searchBoxY + mSearchBoxView.getPaddingTop(),
            searchBoxX + mSearchBoxView.getWidth() - mSearchBoxView.getPaddingRight(),
            searchBoxY + mSearchBoxView.getHeight() - mSearchBoxView.getPaddingBottom());

    translation.set(0, 0);

    View view = mSearchBoxView;
    while (true) {
        view = (View) view.getParent();
        if (view == null) {
            // The |mSearchBoxView| is not a child of this view. This can happen if the
            // RecyclerView detaches the NewTabPageLayout after it has been scrolled out of
            // view. Set the translation to the minimum Y value as an approximation.
            translation.y = Integer.MIN_VALUE;
            break;
        }
        translation.offset(-view.getScrollX(), -view.getScrollY());
        if (view == this)
            break;
        translation.offset((int) view.getX(), (int) view.getY());
    }
    bounds.offset(translation.x, translation.y);
}

From source file:com.example.mylibrary.utils.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}//w w  w.j av  a 2  s.co  m
 */
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view);
    int headerHeight = 0;

    if (!mIncludeHeader) {
        if (parent.getAdapter() instanceof RecyclerArrayAdapter) {
            int headerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getHeaderCount();
            int footerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getFooterCount();
            int dataCount = ((RecyclerArrayAdapter) parent.getAdapter()).getCount();
            if (position < headerCount) {
                return;
            }
            if (position >= headerCount + dataCount) {
                return;
            }
            if (position >= headerCount) {
                position -= headerCount;
            }

        }
    }

    if (position != RecyclerView.NO_POSITION && hasHeader(position) && showHeaderAboveItem(position)) {

        View header = getHeader(parent, position).itemView;
        headerHeight = getHeaderHeightForLayout(header);
    }

    outRect.set(0, headerHeight, 0, 0);
}

From source file:com.youth.xframe.adapter.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}/*from ww w  .  j  a v  a2  s .  c om*/
 */
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view);
    int headerHeight = 0;

    if (!mIncludeHeader) {
        if (parent.getAdapter() instanceof XRecyclerViewAdapter) {
            int headerCount = ((XRecyclerViewAdapter) parent.getAdapter()).getHeaderCount();
            int footerCount = ((XRecyclerViewAdapter) parent.getAdapter()).getFooterCount();
            int dataCount = ((XRecyclerViewAdapter) parent.getAdapter()).getDataCount();
            if (position < headerCount) {
                return;
            }
            if (position >= headerCount + dataCount) {
                return;
            }
            if (position >= headerCount) {
                position -= headerCount;
            }

        }
    }

    if (position != RecyclerView.NO_POSITION && hasHeader(position) && showHeaderAboveItem(position)) {

        View header = getHeader(parent, position).itemView;
        headerHeight = getHeaderHeightForLayout(header);
    }

    outRect.set(0, headerHeight, 0, 0);
}

From source file:com.sanjie.zy.adpter.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}/*from  w w  w.  jav  a2 s.  com*/
 */
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view);
    int headerHeight = 0;

    if (!mIncludeHeader) {
        if (parent.getAdapter() instanceof ZYRecyclerViewAdapter) {
            int headerCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getHeaderCount();
            int footerCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getFooterCount();
            int dataCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getDataCount();
            if (position < headerCount) {
                return;
            }
            if (position >= headerCount + dataCount) {
                return;
            }
            if (position >= headerCount) {
                position -= headerCount;
            }

        }
    }

    if (position != RecyclerView.NO_POSITION && hasHeader(position) && showHeaderAboveItem(position)) {

        View header = getHeader(parent, position).itemView;
        headerHeight = getHeaderHeightForLayout(header);
    }

    outRect.set(0, headerHeight, 0, 0);
}