如何使RecyclerView标头不粘?

时间:2015-06-10 12:39:20

标签: android android-recyclerview

我有以下布局:

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

问题是TextView总是粘在顶部,而RecyclerView占用屏幕上的剩余空间。我可以水平滚动recyclerview,但TextView就在那里。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

您可以在recyclerView的第0个元素处添加Header View / TextView。 以下是描述相同内容的好例子。

Is there an addHeaderView equivalent for RecyclerView?

编辑(从提到的链接复制来源) : -

没有像listview.addHeaderView()这样的简单方法,但您可以通过在适配器中为标题添加类型来实现此目的。

这是一个例子

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);
       }
   }
}

答案 1 :(得分:0)

它还值得一试,XRecyclerView,它支持pullrefresh,loadingmore和header featrues。