1

我在 RecyclerView 中使用 CardView 布局,JSON 将加载数据并填充适配器。我的布局包括一个 ImageView、一个 TextView,一切都很好。

这是适配器类(更新):

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private static final int TYPE_HEADER = 4;
    private static final int TYPE_ITEM = 1;
    Context context;
    List<CarData> getCarData;  // getDataAdapter
    ImageLoader imageThumbLoader;

    public RecyclerViewAdapter(List<CarData> getCarData, Context context){
        super();
        this.getCarData = getCarData;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder Viewholder, int position) {
        // if (!isPositionHeader(position)) {
            CarData getCarData1 = getCarData.get(position - 1);
            imageThumbLoader = ServerImageParseAdapter.getInstance(context).getImageLoader();

            imageThumbLoader.get(getCarData1.getImageThumb(),
                    ImageLoader.getImageListener(
                            Viewholder.imageThumb,//Server Image
                            R.mipmap.ic_launcher,//Before loading server image the default showing image.
                            android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
                    )
            );

        Viewholder.imageThumb.setImageUrl(getCarData1.getImageThumb(), imageThumbLoader);
        Viewholder.titleName.setText(getCarData1.getTitleName());
        Viewholder.doorName.setText(Html.fromHtml("<b>Số cửa:</b> " + getCarData1.getDoorName()));
        Viewholder.seatName.setText(Html.fromHtml("<b>Số ghế:</b> " + getCarData1.getSeatName()));
        Viewholder.cityName.setText(Html.fromHtml(getCarData1.getCityName()));
        Viewholder.districtName.setText(Html.fromHtml(getCarData1.getDistrictName()));

        // }
    }

    public int getDataAdapter() {
        return getCarData == null ? 0 : getCarData.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position)) {
            return TYPE_HEADER;
        }

        return TYPE_ITEM;
    }

    @Override
    public int getItemCount() {

        return getDataAdapter();  // + 1
    }

    private boolean isPositionHeader(int position) {
        return position == 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView titleName;
        public NetworkImageView imageThumb;
        public TextView doorName;
        public TextView seatName;
        public TextView cityName;
        public TextView districtName;

        public ViewHolder(View itemView) {

            super(itemView);

            titleName = (TextView) itemView.findViewById(R.id.titleName);
            imageThumb = (NetworkImageView) itemView.findViewById(R.id.imageThumb);
            doorName = (TextView) itemView.findViewById(R.id.doorName);
            seatName = (TextView) itemView.findViewById(R.id.seatName);
            cityName = (TextView) itemView.findViewById(R.id.cityName);
            districtName = (TextView) itemView.findViewById(R.id.districtName);
        }
    }

}

我的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="3dp"
card_view:contentPadding="3dp"
card_view:cardCornerRadius="3dp"
card_view:cardMaxElevation="3dp"
card_view:cardUseCompatPadding="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/imageThumb"
        android:layout_width="400dp"
        android:layout_height="230dp"
        android:src="@mipmap/ic_launcher"
        android:gravity="top"
        android:scaleType="centerCrop"/>

    <TextView
        android:id="@+id/titleName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Image View"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageThumb"
        android:layout_toEndOf="@+id/imageThumb"
        android:layout_marginLeft="20dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2">

            <TextView
                android:id="@+id/doorName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"/>
            <TextView
                android:id="@+id/seatName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2">

            <TextView
                android:id="@+id/cityName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"/>

            <TextView
                android:id="@+id/districtName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"/>

        </LinearLayout>

    <TextView
        android:id="@+id/checkStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="KIỂM TRA TÌNH TRẠNG XE"
        android:textColor="@color/color_primary_green_dark"
        android:textStyle="bold"
        android:textSize="12dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/textView_item32"
        android:layout_toEndOf="@+id/textView_item32"
        android:layout_marginLeft="20dp"/>
</LinearLayout>

但是当我第一次加载数据时,有一个冗余项。请看以下附件:

在此处输入图像描述

更新代码时发生了异常:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: pl.michalz.hideonscrollexample, PID: 9794
              java.lang.ArrayIndexOutOfBoundsException: length=18; index=-1
                  at java.util.ArrayList.get(ArrayList.java:310)
                  at pl.michalz.hideonscrollexample.RecyclerViewAdapter$override.onBindViewHolder(RecyclerViewAdapter.java:54)
                  at pl.michalz.hideonscrollexample.RecyclerViewAdapter$override.access$dispatch(RecyclerViewAdapter.java)
                  at pl.michalz.hideonscrollexample.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:0)
                  at pl.michalz.hideonscrollexample.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:20)
                  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
                  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)

如何在 CardView 中删除这个多余的项目?

4

1 回答 1

0

您的覆盖getItemCount()方法中有 + 1 。这会导致您的列表中有一个额外的项目。

然后你有方法isPositionHeader,你在onBindViewHolder方法中调用它来找出位置是否是标题位置。cardview如果不是标题位置,您正在填充数据。但如果它是标题位置,你就跳过它。所以第一个项目是空白的,实际上是使用与其他项目相同的卡片视图的标题。

尝试删除 + 1 和这个条件if (!isPositionHeader(position))

于 2016-12-12T13:52:21.817 回答