我对sub api21设备有一个讨厌的问题。我正在使用 RecyclerView.Adapter 适配器来充气 android.support.v7.widget.CardView 。这个想法是卡片只会添加卡片上的信息。如果没有信息,卡片根本不显示。所以我以编程方式创建这些卡片。
以下是api21和api19的截屏
注意api19如何显示没有值的卡片。我认为问题是由适配器调用onCreateViewHolder而没有值。有没有办法绕过onCreate?还是onBind?或者有没有办法让sub api21处理卡有点不同?
CardView XML
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="true"
card_view:cardCornerRadius="5dp"
android:layout_margin="5dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:id="@+id/card_info_lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
适配器
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
switch(viewType) {
case VIEW_ENGINE:
itemViewEngine = LayoutInflater
.from(viewGroup.getContext())
.inflate(R.layout.card_info, viewGroup, false);
cardInfo.clear();
return new ViewHolderEngine(itemViewEngine, cardInfo);
case VIEW_TIRES:
itemViewTires = LayoutInflater
.from(viewGroup.getContext())
.inflate(R.layout.card_info, viewGroup, false);
cardInfo.clear();
return new ViewHolderTires(itemViewTires, cardInfo);
default: //General
itemViewGeneral = LayoutInflater
.from(viewGroup.getContext())
.inflate(R.layout.card_info, viewGroup, false);
cardInfo.clear();
return new ViewHolderGeneral(itemViewGeneral, cardInfo);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
cardInfo = vehicleInfo.get(i);
switch (getItemViewType(i)) {
case VIEW_GENERAL:
new ViewHolderGeneral(itemViewGeneral, cardInfo);
break;
case VIEW_ENGINE:
new ViewHolderEngine(itemViewEngine, cardInfo);
break;
case VIEW_TIRES:
new ViewHolderTires(itemViewTires, cardInfo);
break;
}
}
}
观看者类别(1/3)
class ViewHolderGeneral extends RecyclerView.ViewHolder {
private static final String TAG = "adapter_info_ViewHolder_gen";
private TypedArray headerColors;
private ArrayList<String> labels = new ArrayList<>();
public ViewHolderGeneral(View view, final ArrayList<String> cardInfo) {
super(view);
View layout = view.findViewById(R.id.card_info_lin);
DisplayMetrics metrics = layout.getContext().getResources().getDisplayMetrics();
...set up cards
答案 0 :(得分:3)
您看到的差异很可能是由于CardView
在子API21上使用自己的填充/阴影机制。解决这个问题的方法是不要让你的适配器报告它没有数据。换句话说,如果卡片因为项目为空而为空,则不要在计数中报告此项目(或跟踪它们)。