1. 程式人生 > >RecyclerView的基本使用(一)

RecyclerView的基本使用(一)

RecyclerView 使用之前需要導包,不然系統會不認識這個控制元件,引用是新增 app  module 的,如下:

dependencies {compile 'com.android.support:recyclerview-v7:25.2.0'}

在 xml 檔案中 新增控制元件:

<android.support.v7.widget.RecyclerView
android:id="@+id/grid_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" 
/>

RecyclerView 的 layoutManager 有三種,

1. LinearLayoutManager ,實現 ListView 效果

2.GridLayoutManager,實現 GridView 效果

3.StaggeredGridLayoutManager ,自定義,可以實現瀑布流之類的

下面,先講 RecyclerView 實現List View的效果

初始化RecyclerView:    

  @BindView(R.id.grid_recycler_view)
    RecyclerView mRecyclerView;
在activity 的 onCrate 方法中,設定 RecyclerView 的 基本設定
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mLinearAdapter = new LinearAdapter<>(this, mNames);
        mRecyclerView.setAdapter(mLinearAdapter);

RecyclerView 的adapter 要繼承 RecyclerView.Adapter,LinearAdapter的程式碼如下:
public class LinearAdapter <T>extends RecyclerView.Adapter {

    protected Context mContext;
    protected View mLayoutView;
    protected List<T> mData;

    public LinearAdapter(Context context,List<T>data){
        this.mContext=context;
        this.mData=data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //注:不可使用view=LayoutInflater.from(mContext).inflate(R.layout.item_layout,null);不然會報錯
        mLayoutView= LayoutInflater.from(mContext).inflate(R.layout.item_layout,parent,false);
        ViewHolder viewHolder=new ViewHolder(mLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        String name=mData.get(position).toString();
        ((ViewHolder)holder).mTvName.setText(name);
    }

    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }

   class ViewHolder extends RecyclerView.ViewHolder{

        TextView mTvName;

        public ViewHolder(View view) {
            super(view);

            mTvName=(TextView)view.findViewById(R.id.tv_name);
        }


    }
}

RecyclerView 要實現gridView 的效果的話也很簡單,adapter 可以不變,只需要將 LinearLayoutManager 換成  GridLayoutManager 即可,如 Activity中程式碼如下:

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
        mLinearAdapter = new LinearAdapter<>(this, mNames);
        mRecyclerView.setAdapter(mLinearAdapter);

注:new GridLayoutManager(this,3) 中第一個引數是 Activity 或 Fragment 的例項,第二個引數表示 GridView 一行顯示幾個item。

RecyclerView 的點選事件:

雖然 RecyclerView 給我們提供了很好的自定義效果,但是每種不足的是,它不像ListView 和 GridView 一樣 有 OnItemClick 的點選事件,那麼就需要我們自己來實現它的 項點選和 長按事件了。

首先,需要在 adapter 中實現 onClick 的介面,將單個View的 點選和長按事件賦予給這個介面,在之前的 adapter 基礎上做修改,程式碼如下:

package com.test;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;

/**
 * Created by Admin on 2017/4/18.
 */

public class LinearAdapter <T>extends RecyclerView.Adapter {

    protected Context mContext;
    protected View mLayoutView;
    protected List<T> mData;

    protected OnRecyclerItemClickListener mOnRecyclerItemClickListener;

    public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener onRecyclerItemClickListener){
        this.mOnRecyclerItemClickListener=onRecyclerItemClickListener;
    }

    public LinearAdapter(Context context,List<T>data){
        this.mContext=context;
        this.mData=data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //注:不可使用view=LayoutInflater.from(mContext).inflate(R.layout.item_layout,null);不然會報錯
        mLayoutView= LayoutInflater.from(mContext).inflate(R.layout.item_layout,parent,false);
        ViewHolder viewHolder=new ViewHolder(mLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final String name=mData.get(position).toString();
        ((ViewHolder)holder).mTvName.setText(name);

        mLayoutView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnRecyclerItemClickListener!=null){
                    mOnRecyclerItemClickListener.onRecyclerClick(position,name);
                }
            }
        });

        mLayoutView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(mOnRecyclerItemClickListener!=null){
                    mOnRecyclerItemClickListener.onRecyclerLongClick(position,name);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }

   class ViewHolder extends RecyclerView.ViewHolder{

        TextView mTvName;

        public ViewHolder(View view) {
            super(view);

            mTvName=(TextView)view.findViewById(R.id.tv_name);
        }
    }

    public interface OnRecyclerItemClickListener{
        public void onRecyclerClick(int position,String s);
        public void onRecyclerLongClick(int position,String s);
    }
}

然後在 Activity 中 實現監聽:
public class MainActivity extends AppCompatActivity implements View.OnClickListener,LinearAdapter.OnRecyclerItemClickListener {

  private void setListener(){
        mLinearAdapter.setOnRecyclerItemClickListener(this);
    }

實現 onRecyclerClick 和 onRecyclerLongClick 方法,在次只做列印,實際在專案中再做具體邏輯
    @Override
    public void onRecyclerClick(int position, String s) {
        Log.e("","=========position=="+position);
        Log.e("","=========s=="+s);
    }

    @Override
    public void onRecyclerLongClick(int position, String s) {

    }

ok,這節的講解就到這裡,主要是講了 RecyclerView 實現 ListView 和GridView 及點選事件的實現功能,下節研究 RecyclerView 的自定義佈局