我正在创建一个照片库,我正在使用RecyclerView来处理这个交错列表。
LEFT 一个是它应该看起来像, RIGHT 一侧显示 IT现在看起来如何。
这就是我的代码。
调用适配器的代码:
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);
有人能告诉我我做错了什么吗?非常感谢!