其他分享
首页 > 其他分享> > android-RecyclerView适配器显示空白cardview

android-RecyclerView适配器显示空白cardview

作者:互联网

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

这是api21和api19的屏幕截图

请注意api19如何显示其中没有值的卡.我相信问题是由适配器调用无值的onCreateViewHolder引起的.有没有一种方法可以绕过onCreate?还是onBind?还是有办法让子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

解决方法:

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

标签:recycler-adapter,android-recyclerview,android-cardview,android-support-library,a
来源: https://codeday.me/bug/20191120/2046148.html