(轉(zhuǎn))RecyclerView常用功能

RecyclerView是support:recyclerview-v7中提供的控件捷枯,最低兼容到android 3.0版本晓锻。

官方介紹RecyclerView為在有限的窗口展現(xiàn)大量數(shù)據(jù)的控件。擁有類似功能的控件有ListView肩祥、GridView以及被Google遺棄的Gallery等僚匆,為毛已經(jīng)有了它們,Google還推出RecyclerView呢搭幻,那就要說說RecyclerView所具有的一些優(yōu)勢了。

那RecyclerView到底有啥優(yōu)勢呢逞盆?總結(jié)起來六顆字:低耦合高類聚檀蹋。RecyclerView已經(jīng)標(biāo)準(zhǔn)化ViewHolder,我們自定義的ViewHoler需要繼承?RecyclerView.ViewHolder云芦,然后在構(gòu)造方法中初始化控件俯逾,后面會有具體介紹。通過設(shè)置不同的LayoutManager舅逸,以及結(jié)合ItemDecoration , ItemAnimator桌肴,ItemTouchHelper,可以實現(xiàn)非常炫酷的效果琉历,這些是ListView等控件難以企及的坠七。

基本使用:

1.使用前需要在在gradle中添加依賴

`implementation?'com.android.support:recyclerview-v7:27.0.2'? `

2.編寫代碼,首先我們需要在Xml中寫RecyclerView的布局旗笔,

```

android:id="@+id/recyclerView"??

android:layout_width="match_parent"??

android:layout_height="match_parent"/>????

```

然后在activity中獲取RecyclerView彪置,并設(shè)置LayoutManager以及adapter

//通過findViewById拿到RecyclerView實例??

`mRecyclerView?=???findViewById(R.id.recyclerView);? `

//設(shè)置RecyclerView管理器??

mRecyclerView.setLayoutManager(new?LinearLayoutManager(this,?LinearLayoutManager.VERTICAL,?false));??

//初始化適配器??

mAdapter?=new?MyRecyclerViewAdapter(list);???

//設(shè)置添加或刪除item時的動畫,這里使用默認(rèn)動畫??

mRecyclerView.setItemAnimator(new?DefaultItemAnimator());??

//設(shè)置適配器??

mRecyclerView.setAdapter(mAdapter);??

下面是MyRecyclerViewAdapter的代碼:

[java]?view plain?copy

package?com.sharejoys.recyclerviewdemo.actvity;??


import?android.support.v7.widget.RecyclerView;??

import?android.view.LayoutInflater;??

import?android.view.View;??

import?android.view.ViewGroup;??

import?android.widget.TextView;??


import?com.sharejoys.recyclerviewdemo.R;??


import?java.util.List;??


/**

?*?Created?by?青青-子衿?on?2018/1/15.

?*/??



public?class?MyRecyclerViewAdapterextends?RecyclerView.Adapter?{??

private?List?list;??


public?MyAdapter(List?list)?{??

this.list?=?list;??

????}??


@Override??

public?MyAdapter.ViewHolder?onCreateViewHolder(ViewGroup?parent,?int?viewType)?{??

View?view?=?LayoutInflater.from(parent.getContext()).inflate(R.layout.item_base_use,?parent,false);??

MyAdapter.ViewHolder?viewHolder?=new?MyAdapter.ViewHolder(view);??

return?viewHolder;??

????}??


@Override??

public?void?onBindViewHolder(MyAdapter.ViewHolder?holder,?int?position)?{??

????????holder.mText.setText(list.get(position));??

????}??


@Override??

public?int?getItemCount()?{??

return?list.size();??

????}??


class?ViewHolder?extends?RecyclerView.ViewHolder?{??

????????TextView?mText;??

????????ViewHolder(View?itemView)?{??

super(itemView);??

????????????mText?=?itemView.findViewById(R.id.item_tx);??

????????}??

????}??

}??

這里item_normal的布局也非常簡單

[html]?view plain?copy


xmlns:android="http://schemas.android.com/apk/res/android"??

android:layout_width="match_content"??

android:layout_height="wrap_content"??

android:orientation="vertical">??


android:id="@+id/item_tx"??

android:layout_width="match_content"??

android:layout_height="wrap_content"??

android:gravity="center"??

android:padding="10dp"??

android:layout_gravity="center_horizontal"??

android:text="Item"/>??



然后我們運行效果如下

從例子也可以看出來蝇恶,RecyclerView的用法并不比ListView復(fù)雜拳魁,反而更靈活好用,它將數(shù)據(jù)撮弧、排列方式潘懊、數(shù)據(jù)的展示方式都分割開來,因此可定制型贿衍,自定義的形式也非常多授舟,非常靈活。

設(shè)置橫向布局:

[java]?view plain?copy

mRecyclerView.setLayoutManager(new?LinearLayoutManager(this,?LinearLayoutManager.HORIZONTAL,?false));??

設(shè)置網(wǎng)格布局:

[java]?view plain?copy

mRecyclerView.setLayoutManager(new?GridLayoutManager(this,?3));??

設(shè)置瀑布流:

[java]?view plain?copy

mRecyclerView.setLayoutManager(new?StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));??

如果第二個參數(shù)可以設(shè)置為橫向的舌厨,則效果如下:

從以上可知岂却,我們可以通過設(shè)置不同的管理器,實現(xiàn)不同的效果

LinearLayoutManager:以線性布局展示,可以設(shè)置橫向和縱向

GridLayoutManager:以網(wǎng)格形式展示躏哩,類似GridView效果

StaggeredGridLayoutManager:以瀑布流形式的效果

RecyclerView條目之間默認(rèn)沒有分割線署浩,那是否可以像ListView一樣設(shè)置divider以及dividerHight搞一條分割線出來呢,答案是不可以的扫尺,google并沒有提供這樣的屬性筋栋。但是谷歌為我們提供了可以定制的解決辦法,那就是以下要說ItemDecoration

利用ItemDecoration實現(xiàn)條目分割線

ItemDecoration是谷歌定義的可用于畫分割線的類正驻, 是抽象的弊攘,需要我們自己去實現(xiàn)

[java]?view plain?copy

/**

???*?An?ItemDecoration?allows?the?application?to?add?a?special?drawing?and?layout?offset

???*?to?specific?item?views?from?the?adapter's?data?set.?This?can?be?useful?for?drawing?dividers

???*?between?items,?highlights,?visual?grouping?boundaries?and?more.

???*

???*?

All?ItemDecorations?are?drawn?in?the?order?they?were?added,?before?the?item

???*?views?(in?{@link?ItemDecoration#onDraw(Canvas,?RecyclerView,?RecyclerView.State)?onDraw()}

???*?and?after?the?items?(in?{@link?ItemDecoration#onDrawOver(Canvas,?RecyclerView,

???*?RecyclerView.State)}.

???*/??

public?abstract?static?class?ItemDecoration?{??

public?void?onDraw(Canvas?c,?RecyclerView?parent,?State?state)?{??

??????????onDraw(c,?parent);??

??????}??

@Deprecated??

public?void?onDraw(Canvas?c,?RecyclerView?parent)?{??

??????}??


public?void?onDrawOver(Canvas?c,?RecyclerView?parent,?State?state)?{??

??????????onDrawOver(c,?parent);??

??????}??


@Deprecated??

public?void?onDrawOver(Canvas?c,?RecyclerView?parent)?{??

??????}??



@Deprecated??

public?void?getItemOffsets(Rect?outRect,?int?itemPosition,?RecyclerView?parent)?{??

outRect.set(0,?0,?0,?0);??

??????}??


public?void?getItemOffsets(Rect?outRect,?View?view,?RecyclerView?parent,?State?state)?{??

??????????getItemOffsets(outRect,?((LayoutParams)?view.getLayoutParams()).getViewLayoutPosition(),??

??????????????????parent);??

??????}??

??}??

當(dāng)我們通過

[java]?view plain?copy

mRecyclerView.addItemDecoration();??

??? onDraw: 該方法可以在RecyclerView的畫布上畫任何裝飾,且是在?the item views 被繪制之前回調(diào)

??? onDrawOver:該方法可以在RecyclerView的畫布上畫任何裝飾姑曙,且是在?the item views 被繪制之后回調(diào)

??? getItemOffsets :可以在該方法中為the item views添加偏移量

下面我們可以就通過繼承ItemDecoration為RecyclerView添加分割線襟交。

DividerItemDecoration的代碼如下:?

[java]?view plain?copy

package?com.sharejoys.mvpdemo.ui.customview;??


import?android.content.Context;??

import?android.content.res.TypedArray;??

import?android.graphics.Canvas;??

import?android.graphics.Rect;??

import?android.graphics.drawable.Drawable;??

import?android.support.annotation.IntDef;??

import?android.support.v4.view.ViewCompat;??

import?android.support.v7.widget.LinearLayoutManager;??

import?android.support.v7.widget.RecyclerView;??

import?android.view.View;??


/**

?*?Date:?2018/1/14

?*

?*?@author?青青-子衿

?*?@since?1.0

?*/??


public?class?DividerItemDecoration?extends?RecyclerView.ItemDecoration?{??

@OrientationType??

private?int?mOrientation?=?LinearLayoutManager.VERTICAL;??

private?Drawable?mDivider;??


private?int[]?attrs?=?new?int[]{??

????????????android.R.attr.listDivider??

????};??


public?DividerItemDecoration(Context?context,?@OrientationType?int?orientation)?{??

????????TypedArray?typedArray?=?context.obtainStyledAttributes(attrs);??

mDivider?=?typedArray.getDrawable(0);??

????????typedArray.recycle();??

????????setOrientation(orientation);??

????}??


private?void?setOrientation(@OrientationType?int?orientation)?{??

if?(orientation?!=?LinearLayoutManager.VERTICAL?&&?orientation?!=?LinearLayoutManager.HORIZONTAL)?{??

throw?new?IllegalArgumentException("傳入的布局類型不合法");??

????????}??

this.mOrientation?=?orientation;??

????}??


@Override??

public?void?onDraw(Canvas?c,?RecyclerView?parent,?RecyclerView.State?state)?{??

//調(diào)用這個繪制方法,RecyclerView會回調(diào)該繪制方法伤靠,需要我們自己去繪制條目的間隔線??

if?(mOrientation?==?LinearLayoutManager.VERTICAL)?{??

//垂直??

????????????drawVertical(c,?parent);??

}else?{??

//水平??

????????????drawHorizontal(c,?parent);??

????????}??

????}??


private?void?drawVertical(Canvas?c,?RecyclerView?parent)?{??

//?畫水平線??

int?left?=?parent.getPaddingLeft();??

int?right?=?parent.getWidth()?-?parent.getPaddingRight();??

int?childCount?=?parent.getChildCount();??

for?(int?i?=?0;?i?<?childCount;?i++)?{??

????????????View?child?=?parent.getChildAt(i);??


????????????RecyclerView.LayoutParams?params?=?(RecyclerView.LayoutParams)?child.getLayoutParams();??

int?top?=?child.getBottom()?+?params.bottomMargin?+?Math.round(ViewCompat.getTranslationY(child));??

int?bottom?=?top?+?mDivider.getIntrinsicHeight();??

????????????mDivider.setBounds(left,?top,?right,?bottom);??

????????????mDivider.draw(c);??

????????}??

????}??


private?void?drawHorizontal(Canvas?c,?RecyclerView?parent)?{??

int?top?=?parent.getPaddingTop();??

int?bottom?=?parent.getHeight()?-?parent.getPaddingBottom();??

int?childCount?=?parent.getChildCount();??

for?(int?i?=?0;?i?<?childCount;?i++)?{??

????????????View?child?=?parent.getChildAt(i);??


????????????RecyclerView.LayoutParams?params?=?(RecyclerView.LayoutParams)?child.getLayoutParams();??

int?left?=?child.getRight()?+?params.rightMargin?+?Math.round(ViewCompat.getTranslationX(child));??

int?right?=?left?+?mDivider.getIntrinsicHeight();??

????????????mDivider.setBounds(left,?top,?right,?bottom);??

????????????mDivider.draw(c);??

????????}??

????}??


@Override??

public?void?getItemOffsets(Rect?outRect,?View?view,?RecyclerView?parent,?RecyclerView.State?state)?{??

//獲得條目的偏移量(所有的條目都會回調(diào)一次該方法)??

if?(mOrientation?==?LinearLayoutManager.VERTICAL)?{??

//垂直??

outRect.set(0,?0,?0,?mDivider.getIntrinsicHeight());??

}else?{??

//水平??

outRect.set(0,?0,?mDivider.getIntrinsicWidth(),?0);??

????????}??

????}??


@IntDef({LinearLayoutManager.VERTICAL,?LinearLayoutManager.HORIZONTAL})??

public?@interface?OrientationType?{??

????}??

}??

然后在activity設(shè)置水平方向

[java]?view plain?copy

mRecyclerView.setLayoutManager(new?LinearLayoutManager(this,?LinearLayoutManager.VERTICAL,?false);???

mRecyclerView.addItemDecoration(new?DividerItemDecoration(this,?LinearLayoutManager.VERTICAL));??

豎直方向:

[java]?view plain?copy

mRecyclerView.setLayoutManager(new?LinearLayoutManager(this,?LinearLayoutManager.HORIZONAL,?false);???

mRecyclerView.addItemDecoration(new?DividerItemDecoration(this,?LinearLayoutManager.HORIZONAL))??

這里的分割線是默認(rèn)的捣域,我們可以在主題中去設(shè)置分割線的顏色

[html]?view plain?copy




@color/colorPrimary??

@color/colorPrimaryDark??

@color/colorAccent??

@drawable/bg_recyclerview_divider??


bg_recyclerview_divider.xml

[html]?view plain?copy


android:shape="rectangle">??


android:centerColor="#ff00ff00"??

android:endColor="#ff0000ff"??

android:startColor="#ffff0000"??

android:type="linear"/>??


android:width="10dp"??

android:height="10dp"/>??



運行后效果如下

以上的分割線只適用在LinearLayoutManager的相關(guān)布局中。

對于GridLayoutManager布局是不適用的宴合。需要我們單獨寫一個焕梅。

以下是對于GridLayoutManager布局的分割線代碼

[java]?view plain?copy

package?com.sharejoys.recyclerviewdemo.view;??


import?android.content.Context;??

import?android.content.res.TypedArray;??

import?android.graphics.Canvas;??

import?android.graphics.Rect;??

import?android.graphics.drawable.Drawable;??

import?android.support.v7.widget.GridLayoutManager;??

import?android.support.v7.widget.RecyclerView;??

import?android.view.View;??


/**

?*?Date:?2018/1/14?

?*

?*?@author?青青-子衿

?*?@since?1.0

?*/??


public?class?DividerGridViewItemDecoration?extends?RecyclerView.ItemDecoration?{??

private?Drawable?mDivider;??

private?int[]?attrs?=?new?int[]{??

????????????android.R.attr.listDivider};??


public?DividerGridViewItemDecoration(Context?context)?{??

????????TypedArray?typedArray?=?context.obtainStyledAttributes(attrs);??

mDivider?=?typedArray.getDrawable(0);??

????????typedArray.recycle();??

????}??


@Override??

public?void?onDraw(Canvas?c,?RecyclerView?parent,?RecyclerView.State?state)?{??

????????drawVertical(c,?parent);??

????????drawHorizontal(c,?parent);??

????}??


private?void?drawVertical(Canvas?c,?RecyclerView?parent)?{??

//繪制垂直間隔線(垂直的矩形)??

int?childCount?=?parent.getChildCount();??

for?(int?i?=?0;?i?<?childCount;?i++)?{??

????????????View?child?=?parent.getChildAt(i);??

????????????RecyclerView.LayoutParams?params?=?(RecyclerView.LayoutParams)?child.getLayoutParams();??

int?left?=?child.getRight()?+?params.rightMargin;??

int?right?=?left?+?mDivider.getIntrinsicWidth();??

int?top?=?child.getTop()?-?params.topMargin;??

int?bottom?=?child.getBottom()?+?params.bottomMargin;??


????????????mDivider.setBounds(left,?top,?right,?bottom);??

????????????mDivider.draw(c);??

????????}??

????}??


private?void?drawHorizontal(Canvas?c,?RecyclerView?parent)?{??

//繪制水平分割線??

int?childCount?=?parent.getChildCount();??

for?(int?i?=?0;?i?<?childCount;?i++)?{??

????????????View?child?=?parent.getChildAt(i);??

????????????RecyclerView.LayoutParams?params?=?(RecyclerView.LayoutParams)?child.getLayoutParams();??

int?left?=?child.getLeft()?-?params.leftMargin;??

int?right?=?child.getRight()?+?params.rightMargin?+?mDivider.getIntrinsicWidth();??

int?top?=?child.getBottom()?+?params.bottomMargin;??

int?bottom?=?top?+?mDivider.getIntrinsicHeight();??


????????????mDivider.setBounds(left,?top,?right,?bottom);??

????????????mDivider.draw(c);??

????????}??

????}??


@Override??

public?void?getItemOffsets(Rect?outRect,?View?view,?RecyclerView?parent,?RecyclerView.State?state)?{??

//?四個方向的偏移值??

int?right?=?mDivider.getIntrinsicWidth();??

int?bottom?=?mDivider.getIntrinsicHeight();??


????????RecyclerView.LayoutParams?params?=?(RecyclerView.LayoutParams)?view.getLayoutParams();??

int?itemPosition?=?params.getViewAdapterPosition();??

if?(isLastColum(itemPosition,?parent))?{??

right?=0;??

????????}??


if?(isLastRow(itemPosition,?parent))?{??

bottom?=0;??

????????}??

outRect.set(0,?0,?right,?bottom);??

????}??


/**

?????*?是否最后一行

?????*/??

private?boolean?isLastRow(int?itemPosition,?RecyclerView?parent)?{??

int?spanCount?=?getSpanCount(parent);??

if?(spanCount?!=?-1)?{??

int?childCount?=?parent.getAdapter().getItemCount();??

int?lastRowCount?=?childCount?%?spanCount;??

//最后一行的數(shù)量小于spanCount??

if?(lastRowCount?==?0?||?lastRowCount?<?spanCount)?{??

return?true;??

????????????}??

????????}??


return?false;??

????}??



/**

?????*?根據(jù)parent獲取到列數(shù)

?????*/??

private?int?getSpanCount(RecyclerView?parent)?{??

????????RecyclerView.LayoutManager?layoutManager?=?parent.getLayoutManager();??

if?(layoutManager?instanceof?GridLayoutManager)?{??

????????????GridLayoutManager?lm?=?(GridLayoutManager)?layoutManager;??

int?spanCount?=?lm.getSpanCount();??

return?spanCount;??

????????}??

return?-1;??

????}??


/**

?????*?判斷是否是最后一列

?????*/??

private?boolean?isLastColum(int?itemPosition,?RecyclerView?parent)?{??

int?spanCount?=?getSpanCount(parent);??

if?(spanCount?!=?-1)?{??

if?((itemPosition?+?1)?%?spanCount?==?0)?{??

return?true;??

????????????}??

????????}??

return?false;??

????}??

}??

我們在activity中使用該分割線

[java]?view plain?copy

mRecyclerView.setLayoutManager(new?GridLayoutManager(this,?3));??

mRecyclerView.addItemDecoration(new?DividerGridViewItemDecoration(this));??

點擊事件

RecyclerView并沒有像ListView的那樣可以設(shè)置點擊事件以及長按點擊事件,這個需要我們可以在adapter中去設(shè)置回調(diào)的方式實現(xiàn)卦洽,具體代碼如下:

MyRecyclerViewAdapter的代碼如下:

[java]?view plain?copy

package?com.sharejoys.recyclerviewdemo.actvity;??


import?android.support.v7.widget.RecyclerView;??

import?android.view.LayoutInflater;??

import?android.view.View;??

import?android.view.ViewGroup;??

import?android.widget.TextView;??


import?com.sharejoys.recyclerviewdemo.R;??


import?java.util.List;??


/**

?*?Created?by?青青-子衿?on?2018/1/15.

?*/??



public?class?MyRecyclerViewAdapter?extends?RecyclerView.Adapter?{??

private?List?list;??

private?OnItemClickListener?onItemClickListener;??

private?OnItemLongClickListener?onItemLongClickListener;??


/**

?????*?設(shè)置點擊事件

?????*/??

public?void?setOnItemClickListener(OnItemClickListener?onItemClickListener)?{??

this.onItemClickListener?=?onItemClickListener;??

????}??


/**

?????*?設(shè)置長按點擊事件

?????*/??

public?void?setOnItemLongClickListener(OnItemLongClickListener?onItemLongClickListener)?{??

this.onItemLongClickListener?=?onItemLongClickListener;??

????}??


public?MyRecyclerViewAdapter(List?list)?{??

this.list?=?list;??

????}??


@Override??

public?MyRecyclerViewAdapter.ViewHolder?onCreateViewHolder(ViewGroup?parent,?int?viewType)?{??

View?view?=?LayoutInflater.from(parent.getContext()).inflate(R.layout.item_base_use,?parent,false);??

MyRecyclerViewAdapter.ViewHolder?viewHolder?=new?MyRecyclerViewAdapter.ViewHolder(view);??

return?viewHolder;??

????}??


@Override??

public?void?onBindViewHolder(MyRecyclerViewAdapter.ViewHolder?holder,?int?position)?{??

????????holder.mText.setText(list.get(position));??

int?adapterPosition?=?holder.getAdapterPosition();??

if?(onItemClickListener?!=?null)?{??

holder.itemView.setOnClickListener(new?MyOnClickListener(position,?list.get(adapterPosition)));??

????????}??

if?(onItemLongClickListener?!=?null)?{??

holder.itemView.setOnLongClickListener(new?MyOnLongClickListener(position,?list.get(adapterPosition)));??

????????}??

????}??


@Override??

public?int?getItemCount()?{??

return?list.size();??

????}??


class?ViewHolder?extends?RecyclerView.ViewHolder?{??

????????TextView?mText;??


????????ViewHolder(View?itemView)?{??

super(itemView);??

????????????mText?=?itemView.findViewById(R.id.item_tx);??

????????}??

????}??


private?class?MyOnLongClickListener?implements?View.OnLongClickListener?{??

private?int?position;??

private?String?data;??


public?MyOnLongClickListener(int?position,?String?data)?{??

this.position?=?position;??

this.data?=?data;??

????????}??


@Override??

public?boolean?onLongClick(View?v)?{??

????????????onItemLongClickListener.onItemLongClick(v,?position,?data);??

return?true;??

????????}??

????}??


private?class?MyOnClickListener?implements?View.OnClickListener?{??

private?int?position;??

private?String?data;??


public?MyOnClickListener(int?position,?String?data)?{??

this.position?=?position;??

this.data?=?data;??

????????}??


@Override??

public?void?onClick(View?v)?{??

????????????onItemClickListener.onItemClick(v,?position,?data);??

????????}??

????}??



public?interface?OnItemClickListener?{??

void?onItemClick(View?view,?int?position,?String?data);??

????}??


public?interface?OnItemLongClickListener?{??

void?onItemLongClick(View?view,?int?position,?String?data);??

????}??


}??

activity中設(shè)置監(jiān)聽:

[java]?view plain?copy

mAdapter.setOnItemClickListener(new?MyRecyclerViewAdapter.OnItemClickListener()?{??

@Override??

public?void?onItemClick(View?view,?int?position,?String?data)?{??

Toast.makeText(MainActivity.this,?"您點擊了:??"?+?data,?Toast.LENGTH_SHORT).show();??

????????}??

????});??

mAdapter.setOnItemLongClickListener(new?MyRecyclerViewAdapter.OnItemLongClickListener()?{??

@Override??

public?void?onItemLongClick(View?view,?int?position,?String?data)?{??

Toast.makeText(MainActivity.this,?"您長按點擊了:??"?+?data,?Toast.LENGTH_SHORT).show();??

????????}??

????});??

運行后效果如下:

ItemAnimator

我們可以為RecyclerView設(shè)置增加和刪除動畫贞言,這里我們可以使用默認(rèn)動畫

[java]?view plain?copy

//設(shè)置添加或刪除item時的動畫,這里使用默認(rèn)動畫??

mRecyclerView.setItemAnimator(new?DefaultItemAnimator());??

然后在Adapter中增加刪除和添加數(shù)據(jù)的方法

[java]?view plain?copy

/**

??*?插入一條數(shù)據(jù)

??*

??*?@param?index?下標(biāo)

??*?@param?s?????數(shù)據(jù)

??*/??

public?void?addItem(int?index,?String?s)?{??

?????list.add(index,?s);??

?????notifyItemInserted(index);??

?}??


/**

??*?刪除一條數(shù)據(jù)

??*

??*?@param?index?下標(biāo)

??*/??

public?void?deleteItem(int?index)?{??

?????list.remove(index);??

?????notifyItemRemoved(index);??

?}??

activty調(diào)用刪除和添加方法后效果如下

RecycleView還有一些其他用法阀蒂,比如結(jié)合ItemTouchHelper實現(xiàn)item的拖拽效果该窗,可以自定義增加header和footer(類似Listview)

最后附上demo地址

本文來自:https://blog.csdn.net/tuike/article/details/79064750

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蚤霞,隨后出現(xiàn)的幾起案子挪捕,更是在濱河造成了極大的恐慌,老刑警劉巖争便,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件级零,死亡現(xiàn)場離奇詭異,居然都是意外死亡滞乙,警方通過查閱死者的電腦和手機奏纪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來斩启,“玉大人序调,你說我怎么就攤上這事⊥么兀” “怎么了发绢?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵硬耍,是天一觀的道長。 經(jīng)常有香客問我边酒,道長经柴,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任墩朦,我火速辦了婚禮坯认,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘氓涣。我一直安慰自己牛哺,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布劳吠。 她就那樣靜靜地躺著引润,像睡著了一般。 火紅的嫁衣襯著肌膚如雪痒玩。 梳的紋絲不亂的頭發(fā)上椰拒,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天,我揣著相機與錄音凰荚,去河邊找鬼。 笑死褒脯,一個胖子當(dāng)著我的面吹牛便瑟,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播番川,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼到涂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了颁督?” 一聲冷哼從身側(cè)響起践啄,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎沉御,沒想到半個月后屿讽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡吠裆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年伐谈,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片试疙。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡诵棵,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出祝旷,到底是詐尸還是另有隱情履澳,我是刑警寧澤嘶窄,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布,位于F島的核電站距贷,受9級特大地震影響柄冲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜储耐,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一羊初、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧什湘,春花似錦长赞、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至哟旗,卻和暖如春贩据,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背闸餐。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工饱亮, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人舍沙。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓近上,卻偏偏與公主長得像,于是被迫代替她去往敵國和親拂铡。 傳聞我的和親對象是個殘疾皇子壹无,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內(nèi)容