问题描述
我有一个RecyclerView,其中包含CardView的列表以及从服务器提取的信息,有些卡仅具有背景颜色,而有些卡则包含背景图像.如果所有卡都只有背景颜色,则RecyclerView的行为将相应(我也可以设置边距和拐角半径).但是,如果任何一张卡片包含背景图像,当我尝试向上/向下滚动列表时都会崩溃(尽管背景图像将在没有预先设置的转角半径的情况下加载).
这是崩溃日志:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.support.v7.widget.RoundRectDrawable
at android.support.v7.widget.CardViewApi21.setBackgroundColor(CardViewApi21.java:107)
at android.support.v7.widget.CardView.setCardBackgroundColor(CardView.java:234)
at co.roverlabs.sdk.ui.CardListAdapter.onBindViewHolder(CardListAdapter.java:88)
at co.roverlabs.sdk.ui.CardListAdapter.onBindViewHolder(CardListAdapter.java:28)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4402)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3717)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3609)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1859)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1311)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1274)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1102)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:959)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3062)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这是我的onBindViewHolder()方法(用硬编码值简化,以便于阅读):
public void onBindViewHolder(final CardViewHolder holder, int position) {
//Set margins
CardView.LayoutParams cardLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2000);
cardLayoutParams.setMargins(20, 20, 20, 20);
holder.cardView.setLayoutParams(cardLayoutParams);
//Set background color or image
if(view.hasBackgroundImage()) {
holder.cardView.setBackground(R.drawable.background);
}
else {
holder.cardView.setCardBackgroundColor(Color.CYAN);
}
//Set border radius
holder.cardView.setRadius(20);
//Set elevation
holder.cardView.setCardElevation(0);
这是我的onCreateViewHolder()方法和自定义Recycler.ViewHolder方法:
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
return new CardViewHolder(itemView);
}
public class CardViewHolder extends RecyclerView.ViewHolder {
protected CardView cardView;
public CardViewHolder(View view) {
super(view);
cardView = (CardView)view.findViewById(R.id.single_card_view);
}
}
最后,这是CardView的XML文件:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/single_card_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/single_card_view_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</android.support.v7.widget.CardView>
我只是想知道如何将CardView的背景设置为自定义图像,而不只是颜色,以及是否有解决方法.对我来说,重要的是能够设置卡的拐角半径.谢谢!
假设您正在使用CardView,则无法更改背景可绘制对象,因为CardView使用背景可绘制对象绘制卡片.您可以通过 setCardBackgroundColor 更改卡片背景颜色.. >
I have a RecyclerView containing a list of CardViews with information that is pulled from a server, some cards have only a background color but some contains a background image. If the cards all have just background colors, the RecyclerView behaves accordingly (I am able to set the margins and corner radius as well). However, if any one card contains a background image, a crash will happen when I try to scroll up/down the list (although the background image will load without the previously set corner radius).
Here is the crash log:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.support.v7.widget.RoundRectDrawable
at android.support.v7.widget.CardViewApi21.setBackgroundColor(CardViewApi21.java:107)
at android.support.v7.widget.CardView.setCardBackgroundColor(CardView.java:234)
at co.roverlabs.sdk.ui.CardListAdapter.onBindViewHolder(CardListAdapter.java:88)
at co.roverlabs.sdk.ui.CardListAdapter.onBindViewHolder(CardListAdapter.java:28)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4402)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3717)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3609)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1859)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1311)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1274)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1102)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:959)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3062)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Here is my onBindViewHolder() method (simplified with hard coded values for easier reading):
public void onBindViewHolder(final CardViewHolder holder, int position) {
//Set margins
CardView.LayoutParams cardLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2000);
cardLayoutParams.setMargins(20, 20, 20, 20);
holder.cardView.setLayoutParams(cardLayoutParams);
//Set background color or image
if(view.hasBackgroundImage()) {
holder.cardView.setBackground(R.drawable.background);
}
else {
holder.cardView.setCardBackgroundColor(Color.CYAN);
}
//Set border radius
holder.cardView.setRadius(20);
//Set elevation
holder.cardView.setCardElevation(0);
Here is my onCreateViewHolder() method and custom Recycler.ViewHolder method:
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
return new CardViewHolder(itemView);
}
public class CardViewHolder extends RecyclerView.ViewHolder {
protected CardView cardView;
public CardViewHolder(View view) {
super(view);
cardView = (CardView)view.findViewById(R.id.single_card_view);
}
}
And finally, here is my XML file for the CardView:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/single_card_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/single_card_view_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</android.support.v7.widget.CardView>
I just wish to know how to set the background of a CardView to a custom image instead of just a color, and if there are any workarounds. It is important for me to be able to set the corner radius of the cards. Thank you!
Assuming you are using CardView, you cannot change background drawable because CardView uses background drawable to draw the card.You can change card background color via setCardBackgroundColor.
这篇关于在RecyclerView中设置CardView的背景图像会导致程序在滚动时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!