gpt4 book ai didi

android - StaggeredLayout 与 FlexboxLayoutManager

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:56:00 25 4
gpt4 key购买 nike

我有什么办法可以使用类似于此的谷歌 Flexbox 库来实现交错布局

Layout implemented using StaggeredLayoutManager

上面的布局是使用 StaggeredLayoutManger 创建的,同时保持图像的纵横比

我试过代码:

recyclerView.apply {
layoutManager = FlexboxLayoutManager(context).apply {
flexWrap = FlexWrap.WRAP
justifyContent = JustifyContent.CENTER
}
adapter = mAdapter
}

但它使单列布局和宽高比也没有保持(考虑到图像尺寸很大)。

最佳答案

enter image description here

这就是我在我的一个项目中所做的,使用 FlexBoxLayout,同样在我的情况下,一些图片尺寸非常大,所以不得不做一些关于并发的事情。

  flexboxLayoutManager = new FlexboxLayoutManager(yourActivity.this);
flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);
flexboxLayoutManager.setAlignItems(AlignItems.STRETCH);
flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);
recyclerView.setLayoutManager(flexboxLayoutManager);

主要逻辑在adapter,在onBindViewHolder

 Glide.with(context)
.load(String.format("yourUrlHere")
.diskCacheStrategy(DiskCacheStrategy.ALL)
.animate(R.anim.fadein)
.override(dpToPx(90) ,dpToPx(90)) // here replace with your desired size.
.centerCrop()
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
// holder.loader.setVisibility(View.GONE);
return false;
}

@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
// holder.loader.setVisibility(View.GONE);
return false;
}
})
.priority(Priority.IMMEDIATE)
.into(viewHolder.imageView);

ViewGroup.LayoutParams lp = viewHolder.imageView.getLayoutParams();
if (lp instanceof FlexboxLayoutManager.LayoutParams) {
FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
Random random = new Random();
int val = random.nextInt(5);
if (val ==0){
val = 1;
}
flexboxLp.setFlexGrow(val);
flexboxLp.setFlexShrink(1f);

}

您可以使用这段代码解决您的问题,就像我自己在一个项目中实现的那样。希望这能解决您的问题。 快乐编码 :)

注意: 在我的项目中,根据需要,我使用了 flexboxLayoutManager.setFlexDirection(FlexDirection.COLUMN); 所以我的布局是这样的。

关于android - StaggeredLayout 与 FlexboxLayoutManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50291691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com