3

ImageView を含む recyclerview があります。ただし、各画像の下部には余分なスペースがあります。余分なスペースは、スクリーンショットでわかるように赤い背景です。すべての画像を並べて表示したいと思います。

ここに画像の説明を入力

レイアウト マネージャーを定義するアクティビティのコードは次のとおりです。

fun setupRecyclerView(recyclerView: RecyclerView) {
    val adapter = PictureAdapter()
    recyclerView.adapter = adapter
    recyclerView.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
}

recyclerview のアイテムの xml ファイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools">

    <data>
        <variable
        name="viewModel"
        type="com.my.app.viewModel.PictureViewModel" />
    </data>

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="280dp"
        app:imageUrl="@{viewModel.imageUrl}"
        android:background="#ff0000"
        android:adjustViewBounds="true"
        android:scaleType="fitStart" />
</layout>

そして、これで画像をロードしています

@BindingAdapter({"bind:imageUrl"})
public static void loadImage(final ImageView view, String imageUrl) {
    Picasso.with(view.getContext())
    .load(imageUrl)
    .into(view);
}

余分なスペースを削除するために、ImageView の高さを自動的にサイズ変更するにはどうすればよいですか? 画像をトリミングしたくありません。そしてwrap_content、ImageView を配置するとlayout_height、アプリケーションが遅くなり、スクロールがスムーズにならず、多くのエラーが発生します

アプリケーションがメイン スレッドで処理しすぎている可能性があります。

4

1 に答える 1

-2

単純な Gridview の代わりに Staggered gridview を使用する必要があります。また、imageview の代わりに DynamicHeightImageview を使用する必要があります。これにより、画像の高さが自動的に調整され、Pinterest アプリのように画像が次々と配置されます。そして、それはあなたの問題を完全に解決します。ありがとう

于 2015-12-12T19:09:31.833 に答える