如何解决带有CardStackView和Picasso Picture的Scrollview中的LinearLayout中的ImageView不显示
我正在开发一个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()
}
这是片段布局
<?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
的父母的父母)或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的底部,因为它会根据其高度和宽度进行缩放?
解决方法
我从您更新的问题中了解到,您需要将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 举报,一经查实,本站将立刻删除。