RecyclerView列表布局,瀑布流(多规格布局)

1,列表布局

//设置布局管理器 

recyclerview.setLayoutManager(new GridLayoutManager(context, 2));

recyclerview.addItemDecoration(new RecyclerGridDecoration(context));

//设置Adapter

@Override

public int getItemViewType(int position) {

if (position ==0) {

return HEADER;

    }else {

return ITEM;

    }

}

@Override

public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {

RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();

    if (managerinstanceof GridLayoutManager) {

final GridLayoutManager gridManager = ((GridLayoutManager) manager);

        gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

@Override

            public int getSpanSize(int position) {

int type = getItemViewType(position);

                switch (type) {//设置每种类型的列数

case ITEM:

case HEADER:

return 1;

                    default:

return 2;

                }

}

});

    }

}

1,瀑布流布局

//设置布局管理器 

StaggeredGridLayoutManager manager =new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

//        manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

//        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {

//            @Override

//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

//                super.onScrollStateChanged(recyclerView, newState);

//                manager.invalidateSpanAssignments(); //防止第一行到顶部有空白区域

//            }

//        });

        recyclerview.setLayoutManager(manager);

@NonNull

@Override

public RecyclerView.ViewHolderonCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

if (viewType ==HEADER) {

View view = LayoutInflater.from(context).inflate(R.layout.holder_main_home_viewpager, parent, false);

//设置头部宽度满屏

 StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();

        params.setFullSpan(true);

        return new HeaderHolder(view);

    }else {

View view = LayoutInflater.from(context).inflate(R.layout.holder_home_goods, parent, false);

        return new ViewHolder(view);

    }

}

//设置图片高度

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.img.getLayoutParams();

你可能感兴趣的:(RecyclerView列表布局,瀑布流(多规格布局))