Search in sources :

Example 1 with LayoutParams

use of com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams in project vlayout by alibaba.

the class VLayoutActivity method onCreate.

@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
        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);
    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));
                }
            }
        });
    }
    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);
                }
            }
        });
    }
    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();
            recyclerView.requestLayout();
        // mainHandler.postDelayed(trigger, 1000);
        }
    };
    mainHandler.postDelayed(trigger, 1000);
}
Also used : ScrollFixLayoutHelper(com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper) FixLayoutHelper(com.alibaba.android.vlayout.layout.FixLayoutHelper) ScrollFixLayoutHelper(com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper) DelegateAdapter(com.alibaba.android.vlayout.DelegateAdapter) RecyclablePagerAdapter(com.alibaba.android.vlayout.RecyclablePagerAdapter) ColumnLayoutHelper(com.alibaba.android.vlayout.layout.ColumnLayoutHelper) StaggeredGridLayoutHelper(com.alibaba.android.vlayout.layout.StaggeredGridLayoutHelper) GridLayoutHelper(com.alibaba.android.vlayout.layout.GridLayoutHelper) RecyclablePagerAdapter(com.alibaba.android.vlayout.RecyclablePagerAdapter) DelegateAdapter(com.alibaba.android.vlayout.DelegateAdapter) SingleLayoutHelper(com.alibaba.android.vlayout.layout.SingleLayoutHelper) StickyLayoutHelper(com.alibaba.android.vlayout.layout.StickyLayoutHelper) EditText(android.widget.EditText) Rect(android.graphics.Rect) LayoutParams(com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams) LinearLayoutHelper(com.alibaba.android.vlayout.layout.LinearLayoutHelper) ViewGroup(android.view.ViewGroup) Handler(android.os.Handler) OnePlusNLayoutHelper(com.alibaba.android.vlayout.layout.OnePlusNLayoutHelper) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) ViewPager(android.support.v4.view.ViewPager) LinkedList(java.util.LinkedList) RecyclerView(android.support.v7.widget.RecyclerView) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) StaggeredGridLayoutHelper(com.alibaba.android.vlayout.layout.StaggeredGridLayoutHelper) FloatLayoutHelper(com.alibaba.android.vlayout.layout.FloatLayoutHelper)

Example 2 with LayoutParams

use of com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams in project vlayout by alibaba.

the class OnePlusNLayoutActivity method onCreate.

@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
        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.setOnScrollListener(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);
    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(OnePlusNLayoutActivity.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));
                }
            }
        });
    }
    if (GRID_LAYOUT) {
        GridLayoutHelper layoutHelper;
        layoutHelper = new GridLayoutHelper(4);
        layoutHelper.setMargin(0, 10, 0, 10);
        layoutHelper.setHGap(3);
        layoutHelper.setAspectRatio(4f);
        adapters.add(new SubAdapter(this, layoutHelper, 8));
    }
    if (HORIZONTAL_SCROLL_LAYOUT) {
    }
    if (GRID_LAYOUT) {
        GridLayoutHelper layoutHelper;
        layoutHelper = new GridLayoutHelper(2);
        layoutHelper.setMargin(0, 10, 0, 10);
        layoutHelper.setHGap(3);
        layoutHelper.setAspectRatio(3f);
        adapters.add(new SubAdapter(this, layoutHelper, 2));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xff876384);
        helper.setMargin(10, 10, 10, 10);
        helper.setPadding(10, 10, 10, 10);
        adapters.add(new SubAdapter(this, helper, 3) {

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
            //                    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
            //                    layoutParams.leftMargin = 10;
            //                    layoutParams.topMargin = 10;
            //                    layoutParams.rightMargin = 10;
            //                    layoutParams.bottomMargin = 10;
            //                    holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 4));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 5));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 5));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        helper.setColWeights(new float[] { 40f, 45f, 15f, 60f, 0f });
        adapters.add(new SubAdapter(this, helper, 5));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        helper.setColWeights(new float[] { 20f, 80f, 0f, 60f, 20f });
        helper.setAspectRatio(4);
        adapters.add(new SubAdapter(this, helper, 5));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 6));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        adapters.add(new SubAdapter(this, helper, 7));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xff876384);
        helper.setMargin(0, 10, 0, 10);
        helper.setColWeights(new float[] { 40f, 45f, 15f, 60f, 0f, 30f, 30f });
        adapters.add(new SubAdapter(this, helper, 7));
    }
    if (ONEN_LAYOUT) {
        OnePlusNLayoutHelperEx helper = new OnePlusNLayoutHelperEx();
        helper.setBgColor(0xffed7612);
        //            helper.setMargin(10, 10, 10, 10);
        //            helper.setPadding(10, 10, 10, 10);
        helper.setColWeights(new float[] { 30f, 20f, 50f, 40f, 30f, 35f, 35f });
        adapters.add(new SubAdapter(this, helper, 7) {

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
            //                    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
            //                    layoutParams.leftMargin = 10;
            //                    layoutParams.topMargin = 10;
            //                    layoutParams.rightMargin = 10;
            //                    layoutParams.bottomMargin = 10;
            //                    holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }
    if (STICKY_LAYOUT) {
        StickyLayoutHelper layoutHelper = new StickyLayoutHelper();
        layoutHelper.setAspectRatio(4);
        adapters.add(new SubAdapter(this, layoutHelper, 1, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
    }
    if (SCROLL_FIX_LAYOUT) {
        ScrollFixLayoutHelper layoutHelper = new ScrollFixLayoutHelper(FixLayoutHelper.BOTTOM_RIGHT, 20, 20);
        layoutHelper.setShowType(ScrollFixLayoutHelper.SHOW_ON_LEAVE);
        adapters.add(new SubAdapter(this, layoutHelper, 1) {

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                LayoutParams layoutParams = new LayoutParams(50, 50);
                holder.itemView.setLayoutParams(layoutParams);
            }
        });
    }
    if (LINEAR_LAYOUT)
        adapters.add(new SubAdapter(this, new LinearLayoutHelper(), 100));
    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);
}
Also used : ScrollFixLayoutHelper(com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper) DelegateAdapter(com.alibaba.android.vlayout.DelegateAdapter) RecyclablePagerAdapter(com.alibaba.android.vlayout.RecyclablePagerAdapter) GridLayoutHelper(com.alibaba.android.vlayout.layout.GridLayoutHelper) RecyclablePagerAdapter(com.alibaba.android.vlayout.RecyclablePagerAdapter) DelegateAdapter(com.alibaba.android.vlayout.DelegateAdapter) StickyLayoutHelper(com.alibaba.android.vlayout.layout.StickyLayoutHelper) OnePlusNLayoutHelperEx(com.alibaba.android.vlayout.layout.OnePlusNLayoutHelperEx) EditText(android.widget.EditText) Rect(android.graphics.Rect) LayoutParams(com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams) LinearLayoutHelper(com.alibaba.android.vlayout.layout.LinearLayoutHelper) ViewGroup(android.view.ViewGroup) Handler(android.os.Handler) OnePlusNLayoutHelper(com.alibaba.android.vlayout.layout.OnePlusNLayoutHelper) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) ViewPager(android.support.v4.view.ViewPager) LinkedList(java.util.LinkedList) RecyclerView(android.support.v7.widget.RecyclerView) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager)

Example 3 with LayoutParams

use of com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams in project vlayout by alibaba.

the class GridLayoutHelper method layoutViews.

@Override
public void layoutViews(RecyclerView.Recycler recycler, RecyclerView.State state, LayoutStateWrapper layoutState, LayoutChunkResult result, LayoutManagerHelper helper) {
    // reach the end of this layout
    if (isOutOfRange(layoutState.getCurrentPosition())) {
        return;
    }
    boolean isStartLine = false, isEndLine = false;
    final int currentPosition = layoutState.getCurrentPosition();
    final int itemDirection = layoutState.getItemDirection();
    final boolean layingOutInPrimaryDirection = itemDirection == LayoutStateWrapper.ITEM_DIRECTION_TAIL;
    OrientationHelper orientationHelper = helper.getMainOrientationHelper();
    final boolean layoutInVertical = helper.getOrientation() == VERTICAL;
    if (layoutInVertical) {
        mTotalSize = helper.getContentWidth() - helper.getPaddingRight() - helper.getPaddingLeft() - getHorizontalMargin() - getHorizontalPadding();
        mSizePerSpan = (int) ((mTotalSize - (mSpanCount - 1) * mHGap) * 1.0f / mSpanCount + 0.5f);
    } else {
        mTotalSize = helper.getContentHeight() - helper.getPaddingBottom() - helper.getPaddingTop() - getVerticalMargin() - getVerticalPadding();
        mSizePerSpan = (int) ((mTotalSize - (mSpanCount - 1) * mVGap) * 1.0f / mSpanCount + 0.5f);
    }
    int count = 0;
    int consumedSpanCount = 0;
    int remainingSpan = mSpanCount;
    ensureSpanCount();
    if (!layingOutInPrimaryDirection) {
        // fill the remaining spacing this row
        int itemSpanIndex = getSpanIndex(recycler, state, layoutState.getCurrentPosition());
        int itemSpanSize = getSpanSize(recycler, state, layoutState.getCurrentPosition());
        remainingSpan = itemSpanIndex + itemSpanSize;
        // should find the last element of this row
        if (itemSpanIndex != mSpanCount - 1) {
            int index = layoutState.getCurrentPosition();
            int revRemainingSpan = mSpanCount - remainingSpan;
            while (count < mSpanCount && revRemainingSpan > 0) {
                // go reverse direction to find views fill current row
                index -= itemDirection;
                if (isOutOfRange(index)) {
                    break;
                }
                final int spanSize = getSpanSize(recycler, state, index);
                if (spanSize > mSpanCount) {
                    throw new IllegalArgumentException("Item at position " + index + " requires " + spanSize + " spans but GridLayoutManager has only " + mSpanCount + " spans.");
                }
                View view = layoutState.retrieve(recycler, index);
                if (view == null)
                    break;
                if (!isStartLine) {
                    isStartLine = helper.getReverseLayout() ? index == getRange().getUpper() : index == getRange().getLower();
                }
                if (!isEndLine) {
                    isEndLine = helper.getReverseLayout() ? index == getRange().getLower() : index == getRange().getUpper();
                }
                revRemainingSpan -= spanSize;
                if (revRemainingSpan < 0)
                    break;
                consumedSpanCount += spanSize;
                mSet[count] = view;
                count++;
            }
            if (count > 0) {
                // reverse array
                int s = 0, e = count - 1;
                while (s < e) {
                    View temp = mSet[s];
                    mSet[s] = mSet[e];
                    mSet[e] = temp;
                    s++;
                    e--;
                }
            }
        }
    }
    while (count < mSpanCount && layoutState.hasMore(state) && remainingSpan > 0) {
        int pos = layoutState.getCurrentPosition();
        if (isOutOfRange(pos)) {
            if (DEBUG)
                Log.d(TAG, "pos [" + pos + "] is out of range");
            break;
        }
        final int spanSize = getSpanSize(recycler, state, pos);
        if (spanSize > mSpanCount) {
            throw new IllegalArgumentException("Item at position " + pos + " requires " + spanSize + " spans but GridLayoutManager has only " + mSpanCount + " spans.");
        }
        remainingSpan -= spanSize;
        if (remainingSpan < 0) {
            // item did not fit into this row or column
            break;
        }
        View view = layoutState.next(recycler);
        if (view == null) {
            break;
        }
        if (!isStartLine) {
            isStartLine = helper.getReverseLayout() ? pos == getRange().getUpper().intValue() : pos == getRange().getLower().intValue();
        }
        if (!isEndLine) {
            isEndLine = helper.getReverseLayout() ? pos == getRange().getLower().intValue() : pos == getRange().getUpper().intValue();
        }
        consumedSpanCount += spanSize;
        mSet[count] = view;
        count++;
    }
    if (count == 0) {
        return;
    }
    int maxSize = 0;
    // we should assign spans before item decor offsets are calculated
    assignSpans(recycler, state, count, consumedSpanCount, layingOutInPrimaryDirection, helper);
    if (remainingSpan > 0 && mIsAutoExpand) {
        if (layoutInVertical) {
            mSizePerSpan = (helper.getContentWidth() - helper.getPaddingRight() - getHorizontalMargin() - getHorizontalPadding() - helper.getPaddingLeft() - (count - 1) * mHGap) / count;
        } else {
            mSizePerSpan = (helper.getContentHeight() - helper.getPaddingBottom() - getVerticalMargin() - getVerticalPadding() - helper.getPaddingTop() - (count - 1) * mVGap) / count;
        }
    }
    boolean weighted = false;
    if (mWeights != null && mWeights.length > 0) {
        weighted = true;
        int totalSpace;
        if (layoutInVertical) {
            totalSpace = helper.getContentWidth() - helper.getPaddingLeft() - helper.getPaddingRight() - getHorizontalMargin() - getHorizontalPadding() - (count - 1) * mHGap;
        } else {
            totalSpace = helper.getContentHeight() - helper.getPaddingTop() - helper.getPaddingBottom() - getVerticalMargin() - getVerticalPadding() - (count - 1) * mVGap;
        }
        // calculate width with weight in percentage
        int eqCnt = 0, remainingSpace = totalSpace;
        int colCnt = (remainingSpan > 0 && mIsAutoExpand) ? count : mSpanCount;
        for (int i = 0; i < colCnt; i++) {
            if (i < mWeights.length && !Float.isNaN(mWeights[i]) && mWeights[i] >= 0) {
                float weight = mWeights[i];
                mSpanCols[i] = (int) (weight * 1.0f / 100 * totalSpace + 0.5f);
                remainingSpace -= mSpanCols[i];
            } else {
                eqCnt++;
                mSpanCols[i] = -1;
            }
        }
        if (eqCnt > 0) {
            int eqLength = remainingSpace / eqCnt;
            for (int i = 0; i < colCnt; i++) {
                if (mSpanCols[i] < 0) {
                    mSpanCols[i] = eqLength;
                }
            }
        }
    }
    for (int i = 0; i < count; i++) {
        View view = mSet[i];
        helper.addChildView(layoutState, view, layingOutInPrimaryDirection ? -1 : 0);
        int spanSize = getSpanSize(recycler, state, helper.getPosition(view)), spec;
        if (weighted) {
            final int index = mSpanIndices[i];
            int spanLength = 0;
            for (int j = 0; j < spanSize; j++) {
                spanLength += mSpanCols[j + index];
            }
            spec = View.MeasureSpec.makeMeasureSpec(Math.max(0, spanLength), View.MeasureSpec.EXACTLY);
        } else {
            spec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan * spanSize + Math.max(0, spanSize - 1) * (layoutInVertical ? mHGap : mVGap), View.MeasureSpec.EXACTLY);
        }
        final VirtualLayoutManager.LayoutParams lp = (VirtualLayoutManager.LayoutParams) view.getLayoutParams();
        if (helper.getOrientation() == VERTICAL) {
            helper.measureChild(view, spec, getMainDirSpec(lp.height, mTotalSize, View.MeasureSpec.getSize(spec), lp.mAspectRatio));
        } else {
            helper.measureChild(view, getMainDirSpec(lp.width, mTotalSize, View.MeasureSpec.getSize(spec), lp.mAspectRatio), View.MeasureSpec.getSize(spec));
        }
        final int size = orientationHelper.getDecoratedMeasurement(view);
        if (size > maxSize) {
            maxSize = size;
        }
    }
    // views that did not measure the maxSize has to be re-measured
    final int maxMeasureSpec = getMainDirSpec(maxSize, mTotalSize, 0, Float.NaN);
    for (int i = 0; i < count; i++) {
        final View view = mSet[i];
        if (orientationHelper.getDecoratedMeasurement(view) != maxSize) {
            int spanSize = getSpanSize(recycler, state, helper.getPosition(view)), spec;
            if (weighted) {
                final int index = mSpanIndices[i];
                int spanLength = 0;
                for (int j = 0; j < spanSize; j++) {
                    spanLength += mSpanCols[j + index];
                }
                spec = View.MeasureSpec.makeMeasureSpec(Math.max(0, spanLength), View.MeasureSpec.EXACTLY);
            } else {
                spec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan * spanSize + Math.max(0, spanSize - 1) * (layoutInVertical ? mHGap : mVGap), View.MeasureSpec.EXACTLY);
            }
            if (helper.getOrientation() == VERTICAL) {
                helper.measureChild(view, spec, maxMeasureSpec);
            } else {
                helper.measureChild(view, maxMeasureSpec, spec);
            }
        }
    }
    int startSpace = 0, endSpace = 0;
    if (isStartLine) {
        startSpace = layoutInVertical ? mMarginTop + mPaddingTop : mMarginLeft + mPaddingLeft;
    }
    if (isEndLine) {
        endSpace = layoutInVertical ? mMarginBottom + mPaddingBottom : mMarginRight + mPaddingRight;
    }
    result.mConsumed = maxSize + startSpace + endSpace;
    final boolean layoutStart = layoutState.getLayoutDirection() == LayoutStateWrapper.LAYOUT_START;
    if (!mLayoutWithAnchor && (!isEndLine || !layoutStart) && (!isStartLine || layoutStart)) {
        result.mConsumed += (layoutInVertical ? mVGap : mHGap);
    }
    int left = 0, right = 0, top = 0, bottom = 0;
    if (layoutInVertical) {
        if (layoutState.getLayoutDirection() == LayoutStateWrapper.LAYOUT_START) {
            bottom = layoutState.getOffset() - endSpace - ((mLayoutWithAnchor || isEndLine) ? 0 : mVGap);
            top = bottom - maxSize;
        } else {
            top = layoutState.getOffset() + startSpace + ((mLayoutWithAnchor || isStartLine) ? 0 : mVGap);
            bottom = top + maxSize;
        }
    } else {
        if (layoutState.getLayoutDirection() == LayoutStateWrapper.LAYOUT_START) {
            right = layoutState.getOffset() - endSpace - (mLayoutWithAnchor || isEndLine ? 0 : mHGap);
            left = right - maxSize;
        } else {
            left = layoutState.getOffset() + startSpace + (mLayoutWithAnchor || isStartLine ? 0 : mHGap);
            right = left + maxSize;
        }
    }
    for (int i = 0; i < count; i++) {
        View view = mSet[i];
        final int index = mSpanIndices[i];
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        if (layoutInVertical) {
            if (weighted) {
                left = helper.getPaddingLeft() + mMarginLeft + mPaddingLeft;
                for (int j = 0; j < index; j++) left += mSpanCols[j] + mHGap;
            } else
                left = helper.getPaddingLeft() + mMarginLeft + mPaddingLeft + mSizePerSpan * index + index * mHGap;
            right = left + orientationHelper.getDecoratedMeasurementInOther(view);
        } else {
            if (weighted) {
                top = helper.getPaddingTop() + mMarginTop + mPaddingTop;
                for (int j = 0; j < index; j++) top += mSpanCols[j] + mVGap;
            } else
                top = helper.getPaddingTop() + mMarginTop + mPaddingTop + mSizePerSpan * index + index * mVGap;
            bottom = top + orientationHelper.getDecoratedMeasurementInOther(view);
        }
        if (DEBUG) {
            Log.d(TAG, "layout item in position: " + params.getViewPosition() + " with text " + ((TextView) view).getText() + " with SpanIndex: " + index + " into (" + left + ", " + top + ", " + right + ", " + bottom + " )");
        }
        // We calculate everything with View's bounding box (which includes decor and margins)
        // To calculate correct layout position, we subtract margins.
        // modified by huifeng at 20160907, margins are already subtracted
        layoutChild(view, left, top, right, bottom, helper);
        // Consume the available space if the view is not removed OR changed
        if (params.isItemRemoved() || params.isItemChanged()) {
            result.mIgnoreConsumed = true;
        }
        result.mFocusable |= view.isFocusable();
    }
    mLayoutWithAnchor = false;
    Arrays.fill(mSet, null);
    Arrays.fill(mSpanIndices, 0);
    Arrays.fill(mSpanCols, 0);
}
Also used : LayoutParams(com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams) OrientationHelper(android.support.v7.widget.OrientationHelper) LayoutParams(com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)3 View (android.view.View)3 TextView (android.widget.TextView)3 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)3 LayoutParams (com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams)3 Rect (android.graphics.Rect)2 Handler (android.os.Handler)2 ViewPager (android.support.v4.view.ViewPager)2 ViewGroup (android.view.ViewGroup)2 EditText (android.widget.EditText)2 DelegateAdapter (com.alibaba.android.vlayout.DelegateAdapter)2 RecyclablePagerAdapter (com.alibaba.android.vlayout.RecyclablePagerAdapter)2 GridLayoutHelper (com.alibaba.android.vlayout.layout.GridLayoutHelper)2 LinearLayoutHelper (com.alibaba.android.vlayout.layout.LinearLayoutHelper)2 OnePlusNLayoutHelper (com.alibaba.android.vlayout.layout.OnePlusNLayoutHelper)2 ScrollFixLayoutHelper (com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper)2 StickyLayoutHelper (com.alibaba.android.vlayout.layout.StickyLayoutHelper)2 LinkedList (java.util.LinkedList)2 OrientationHelper (android.support.v7.widget.OrientationHelper)1 ColumnLayoutHelper (com.alibaba.android.vlayout.layout.ColumnLayoutHelper)1