手动放置RecyclerView StaggeredGrid项目

时间:2015-05-20 15:25:43

标签: android gridview android-recyclerview

我试图在RecyclerView中使用StaggeredGridLayoutManager,我希望能够手动设置适配器项应该使用哪些列和行,但问题是它完全依赖于接收项目的顺序以及在展示位置方面有点随机,这就是我想要做的事情。enter image description here

这是我的适配器:

class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ContactViewHolder>{

    List<PromotionList> list;

    public RecyclerViewAdapter(List<PromotionList> list){
        this.list = list;
    }

    @Override
    public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_banner, null);

        return new ContactViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ContactViewHolder holder, int position) {

        PromotionList listItem = list.get(position);

        Picasso.with(getActivity())
                .load(listItem.getImageURL())
                .into(holder.promo);
        }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ContactViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.div)
        ImageView promo;

       public ContactViewHolder(View v){
            super(v);
            ButterKnife.inject(this, v);
        }
    }
}

这是我的RecyclerView:

rv.setHasFixedSize(true);
rv.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
rv.setAdapter(new RecyclerViewAdapter(list));

1 个答案:

答案 0 :(得分:0)

SGLM仅支持1个跨度或全量程项目。我们计划支持任意跨度大小。仅供参考,GridLayoutManager支持此功能(您的示例布局看起来像网格而不是交错网格)。