android - StaggeredLayout 与 FlexboxLayoutManager

标签 android kotlin staggered-gridview android-flexboxlayout

我有什么办法可以使用类似于此的谷歌 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/

相关文章:

Kotlin Coroutines 如何获取当前线程的 CoroutineScope?

android - StateFlow 设置值会丢弃一些事件,但更新不会

java - 安卓工作室 : Unable to find explicit activity class but activity already declared in AndroidManifest. xml

android - 不从资源解析布局并在运行时设置它

java - AppWidgetProvider : having trouble with variables

android - 如何以编程方式备份​​ SD 卡中的 sqlite 文件?

android - 从 BitBucket 导入 Android Studio 项目只导入 gradle

java - 如何从 Java 访问 Kotlin 伴随对象

Android Espresso 2.0 卡在点击 gridview 项目上

android - 如何从 StaggeredGridLayoutManager 中找到单元格宽度?