【问题标题】:RecyclerView with 2 column and auto resized images具有 2 列和自动调整大小的图像的 RecyclerView
【发布时间】:2019-12-31 14:56:24
【问题描述】:

我有一个两列的 recyclerView,我想显示不同高度的图像。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="2dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="2dp">

<android.support.v7.widget.CardView
    android:id="@+id/card_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#f7f5f5"
    app:cardCornerRadius="10dp">

    <ImageView
        android:id="@+id/image_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:srcCompat="@tools:sample/avatars" />
</android.support.v7.widget.CardView>

并在加载图像。

  RecyclerView recycler_item =  main.findViewById(R.id.recycler_item);
  recycler_item.setHasFixedSize(false);
  recycler_item .setLayoutManager(new GridLayoutManager(context, 2));
  recycler_item .setAdapter(itemAdapter);
  LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation);
  recycler_item.setLayoutAnimation(animation);
  itemAdapter.notifyDataSetChanged();
  recycler_item.scheduleLayoutAnimation();

结果是

但我想这样展示

【问题讨论】:

标签: android android-studio android-recyclerview


【解决方案1】:

首先,为ImageView 设置android:adjustViewBounds="true" 属性以调整其边界以保持其drawable 的纵横比。

现在,是时候使用StaggeredGridLayoutManager 的实例作为RecyclerView 的布局管理器来实现结果了。

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recycler_item.setLayoutManager(layoutManager);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多