如何使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只是停留在那儿.我该如何解决?
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?
您可以将Header View/TextView添加到recyclerView的第0个元素. 这是描述它们的好例子.
You can add the Header View/TextView at the 0th element of the recyclerView. Here is the good example describing the same.
是否存在与RecyclerView等效的addHeaderView ?
编辑(从提到的链接复制源) :-
Edit (Copying the source from the link mentioned) :-
没有像listview.addHeaderView()这样的简单方法,但是您可以通过在标头的适配器中添加类型来实现此目的.
There isn't an easy way like listview.addHeaderView() but you can achieve this by adding a type to your adapter for header.
这是一个例子
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);
}
}
}