How to make RecyclerView header not sticky?

怎甘沉沦 提交于 2019-12-24 16:58:03

问题


i have the following layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.mkallingal.freakingnav.CardsListFragment">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/darktext"
        android:layout_marginBottom="6dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Cards Listing" />

    <android.support.v7.widget.RecyclerView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/rvCardsList"
        android:scrollbars="vertical" tools:listitem="@layout/recycler_item_layout">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

the problem is that the TextView is always sticky on the top, while RecyclerView takes the remaining space on the screen. I can horizontal scroll the recyclerview, but the TextView just stay there. How do i resolve this?


回答1:


You can add the Header View/TextView at the 0th element of the recyclerView. Here is the good example describing the same.

Is there an addHeaderView equivalent for RecyclerView?

Edit (Copying the source from the link mentioned) :-

There isn't an easy way like listview.addHeaderView() but you can achieve this by adding a type to your adapter for header.

Here is an example

public class HeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    String[] data;

    public HeaderAdapter(String[] data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            //inflate your layout and pass it to view holder
            return new VHItem(null);
       } else if (viewType == TYPE_HEADER) {
           //inflate your layout and pass it to view holder
           return new VHHeader(null);
       }

        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VHItem) {
            String dataItem = getItem(position);
            //cast holder to VHItem and set data
        } else if (holder instanceof VHHeader) {
           //cast holder to VHHeader and set data for header.
       }
   }

   @Override
    public int getItemCount() {
       return data.length + 1;
    }

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

       return TYPE_ITEM;
   }

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

   private String getItem(int position) {
       return data[position - 1];
   }

    class VHItem extends RecyclerView.ViewHolder {
        TextView title;

       public VHItem(View itemView) {
           super(itemView);
        }
    }

   class VHHeader extends RecyclerView.ViewHolder {
       Button button;

       public VHHeader(View itemView) {
           super(itemView);
       }
   }
}



回答2:


Its also worth checking out, XRecyclerView, it supports pullrefresh , loadingmore and header featrues.



来源:https://stackoverflow.com/questions/30756624/how-to-make-recyclerview-header-not-sticky

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!