Search in sources :

Example 1 with VirtualLayoutAdapter

use of com.alibaba.android.vlayout.VirtualLayoutAdapter in project vlayout by alibaba.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_view);
    VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    // layoutManager.setReverseLayout(true);
    recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {

        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.set(10, 10, 10, 10);
        }
    });
    final List<LayoutHelper> helpers = new LinkedList<>();
    final GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(4);
    gridLayoutHelper.setItemCount(25);
    final ScrollFixLayoutHelper scrollFixLayoutHelper = new ScrollFixLayoutHelper(FixLayoutHelper.TOP_RIGHT, 100, 100);
    helpers.add(DefaultLayoutHelper.newHelper(7));
    helpers.add(scrollFixLayoutHelper);
    helpers.add(DefaultLayoutHelper.newHelper(2));
    helpers.add(gridLayoutHelper);
    layoutManager.setLayoutHelpers(helpers);
    recyclerView.setAdapter(new VirtualLayoutAdapter(layoutManager) {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MainViewHolder(new TextView(MainActivity.this));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
            holder.itemView.setLayoutParams(layoutParams);
            ((TextView) holder.itemView).setText(Integer.toString(position));
            if (position == 7) {
                layoutParams.height = 60;
                layoutParams.width = 60;
            } else if (position > 35) {
                layoutParams.height = 200 + (position - 30) * 100;
            }
            if (position > 35) {
                holder.itemView.setBackgroundColor(0x66cc0000 + (position - 30) * 128);
            } else if (position % 2 == 0) {
                holder.itemView.setBackgroundColor(0xaa00ff00);
            } else {
                holder.itemView.setBackgroundColor(0xccff00ff);
            }
        }

        @Override
        public int getItemCount() {
            List<LayoutHelper> helpers = getLayoutHelpers();
            if (helpers == null) {
                return 0;
            }
            int count = 0;
            for (int i = 0, size = helpers.size(); i < size; i++) {
                count += helpers.get(i).getItemCount();
            }
            return count;
        }
    });
    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {

        @Override
        public void run() {
            recyclerView.scrollToPosition(7);
            recyclerView.getAdapter().notifyDataSetChanged();
        }
    }, 6000);
}
Also used : Rect(android.graphics.Rect) ScrollFixLayoutHelper(com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper) ViewGroup(android.view.ViewGroup) Handler(android.os.Handler) GridLayoutHelper(com.alibaba.android.vlayout.layout.GridLayoutHelper) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View) LinkedList(java.util.LinkedList) VirtualLayoutAdapter(com.alibaba.android.vlayout.VirtualLayoutAdapter) RecyclerView(android.support.v7.widget.RecyclerView) DefaultLayoutHelper(com.alibaba.android.vlayout.layout.DefaultLayoutHelper) LayoutHelper(com.alibaba.android.vlayout.LayoutHelper) FixLayoutHelper(com.alibaba.android.vlayout.layout.FixLayoutHelper) ScrollFixLayoutHelper(com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper) GridLayoutHelper(com.alibaba.android.vlayout.layout.GridLayoutHelper) TextView(android.widget.TextView) List(java.util.List) LinkedList(java.util.LinkedList) VirtualLayoutManager(com.alibaba.android.vlayout.VirtualLayoutManager)

Aggregations

Rect (android.graphics.Rect)1 Handler (android.os.Handler)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 TextView (android.widget.TextView)1 LayoutHelper (com.alibaba.android.vlayout.LayoutHelper)1 VirtualLayoutAdapter (com.alibaba.android.vlayout.VirtualLayoutAdapter)1 VirtualLayoutManager (com.alibaba.android.vlayout.VirtualLayoutManager)1 DefaultLayoutHelper (com.alibaba.android.vlayout.layout.DefaultLayoutHelper)1 FixLayoutHelper (com.alibaba.android.vlayout.layout.FixLayoutHelper)1 GridLayoutHelper (com.alibaba.android.vlayout.layout.GridLayoutHelper)1 ScrollFixLayoutHelper (com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1