I have a nasty issue on sub api21 devices. I am using a RecyclerView.Adapter adapter to inflate android.support.v7.widget.CardView. The idea is that the cards will only add information that is present to the cards. If no information is present the card wont show at all. So I create these cards programatically.
Here are screen shots on api21 and api19
Notice how api19 shows cards with no values in them. I believe the issue is cause by the adapter calling onCreateViewHolder with no values. Is there a way to bypass onCreate? or onBind? Or is there a way to have sub api21 handle cards a little differently?
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"/>
adapter
@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;
}
}
}
Viewholder Class (1 of 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
The difference you are seeing is most likely due to
CardView
using its own padding/shadow mechanism on sub API21. The way to fix this is to not have your adapter report that it has data when it really does not. In other words, if the cards would be empty because the items are empty, then do not report this items in the count (or track them.)