RecyclerView

详细参考:http://blog.csdn.net/lmj623565791/article/details/45059587

  • RecyclerView 控件用于在有限的窗口中展示大量数据集,类似:ListView、GridView。
  • 整体上看 RecyclerView 架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同 LayoutManager,ItemDecoration , ItemAnimator 实现令人瞠目的效果。
  • 通过布局管理器** LayoutManager**,控制其显示的方式。
  • 通过 ItemDecoration,控制 Item 间的间隔(可绘制)。
  • 通过 ItemAnimator,控制 Item 增删的动画。
  • 控制点击、长按事件,要自己写。
  • 基本步骤:
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));

一、简单使用:


dependencies {
    ......
    compile 'com.android.support:design:24.2.0'
}




    





    


MainActivity:

package com.ttt.recyclerviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List data;
    private MyRecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        data=new ArrayList<>();
        for(int i=0;i<20;i++){
            data.add(""+i);
        }

        recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter=new MyRecyclerAdapter(this,data);
        recyclerView.setAdapter(adapter);
    }
}

MyRecyclerAdapter:

package com.ttt.recyclerviewtest;

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;

public class MyRecyclerAdapter extends RecyclerView.Adapter{

    private Context context;
    private List list;

    public MyRecyclerAdapter(Context context, List list){
        super();
        this.context=context;
        this.list=list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).
                inflate(R.layout.recycler_view_item,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView.setText(list.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private TextView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView=(TextView)itemView.findViewById(R.id.recycler_text);
        }
    }
}

二、布局管理器 LayoutManager


1. LinearLayoutManager 线性布局管理器,支持横向、纵向。

recyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerView_第1张图片

2. GridLayoutManager 网格布局管理器。

recyclerView.setLayoutManager(new GridLayoutManager(this,4));
RecyclerView_第2张图片

3. StaggeredGridLayoutManager 瀑布流式布局管理器。

recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
RecyclerView_第3张图片

三、分割线 ItemDecoration



四、增删动画 ItemAnimator



五、点击和长按事件


  • 可以通过 recyclerView.addOnItemTouchListener 去监听然后去判断手势。比较麻烦。
  • 也可以通过** adapter 中自己去提供回调**,这里选择后者。

  1. RecyclerViewAdapter 中设置回调接口 MyOnItemClickListener:
    public interface MyOnItemClickListener{
        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }
  1. onBindViewHolder() 方法中给 ViewHolder 的 view 设置点击事件,在点击事件中回调 MyOnItemClickListener 中的方法。
final int pos=holder.getAdapterPosition();
        if(myOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myOnItemClickListener.onItemClick(v,pos);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    myOnItemClickListener.onItemLongClick(v,pos);
                    return false;
                }
            });
        }
  1. 使用时给 adapter 设置 MyOnItemClickListener 并复写回调方法,设置想要的点击事件。
        adapter.setMyOnItemClickListener(new MyRecyclerAdapter.MyOnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, "click  "+position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(MainActivity.this, "long click  "+position, Toast.LENGTH_SHORT).show();
            }
        });

MainActivity:

package com.ttt.recyclerviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List data;
    private MyRecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        data=new ArrayList<>();
        for(int i=0;i<20;i++){
            data.add(""+i);
        }

        recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter=new MyRecyclerAdapter(this,data);

+       adapter.setMyOnItemClickListener(new MyRecyclerAdapter.MyOnItemClickListener() {
+           @Override
+           public void onItemClick(View view, int position) {
+               Toast.makeText(MainActivity.this, "click  "+position, Toast.LENGTH_SHORT).show();
+           }
+
+           @Override
+           public void onItemLongClick(View view, int position) {
+               Toast.makeText(MainActivity.this, "long click  "+position, Toast.LENGTH_SHORT).show();
+           }
+       });

        recyclerView.setAdapter(adapter);
    }
}

MyRecyclerAdapter:

package com.ttt.recyclerviewtest;

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;

public class MyRecyclerAdapter extends RecyclerView.Adapter{

    private Context context;
    private List list;
+   private MyOnItemClickListener myOnItemClickListener;

    public MyRecyclerAdapter(Context context, List list){
        super();
        this.context=context;
        this.list=list;
    }

+   public void setMyOnItemClickListener(MyOnItemClickListener myOnItemClickListener){
+       this.myOnItemClickListener=myOnItemClickListener;
+   }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).
                inflate(R.layout.recycler_view_item,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView.setText(list.get(position));

+       final int pos=holder.getAdapterPosition();
+       if(myOnItemClickListener!=null){
+           holder.itemView.setOnClickListener(new View.OnClickListener() {
+               @Override
+               public void onClick(View v) {
+                   myOnItemClickListener.onItemClick(v,pos);
+               }
+           });
+           holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
+               @Override
+               public boolean onLongClick(View v) {
+                   myOnItemClickListener.onItemLongClick(v,pos);
+                   return false;
+               }
+           });
+       }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private TextView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView=(TextView)itemView.findViewById(R.id.recycler_text);
        }
    }

+   public interface MyOnItemClickListener{
+       void onItemClick(View view,int position);
+       void onItemLongClick(View view,int position);
+   }
}

你可能感兴趣的:(RecyclerView)