如何在不需要加载更多项目的情况下从recyclerview中删除footerview?

时间:2018-03-05 13:49:52

标签: android android-recyclerview recycler-adapter

您可以按以下方式检查我的代码:

private class ContentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_ITEM = 0;
    public static final int TYPE_FOOTER = 1;

    private ArrayList<BookSearchModel.BookSearchVO> itemList    = null;

    public ContentListAdapter(ArrayList<BookSearchModel.BookSearchVO> item) {
        itemList = item;
    }

    @Override
    public int getItemViewType(int position) {
        if(position >= itemList.size()) {
            return TYPE_FOOTER;
        }

        return TYPE_ITEM;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_ITEM)
        {
            LayoutInflater inflater = (LayoutInflater)wrapperContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View item = inflater.inflate(R.layout.lib_book_search_item, parent, false);

            return new BookInfoHolder(item);
        }
        else
        {
            LayoutInflater inflater = (LayoutInflater)wrapperContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View item = inflater.inflate(R.layout.list_footer, parent, false);

            return new FooterViewHolder(item);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder wrappedHolder, int position) {
        if(wrappedHolder instanceof BookInfoHolder) {
            SOME OF VIEW BIND PROCESS....
        } else if(wrappedHolder instanceof FooterViewHolder) {
            // do nothing.
        }

    }

    @Override
    public int getItemCount() {
        return itemList.size() + 1;
    }

    public class BookInfoHolder extends RecyclerView.ViewHolder {

        private ImageView bookCover;
        private TextView bookTitle;
        private TextView bookAuthor;
        private TextView bookPublisher;
        private TextView bookLocation;

        public BookInfoHolder(View itemView) {
            super(itemView);
            bookCover       = (ImageView) itemView.findViewById(R.id.book_cover);
            bookTitle       = (TextView) itemView.findViewById(R.id.book_title);
            bookAuthor      = (TextView) itemView.findViewById(R.id.book_author_info);
            bookPublisher   = (TextView) itemView.findViewById(R.id.book_publisher);
            bookLocation    = (TextView) itemView.findViewById(R.id.book_location);
        }
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {

        private View v;

        public FooterViewHolder(View itemView) {
            super(itemView);
            this.v = itemView;
        }
    }
}

我试过RecyclerView.removeViewAt(position) casues空指针异常,我猜回收者的子视图与listview不相似。 并且,我尝试使用layoutmanager,但我不知道layoutmanager控件childView(或item)。

并注意到这一点:Remove RecyclerView FooterView 但是,任何其他都不适合我。

如果加载所有项目,我该如何删除footerView?

修改

what i see

what i want

lib_book_search_item.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="135dp"
            android:id="@+id/book_cover"
            android:transitionName="@string/book_cover_transition"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textColor="#000000"
            android:textStyle="bold"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:gravity="center_vertical|left"
            android:maxLines="2"
            android:ellipsize="end"
            android:id="@+id/book_title"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textColor="#929292"
            android:textStyle="bold"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:gravity="center_vertical|left"
            android:maxLines="2"
            android:ellipsize="end"
            android:id="@+id/book_author_info"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textColor="#929292"
            android:textStyle="bold"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:gravity="center_vertical|left"
            android:maxLines="2"
            android:ellipsize="end"
            android:layout_marginTop="2dp"
            android:id="@+id/book_publisher"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textColor="#01A997"
            android:textStyle="bold"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:gravity="center_vertical|left"
            android:maxLines="2"
            android:ellipsize="end"
            android:layout_marginTop="2dp"
            android:id="@+id/book_location"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textColor="#3C7FED"
            android:textStyle="bold"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:gravity="center_vertical|left"
            android:maxLines="2"
            android:ellipsize="end"
            android:layout_marginTop="2dp"
            android:id="@+id/book_status"/>

    </LinearLayout>

</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#CCCCCC"/>

</LinearLayout>

list_footer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:gravity="center"
    android:orientation="horizontal">

    <ProgressBar
        android:layout_width="24dp"
        android:layout_height="24dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:text="@string/msg_loading_data"
        android:gravity="center"
        android:layout_marginLeft="10dp"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您必须传递列表的 max_items

max_items = 在添加所有项目后完全形成列表后的项目总数

  public ContentListAdapter(ArrayList<BookSearchModel.BookSearchVO> item, int max_items) {
    itemList = item;
    this.max_items = max_items;
   }

然后在onBindViewHolder执行此检查。

    else if(wrappedHolder instanceof FooterViewHolder) {
        // do nothing.
        if(position == max_items){

          wrappedHolder.rootLayout.setVisibility(View.GONE); // this will remove the footer..
       }
    }