RecyclerView

1support v7包中的新控件

RecyclerView is a more advanced and flexible version of ListView. This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the RecyclerView widget when you have lists with elements that change dynamically.

2几个优点:

奋斗封装了viewHolder的回收和复用。

奋斗RecyclerView使用了布局管理器管理子view的位置,目前只提供了LinearLayout,但LinearLayout可以指定方向,默认是竖直的,可以指定为水平的,不需要再拘泥于ListView的线性展示方式,如果之后提供其他custom LayoutManager的支持,就可以使用复杂的布局展示一个动态组件。

奋斗自带了ItemAnimation,可以设置加载和移除时的动画,方便做出各种动态浏览的效果。

3给出一段MainActivity中的完整代码,但是建议,adapter写在另外的Adapter包里,减少耦合,段段说习惯是最重要的。

<span style="font-size:18px;">package com.example.a.recyclerview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
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.ArrayList;
/**
 * Created by ruyu on 2015/8/13.
 */
public class MainActivity extends Activity {

    //三个要素
    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private RecyclerView.Adapter itemAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview_main);
        /***
         * <span style="font-size:18px;">1,找到view,RecyclerView只负责UI的显示和对数据集处理</span>
         */
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        /***
         * <span style="font-size:18px;">2,为RecyclerView指定一个类型为LinearLayoutManager的layoutmanager,
         *   用来指定每个单元View的具体战士形式</span>
         */
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        ArrayList<String> infos = new ArrayList<String>();
        for (int a = 0; a < 50; a++) {
            infos.add("第" + (a + 1) + "行");
        }

        /***
         *<span style="font-size:18px;"> 3,创建一个名为ItemAdapter.java的类来作为RecyclerView的适配器,继承自RecyclerView.Adapter,
         *需要传入一个RecyclerView.ViewHolder的实现类来保证ViewHolder的实现</span>
         */
        itemAdapter = new ItemAdapter(infos);
        mRecyclerView.setAdapter(itemAdapter);
    }

   <span style="font-size:18px;"> /**
     * 至少需要实现三个方法:
     * ①onCreateViewHolder(根据viewType参数创建对应的子view)
     * ②onBindViewHolder(将数据根据指定位置绑定到对应位置的viewHolder上)
     * ③getItemCount(RecyclerView的长度)
     */</span>
    public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.viewHolder> {
        private ArrayList<String> infos =new ArrayList<String>();

        public ItemAdapter(ArrayList<String> infos) {
            this.infos = infos;
        }

        public viewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_card,
                    viewGroup, false);
            return new viewHolder(view);
        }

        public void onBindViewHolder(viewHolder viewHolder, int position) {
            String info = infos.get(position);
            View view = viewHolder.itemView;
            TextView textView = (TextView) view.findViewById(R.id.info_text);
            textView.setText(info);
        }

        <span style="font-size:18px;">/***
         * 返回数据的size,决定列表的长度
         * @return
         */</span>
        public int getItemCount() {
            return infos.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {

            public viewHolder(View itemView) {
                super(itemView);
            }
        }
    }
}

</span>
相关文章
相关标签/搜索