ImageView in LinearLayout in Scrollview with CardStackView and Picasso 图片不显示

ImageView in LinearLayout in Scrollview with CardStackView and Picasso Picture does not show

我正在开发 Android 应用程序。

我正在为卡片库使用“com.yuyakaido.android:card-stack-view:2.3.4”。

ImageView在CardView的ScrollView中的LinearLayout中。

我想要做的是在第一次加载卡时,按照下图将图像适合卡的大小。

如果向下滚动,它会显示如下图所示的一些文本

我使用 Picasso 将图像适配到 ImageView 但图像不显示。

这是包含 CardStackView 的片段

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_card_stack, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    initCardStackView()
}

private fun initCardStackView() {
    manager = CardStackLayoutManager(context)
    manager.setCanScrollVertical(false)
    manager.setSwipeableMethod(SwipeableMethod.Manual)
    adapter = CardStackAdapter(context, createDummyProfiles())
    cardStackView.layoutManager = manager
    cardStackView.adapter = adapter
    cardStackView.itemAnimator = DefaultItemAnimator()
}

这是 Fragment 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <com.yuyakaido.android.cardstackview.CardStackView
        android:id="@+id/cardStackView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="80dp"
        android:clipToPadding="false"/>

</LinearLayout>

这是 CardStackAdapter,我在这里使用 Piccaso 裁剪图像并将其适合 ImageView

class CardStackAdapter(private var context: Context?, private var movies: MutableList<Movies>) :
    RecyclerView.Adapter<CardStackAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return ViewHolder(inflater.inflate(R.layout.card_view, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        Picasso.get().load(R.drawable.person3).fit().centerCrop().into(holder.itemView.imageView)
    }

    override fun getItemCount(): Int {
        return movies.size
    }

    fun setItems(items: MutableList<Profile>) {
        this.movies = items
    }
}

这是 card_view,它是 CardStackAdapter

的 viewHolder 的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/selectableItemBackground"
    android:foreground="?attr/selectableItemBackground"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="18dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="true">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/lipsum"/>

        </LinearLayout>

    </ScrollView>

</androidx.cardview.widget.CardView>

当我运行申请时,我得到的卡片没有图像,只有如下图所示的文字

如何在加载卡片时以卡片大小显示图像?



------------------------更新-------------------- --------

** 注意:图像将从服务器而非本地可绘制对象中检索。我只是将本地可绘制对象中的图像用于测试目的。

我不知道为什么图像不显示。但是如果我按照下面的代码设置 imageView 的 layoutParams 的高度和宽度,那么图像就会显示出来。

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.itemView.imageView.layoutParams.height = 1918
    holder.itemView.imageView.layoutParams.width = 970
    Picasso.get().load(R.drawable.person3).fit().centerCrop().into(holder.itemView.imageView)
}

所以,现在的问题是我是否可以将 ImageView 的高度和宽度设置为等于 ScrollView 的高度和宽度(imageView 的 parent 的 parent) 或 CardView (imageView 的 parent 的 parent 的 parent) NOT LinearLayout (parent) 因为LinearLayout的高度应该是"wrap_content".



------------------------更新-------------------- --------

看起来你的解决方案是基于图像的高度和宽度,但我需要将图像适合 CardView 或 ScrollView

这是我的 onBindViewHolder 代码。我刚刚删除了毕加索。

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.itemView.imageView.setImageResource(R.drawable.person2)
}

这是我的 CardView 布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/selectableItemBackground"
    android:foreground="?attr/selectableItemBackground"
    app:cardCornerRadius="18dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="true">

    <androidx.core.widget.NestedScrollView
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView"
                android:src="@drawable/person2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/lipsum"/>

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.cardview.widget.CardView>

这是我在 AVD 上得到的。图像没有填满 CardView 的底部,因为它会根据其高度和宽度进行缩放?

为什么要使用 Picasso 加载本地可绘制对象?您可以将源可绘制对象应用到 ImageView。

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:scaleType="fitCenter"
    android:src="@drawable/person3"
    android:layout_height="wrap_content" />

除此之外,我强烈建议您使用 NestedScrollView 而不是 ScrollView 然后,请将 fillViewPort (ScrollView 的属性)设置为 true 以将其高度扩展为等于child的身高


更新:

如果想真正使用Picasso加载外置URL,请进行如下配置:

Picasso
    .get()
    .load([url_you_want_to_load])
    .placeholder([placeholder_you_want_to_pre_load])
    .into(imageView)

然后,将您的 ImageView 配置为适合居中并保持其纵横比:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop" />

我从你更新的问题中了解到,你需要将 ImageView 填充到 CardView 高度,并且 textView 将在滚动后可见。这对于 ScrollView 来说似乎很棘手,因为我们需要以某种方式动态设置高度 match_parent 不适用于 ScrollView.
我在下面做了一个解决方案,看看这个锻炼是否适合你。创建自定义图像视图并在内部使用 CardView.

class FillImageView : AppCompatImageView {
constructor(context: Context?) : super(context!!) {}
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context!!, attrs, defStyleAttr) {}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    val parentWidth = MeasureSpec.getSize(widthMeasureSpec)
    setMeasuredDimension(parentWidth, (parent.parent as View).measuredHeight)
}
}

并在 CardView

中添加下面的 NestedScrolllView
<androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:fillViewport="true"
            android:layout_height="match_parent">
        <LinearLayout
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="wrap_content">
            <com.ace.myapplication.FillImageView
                    android:layout_width="match_parent"
                    android:src="@drawable/hello"
                    android:adjustViewBounds="true"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"/>
            <TextView
                    android:layout_width="match_parent"
                    android:textSize="23sp"
                    android:padding="10dp"
                    android:layout_height="wrap_content"/>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

PS- 使用 (parent.parent as View).measuredHeight 设置高度可能是一个丑陋的解决方案,但这就是我现在所能做的。喜欢为此看到一些优雅的解决方案。我还没有用 CardStackView.

测试过它

更新 现在 RecyclerView 的问题是在设置适配器后绘制的项目,我们需要监听此后绘制的项目的 layoutManager 回调。 解决此问题而不使其变得复杂的一种方法将 cadStackView 高度设置为 imageView 。请参阅下面的代码,仅添加基本代码。

class CardStackAdapter(val stackViewHeight:Int, private var movies: MutableList<Moview>) :
    RecyclerView.Adapter<CardStackAdapter.ViewHolder>() {

   inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView:ImageView = itemView.findViewById(R.id.imageView)
        init {
            imageView.layoutParams.height= stackViewHeight
        }
    }
}

现在在你的片段中

cardStackView.post {
       initCardStackView()
   }

并如下更改 initCardStackView 中的 dapter 创建。此外,您不必将上下文传递给适配器,每个视图都有一个附加到它的上下文。在这种情况下也不要使用 FillImageView 在 xml 布局中使用正常的 ImageView

// Since you are using android:padding it will be same for all sides
    val cardHeight=cardStackView.height- (2*cardStackView.paddingTop)
    val adapter = CardStackAdapter(cardHeight, createDummyProfiles())