【学习笔记】RecyclerView的使用

为什么叫做RecyclerView?
  • 不关心Item是否显示在正确的位置,如何显示。(LayoutManager)
  • 不关心Item间如何分隔。(ItemDecoration)
  • 不关心Item增加与删除的动画效果。(ItemAnimator)
  • 仅仅关注如何回收与复用View。

RecylerView相关的重要类


  • Adapter
  • ViewHolder
  • LayoutManager
  • ItemDecoration
  • ItemAnimator

RecylerView能干什么?


  • Just like ListView
  • Just like GridView
  • 横向 ListView
  • 横向 GridView
  • 瀑布流
  • 定制Item增加与删除动画(ItemAnimator)
  • 需要手动为Adapter提供Item Click/longClick的回调

RecyclerView关于ListView的实现代码:

package cn.jiarong.myrecyclerview;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

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


public class MainActivity extends ActionBarActivity {

    private RecyclerView mRecyclerView;
    private List<String> mDatas;
    private MyAdapter adapter;

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

        initDatas();

        initViews();
        adapter = new MyAdapter(this, mDatas);
        mRecyclerView.setAdapter(adapter);

        //设置RecyclerView的布局管理
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置RecyclerView的item的分割线
        //mRecyclerView.addItemDecoration(new DividerItemDecoration(this,
                //DividerItemDecoration.VERTICAL_LIST));

        adapter.setmOnItemClickListner(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {

                Toast.makeText(MainActivity.this, pos +" click:",Toast.LENGTH_SHORT).show();
            }

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

    //加载视图
    private void initViews(){
        mRecyclerView = (RecyclerView)findViewById(R.id.mRecyclerView);
    }

    //初始化数据
    private void initDatas(){
        mDatas = new ArrayList<String>();
        for(int i='A';i<='z';i++){
            mDatas.add(""+(char)i);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (id){
            case R.id.action_add:
                adapter.addData(1, "insert a");
                break;
            case R.id.action_delete:
                adapter.deleteData(1);
                break;
            case R.id.action_listview:
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this,
                        LinearLayoutManager.VERTICAL, false));

                break;
            case R.id.action_gridview:
                mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
                break;
            case R.id.action_hor_gridview:
                mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(5,
                        StaggeredGridLayoutManager.HORIZONTAL));
                break;
            case R.id.action_staggered:
                Intent intent = new Intent(this, StaggedActivity.class);
                startActivity(intent);
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}



package cn.jiarong.myrecyclerview;

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

import java.util.List;

/**
 * Created by jiaronggeng on 15/6/25.
 */
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private LayoutInflater mInflater;
    private Context mContext;
    protected List<String> mDatas;

    public interface OnItemClickListener{
        public void onItemClick(View v, int pos);
        public void onItemLongClick(View v, int pos);
    }

    private OnItemClickListener mOnItemClickListner;

    public void setmOnItemClickListner(OnItemClickListener mOnItemClickListner) {
        this.mOnItemClickListner = mOnItemClickListner;
    }

    public void addData(int pos, String str){
        this.mDatas.add(pos, str);
        this.notifyItemInserted(1);
    }

    public void deleteData(int pos){
        this.mDatas.remove(pos);
        this.notifyItemRemoved(1);
    }

    public MyAdapter(Context context, List<String> datas){
        this.mContext = context;
        this.mDatas = datas;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder myViewHolder, final int i) {

        myViewHolder.m_tv.setText(mDatas.get(i));
        setUpEvent(myViewHolder);

    }

    protected void setUpEvent(final MyViewHolder myViewHolder){
        if (mOnItemClickListner != null) {
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int pos = myViewHolder.getLayoutPosition();
                    mOnItemClickListner.onItemClick(myViewHolder.itemView, pos);
                }

            });

            myViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    int pos = myViewHolder.getLayoutPosition();
                    mOnItemClickListner.onItemLongClick(myViewHolder.itemView, pos);

                    return false;
                }
            });
        }

    }

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

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item, viewGroup, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }
}

class MyViewHolder extends ViewHolder{

    TextView m_tv;

    public MyViewHolder(View itemView) {
        super(itemView);
        m_tv = (TextView)itemView.findViewById(R.id.m_tv);

    }
}



瀑布流

package cn.jiarong.myrecyclerview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

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


public class StaggedActivity extends Activity {

    private RecyclerView mRecyclerView;
    private List<String> mDatas;
    private StaggedAdapter adapter;

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

        initDatas();

        initViews();
        adapter = new StaggedAdapter(this, mDatas);
        mRecyclerView.setAdapter(adapter);

        //设置RecyclerView的布局管理
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,
                StaggeredGridLayoutManager.VERTICAL));

        adapter.setmOnItemClickListner(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {

            }

            @Override
            public void onItemLongClick(View v, int pos) {
                //长按删除
                adapter.deleteData(pos);
            }
        });

    }

    //加载视图
    private void initViews(){
        mRecyclerView = (RecyclerView)findViewById(R.id.mRecyclerView);
    }

    //初始化数据
    private void initDatas(){
        mDatas = new ArrayList<String>();
        for(int i='A';i<='z';i++){
            mDatas.add(""+(char)i);
        }
    }


}

package cn.jiarong.myrecyclerview;

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

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

/**
 * Created by jiaronggeng on 15/6/25.
 */
public class StaggedAdapter extends MyAdapter {

    private List<Integer> listHeight;

    public StaggedAdapter(Context context, List<String> datas){
        super(context, datas);

        listHeight = new ArrayList<Integer>();
        for(int i=0;i<mDatas.size();i++){
            listHeight.add((int)(100+Math.random()*300));
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, int position) {

        ViewGroup.LayoutParams lp = myViewHolder.itemView.getLayoutParams();
        lp.height = listHeight.get(position);
        myViewHolder.itemView.setLayoutParams(lp);
        myViewHolder.m_tv.setText(mDatas.get(position));

        setUpEvent(myViewHolder);
    }


}






【学习笔记】RecyclerView的使用

【学习笔记】RecyclerView的使用

【学习笔记】RecyclerView的使用

【学习笔记】RecyclerView的使用

你可能感兴趣的:(RecyclerView)