Android技术知识程序员

RecyclerView的基本用法

2016-03-23  本文已影响1271人  Vane_Subin

单一的RecyclerView实现的效果图:

仿QQ动态界面.jpg

上图随意用的几个Android自带的icon,不太美观请轻点吐槽....
既然本篇博文介绍的是基本用法,仅提供本人拙见,就不手把手教程如何实现,所以请伸手党止步哈.

引用

compile 'com.android.support:recyclerview-v7:25.0.1'
<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:scrollbars="none"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

LayoutManager:
这是一个抽象类,负责将每个Item视图在RecylerView中的布局,系统提供了3个实现类:

ItemAnimator:
ItemAnimator也是一个抽象类,系统为我们提供了一种默认的实现类,当Adapter执行notifyItemRemoved()、
notifyItemInserted()进行增加/删除某一项Item时会有动画效果,还用一种是移动Item。

DefaultItemAnimator mDefaultItemAnimator=new DefaultItemAnimator();

DividerItemDecoration
顾名思义就是Item间的分割线,实现很简单(注意RecyclerView包版本)

DividerItemDecoration mDividerItemDecoration =new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL);

分割线样式在你的AppTheme里面添加:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
   ...
   <item name="android:listDivider">@drawable/divider_bg</item> //shape
</style>

** RecyclerViewAdapter**
Adapter 没有提供Item点击回调给我们,因此,我们需要主动回调Item或Item内控件的点击事件。

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;   
    OnItemClickListener onItemClickListener;  
    List<UserInfo> userInfoList;

    public RecyclerViewAdapter(Context mContext,List<UserInfo> userInfoList) { 
       this.mContext = mContext; 
       this.userInfoList = userInfoList;
    }    

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_data_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder vh = (ViewHolder) holder;
        /*当ItemView被点击时,我们通过接口回调给调用方法。如果还需对外提供ItemView内某个Button或  
        TextView的点击事件,将vh.itemView改为相应控件即可*/
        UserInfo userInfo = userInfoList.get(position);
        if(userInfo.getName()!=null) {
             vh.tv_name..setText(userInfo.getName()); 
        }
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override 
           public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick();
                }
            }
        });
    }

    //向外提供点击回调方法。
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //Item的点击接口
    public interface OnItemClickListener {
        void onItemClick();
    }

    @Override
    public int getItemCount() {
        //Item总数
        return userInfoList !=null ? userInfoList.size() : 0;
    }

    @Override
    public int getItemViewType(int position) { 
         return super.getItemViewType(position);
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ViewHolder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name); 
       }
    }
}

以上是最简单的Adapter写法,粗略介绍一下每个方法的作用

@Override
public int getItemViewType(int position) {
    if (position == 0){
        return  topViewType;//private final int topViewType = 0;
    }
    ...
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == topViewType){
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_top, parent, false);
        return new ViewHolder(view);
    }
    ...
}
@Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder){
            ViewHolder vh = (ViewHolder) holder;
        }
        ...
    }
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setItemAnimator(mDefaultItemAnimator);//设置默认动画(可选)
recyclerView.setLayoutManager(mLinearLayoutManager);//设置布局管理器(必选)
recyclerView.addItemDecoration(new DividerItemDecoration(this, mDividerItemDecoration);//设置分割线(可选)
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, userInfoList);
recyclerView.setAdapter(recyclerViewAdapter);//加载适配器(必选)
上一篇下一篇

猜你喜欢

热点阅读