0

我正在创建一个照片库之类的东西,并且正在为这个交错列表使用 RecyclerView。

左边它应该看起来的样子,右边它现在看起来的样子。

在此处输入图像描述在此处输入图像描述

这就是我的代码的样子。

调用适配器的代码:

StaggeredGridLayoutManager lm = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(lm);
recyclerViewAdapter = new CustomRecyclerViewAdapter(imagesList);
recyclerView.setAdapter(recyclerViewAdapter);

适配器 onCreateViewHolder 函数:

final View itemView;
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_image_thumbnail, parent, false);
itemView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        final int vt = viewType;
        final ViewGroup.LayoutParams lp = itemView.getLayoutParams();
        if (lp instanceof StaggeredGridLayoutManager.LayoutParams) {
            StaggeredGridLayoutManager.LayoutParams sglp = (StaggeredGridLayoutManager.LayoutParams) lp;
            switch (vt) {
                case TYPE_LARGE:
                    sglp.setFullSpan(false);
                    sglp.width = 2*(itemView.getWidth());
                    sglp.height = sglp.width;
                    break;
                case TYPE_SMALL:
                    sglp.setFullSpan(false);
                    sglp.width = (itemView.getWidth());
                    sglp.height = sglp.width;
                    break;
            }
            itemView.setLayoutParams(sglp);
            final StaggeredGridLayoutManager lm = (StaggeredGridLayoutManager) ((RecyclerView) parent).getLayoutManager();
            lm.invalidateSpanAssignments();
    }
    itemView.getViewTreeObserver().removeOnPreDrawListener(this);
    return true;
}
});
return new CustomRecyclerViewAdapter.ViewHolder(itemView);

有人可以告诉我我做错了什么吗?非常感谢!

4

0 回答 0