码迷,mamicode.com
首页 > 移动开发 > 详细

Android 新控件RecyclerView

时间:2015-07-01 18:17:16      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:android   recyclervi   新特性   

RecyclerView 是 android-support-v7-21 版本中新增的一个 Widgets。我们可以使用RecyclerView非常简单的实现横向竖向的ListView.GridView.以及瀑布流的效果.先看一下项目的实现的效果技术分享

是不是很炫?使用RecyclerView可以非常简单的实现.

RecyclerView 特性
1.不关心Item是否显示在正确的位置以及如何显示
2.不关心item之间如何分割
3.不关注Item增加与删除的动画效果
4.它仅仅只是关注如何回收和复用item
贴出部分代码:

     mRecyclerView = (RecyclerView) findViewById(R.id.rv);
        //设置它的Adapter
        myRecyclerViewListAdapater = new MyRecyclerViewListAdapater(this,sourceDatas);
        mRecyclerView.setAdapter(myRecyclerViewListAdapater);


        //设置它的LayoutManage , 通过 LayoutManager去设置它的显示方式
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        //设置分割线
//        dividerItemDecoration = new DividerItemDecoration(this,LinearLayoutManager.VERTICAL);
//        mRecyclerView.addItemDecoration(dividerItemDecoration);

        //设置增加删除Item的动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());


        myRecyclerViewListAdapater.setOnItemClickListener(new MyRecyclerViewListAdapater.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                Toast.makeText(MainActivity.this,"click : " + position,Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onLongItemClickListener(View view, int position) {
                myRecyclerViewListAdapater.deleteItem(position);
                return false;
            }
        });

Adapter代码

public class MyRecyclerViewListAdapater extends RecyclerView.Adapter<MyRecyclerViewListAdapater.MyListHolder> {

    private Context context;
    protected List<String> sourceDatas;
    private LayoutInflater layoutInflater;
    public OnItemClickListener mOnItemClickListener;

    public MyRecyclerViewListAdapater(Context context,List<String> sourceDatas){
        this.context = context;
        this.sourceDatas = sourceDatas;
        layoutInflater = LayoutInflater.from(context);
    }


    @Override
    public MyRecyclerViewListAdapater.MyListHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = layoutInflater.inflate(R.layout.item,viewGroup,false);
        MyListHolder myListHolder = new MyListHolder(view);

        return myListHolder;
    }

    @Override
    public void onBindViewHolder(final MyListHolder viewHolder, final int i) {
        viewHolder.textView.setText(sourceDatas.get(i));
        itemClick(viewHolder);
    }

    /**
     * ItemView点击事件
     */
    protected void itemClick(final MyListHolder viewHolder){

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != mOnItemClickListener){
                    int postion = viewHolder.getLayoutPosition();
                    mOnItemClickListener.onItemClickListener(viewHolder.itemView,postion);
                }
            }
        });

        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(null != mOnItemClickListener){
                    int position = viewHolder.getLayoutPosition();
                    return mOnItemClickListener.onLongItemClickListener(viewHolder.itemView,position);
                }
                return false;
            }
        });
    }

    /**
     * 方法返回item的数量
     * */
    @Override
    public int getItemCount() {
        return sourceDatas.size();
    }

    /**
     * 添加item
     */
    public void addItem(int position){
        sourceDatas.add(position,"Add Item");
        notifyItemInserted(position);
    }

    /**
     * 删除Item
     */
    public void deleteItem(int positon){
        sourceDatas.remove(positon);
        notifyItemRemoved(positon);
    }

    /**
     * 设置item点击接口
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener = onItemClickListener;
    }


    /**
     * Recycler本身并没有给我们提供ItemClick事件,这里需要我们自己实现这个接口
     * 点击接口
     * */
    public interface OnItemClickListener{
        //点击事件
        public void onItemClickListener(View view,int position);
        //长按事件
        public boolean onLongItemClickListener(View view,int position);

    }


    class MyListHolder extends RecyclerView.ViewHolder{

        TextView textView;

        //继承 RecyclerView.ViewHolder 必须要实现它的构造方法
        //可以看到在它的构造方法接收到一个View的参数,这个View就是我们创建的ItemView
        public MyListHolder(View itemView) {
            super(itemView);

            textView = (TextView) itemView.findViewById(R.id.tv_list_item);
        }
    }

}

布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</FrameLayout>

源码地址:http://download.csdn.net/detail/u011068996/8858869

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android 新控件RecyclerView

标签:android   recyclervi   新特性   

原文地址:http://blog.csdn.net/u011068996/article/details/46712285

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!