先說使用RecyclerView要實現(xiàn)的必要步驟:
1.用LayoutManager管理它的布局:橫向或豎向,不同的布局風格黄橘;
2.實現(xiàn)Adapter類繼承RecyclerView.Adapter類兆览;
3.RecyclerView.addItemDecoration(newDividerItemDecoration()):
制作item之間的分割線;
4.RecyclerView.setItemAnimator(new MyItemAnimator()):制作刪除添加item等操作的動畫效果塞关;
5.實現(xiàn)RecyclerView的事件監(jiān)聽器:點擊抬探,長按等
mRecyclerView = findView(R.id.id_recyclerview);
//設置布局管理器
mRecyclerView.setLayoutManager(layout);
//設置adapter
mRecyclerView.setAdapter(adapter)
//設置Item增加、移除動畫
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//添加分割線
mRecyclerView.addItemDecoration(new DividerItemDecoration(
getActivity(), DividerItemDecoration.HORIZONTAL_LIST));
實現(xiàn)步驟
使用RecyclerView之前我們要導入相對應的包:
compile 'com.android.support:recyclerview-v7:+'
布局文件:主布局
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
item子布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/item_bg"
android:layout_margin="3dp"
android:layout_height="72dp">
<TextView
android:layout_width="72dp"
android:layout_height="match_parent"
android:id="@+id/Item_text"
android:gravity="center" />
</LinearLayout>
一.設置它的布局管理:
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(linearLayoutManager);
三個布局:直接上源代碼
1 .線性布局:
/** * @param context Current context, will be used to access resources.
* @param orientation Layout orientation. Should be {@link #HORIZONTAL} or{@link#VERTICAL}.
* @param reverseLayout When set to true, layouts from end to start.
*/
public LinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
mAnchorInfo = new AnchorInfo();
setOrientation(orientation);
setReverseLayout(reverseLayout);
}
2.格子布局:
/** * @param context Current context, will be used to access resources.
* @param spanCount The number of columns or rows in the grid
* @param orientation Layout orientation. Should be {@link #HORIZONTAL} or {@link
* #VERTICAL}.
* @param reverseLayout When set to true, layouts from end to start.
*/
public GridLayoutManager(Context context, int spanCount, int orientation,
boolean reverseLayout) {
super(context, orientation, reverseLayout);
setSpanCount(spanCount);
}
3.瀑布流布局:
/** * Creates a StaggeredGridLayoutManager with given parameters.
*
* @param spanCount If orientation is vertical, spanCount is number of columns. If
* orientation is horizontal, spanCount is number of rows.
* @param orientation {@link #VERTICAL} or {@link #HORIZONTAL}
*/
public StaggeredGridLayoutManager(int spanCount, int orientation) {
mOrientation = orientation;
setSpanCount(spanCount);
}
二.創(chuàng)建適配器
public class MyRecyclerViewAdatper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private LayoutInflater inflater;
private Context context;
private List<String> datas;
private OnitemClickListener onitemClickListener;
public interface OnitemClickListener{
void onItemClick(View view,int positon);
void onItemLongClick(View view,int position);
}
public void setOntermClickListener(OnitemClickListener ontermClickListener){
this.onitemClickListener = ontermClickListener;
}
public MyAdapter(Context context,List<String> datas){
this.context = context;
this.datas = datas;
inflater = LayoutInflater.from(context);
}
@Override
public long getItemId(int position) {//返回數(shù)據(jù)的id
return super.getItemId(position);
}
@Override
public int getItemCount() { //返回數(shù)據(jù)的數(shù)量
return datas.size();
}
@Override
public int getItemViewType(int position) { //放回item的類型(制定不同風格的item要用到)
return position;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//這個函數(shù)是初始化視圖帆赢,返回一個ViewHolder小压,方法是把View直接封裝在ViewHolder中,然后我們面向的是ViewHolder這個實例
View view = inflater.inflate(R.layout.item,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//對視圖的操作主要放在這里
holder.textView.setText(datas.get(position));
if(onitemClickListener!=null){
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onitemClickListener.onItemClick(holder.itemView, position);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onitemClickListener.onItemLongClick(holder.itemView, position);
return false;
}
});
}
}
public void addData(int pos){
datas.add(pos,"Insert one");
notifyItemInserted(pos);
}
public void deletData(int pos){
datas.remove(pos);
notifyItemRemoved(pos);
}
//就是一個持有者的類椰于,他里面一般沒有方法怠益,只有屬性,作用就是一個臨時的儲存器瘾婿,
//把你getView方法中每次返回的View存起來蜻牢,可以下次再用。這樣做的好處就是不必每次都到布局文件中去拿到你的View偏陪,提高了效率抢呆。
class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.Item_text);
}
}
說明:
notifyItemInserted(pos);//增加item
notifyItemRemoved(pos);//刪除item