RecyclerView适配器显示空白cardview [英] RecyclerView adapter is showing blank cardview

查看:225
本文介绍了RecyclerView适配器显示空白cardview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在子api21设备上有一个令人讨厌的问题.我正在使用 RecyclerView.Adapter 适配器来充气 android.support.v7.widget.CardView .想法是,卡将仅添加存在于卡中的信息.如果没有信息,该卡将根本不会显示.因此,我以编程方式创建了这些卡.

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.

以下是api21和api19的屏幕截图

Here are screen shots on api21 and api19

请注意api19如何显示其中没有值的卡.我相信问题是由适配器调用无值的onCreateViewHolder引起的.有没有一种方法可以绕过onCreate?或onBind?还是有办法让api21的子卡处理方式有所不同?

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

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个)

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

推荐答案

您看到的差异很可能归因于CardView在子API21上使用其自己的填充/阴影机制.解决此问题的方法是在适配器确实没有数据的情况下不让适配器报告该数据.换句话说,如果由于物品为空而导致卡片为空,则不要在计数中报告此物品(或跟踪它们).

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.)

这篇关于RecyclerView适配器显示空白cardview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆