如何删除CardView布局中的冗余项目

时间:2016-12-12 11:19:48

标签: android android-recyclerview cardview

我在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>

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

enter image description here

更新代码时发生了异常:

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中删除此多余项?

1 个答案:

答案 0 :(得分:0)

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

然后你有方法isPositionHeader,你可以在onBindViewHolder方法中调用,以找出位置是否为标题位置。如果您的数据不是标题位置,则会在cardview填充数据。但如果它是标题位置,你只需跳过它。因此,第一个项目是空白,是使用相同cardview的标题,实际上是其他项目。

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