带有CardStackView和Picasso Picture的Scrollview中的LinearLayout中的ImageView不显示

如何解决带有CardStackView和Picasso Picture的Scrollview中的LinearLayout中的ImageView不显示

我正在开发一个Android应用程序。

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

ImageView位于CardView的ScrollView的LinearLayout中。

我要做的是,在初次插入卡时,按照下图所示将图像调整为卡的大小。

enter image description here

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

enter image description here

我使用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()
}

这是片段布局

<?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>

当我运行该应用程序时,我得到的卡没有图像,只有文本,如下图所示

enter image description here

加载卡后如何显示与卡大小相同的图像?



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

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

我不确定为什么无法显示图像。但是,如果按照下面的代码设置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的高度和宽度设置为等于ScrollViewimageView的父母的父母)或CardView({ {1}}的父母的父母的父母) imageView(父母),因为LinearLayout的高度应为“ wrap_content”。



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

您的解决方案似乎基于图像的高度和宽度,但是我需要将图像放入CardView或ScrollView中

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

LinearLayout

这是我的CardView布局

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

这就是我在AVD上获得的内容。图片无法填充CardView的底部,因为它会根据其高度和宽度进行缩放?

enter image description here

解决方法

我从您更新的问题中了解到,您需要将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的问题是设置适配器后绘制的项目,此后我们需要监听绘制的Item的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创建。另外,您不必将Context传递给适配器,每个视图都附加有上下文。另外,在这种情况下,请不要使用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())
,

为什么要使用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,以使其高度等于孩子的身高。高度


更新

如果您想真正使用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" />

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-