zoukankan      html  css  js  c++  java
  • Android RecyclerView初探

    原文:https://github.com/dyguests/RecyclerViewDemo

    RecyclerView是用于替换ListView的解决方案,将视图控制和数据绑定做了分离,提供了强大的样式定制能力

    使用教程

    1.准备工作

    在 module 的 build.gradle 中 加入 compile 'com.android.support:recyclerview-v7:23.3.0' 代码:

    dependencies {
        ...
        compile 'com.android.support:recyclerview-v7:23.3.0'//1.1
        ...
    }
    

    2.使用RecyclerView

    2.1.在xml中添加RecyclerView

    activity_linear_layout_manager.xml

    <RelativeLayout
        ...>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:listitem="@layout/item_linear"/>
    </RelativeLayout>

    注:tools:listitem="@layout/item_linear"是用于在Android Studio的Preview中查看效果用的

    2.2.在java中添加RecyclerView

    LinearLayoutManagerActivity.java

    2.2.1.绑定

    mRecyclerView = ((RecyclerView) findViewById(R.id.recycler_view));

    2.2.2.添加LayoutManager(布局方式)

    mRecyclerView.setLayoutManager(new LinearLayoutManager(this,
                    LinearLayoutManager.VERTICAL/*方向*/,
                    false/*是否反向*/
            ));
    

    注:这里用LinearLayoutManager表示这是使用线性布局,也可以使用GridLayoutManager等.

    2.2.3.绑定Adapter(参考 2.3.创建Adapter)

    mAdapter = new LinearLayoutManagerAdapter(this);
            mRecyclerView.setAdapter(mAdapter);
    

    2.2.4.添加数据

    ...
    mAdapter.addItem(user);
    ...
    

    也可以使用

    ...
    mAdapter.addItems(users);
    ...
    

    注:addItem(),addItems()方法是自定义的,主要是为了把数据添加操作和动画放在一起.

    2.3.创建Adapter

    2.3.1.创建LinearLayoutManagerAdapter

    public class LinearLayoutManagerAdapter{
    
    }
    

    2.3.2.让类继承RecyclerView.Adapter

    public class LinearLayoutManagerAdapter extends RecyclerView.Adapter{
    
    }
    

    2.3.3.注意,RecyclerView.Adapter是泛型类,需要一个继承至RecyclerView.ViewHolder的泛型参数(?),这里我们创建一个ViewHolder的内部类:

    public class LinearLayoutManagerAdapter extends RecyclerView.Adapter{
    
        public class ViewHolder extends RecyclerView.ViewHolder{
    
        }
    }
    

    2.3.4.添加泛型声明

    public class LinearLayoutManagerAdapter extends RecyclerView.Adapter<LinearLayoutManagerAdapter.ViewHolder>{
    
        public class ViewHolder extends RecyclerView.ViewHolder{
    
        }
    }
    

    2.3.5.实现方法

    public class LinearLayoutManagerAdapter extends RecyclerView.Adapter<LinearLayoutManagerAdapter.ViewHolder>{
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }
    
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
    
        }
    
        @Override
        public int getItemCount() {
            return 0;
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder{
    
            public ViewHolder(View itemView) {
                super(itemView);
            }
        }
    }

    2.3.6.添加构造器

    public class LinearLayoutManagerAdapter extends RecyclerView.Adapter<LinearLayoutManagerAdapter.ViewHolder>{
    
        private final Context context;
    
        public LinearLayoutManagerAdapter(Context context) {
            this.context = context;
        }
    
        ...
    }

    2.3.7.指定item_view,注意这里与ListView不同,这里不返回View,而是返回ViewHolder(自已定义的).

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

    2.3.8.添加数据源

    public class LinearLayoutManagerAdapter extends RecyclerView.Adapter<LinearLayoutManagerAdapter.ViewHolder>{
        ...
        private final ArrayList<User> list;
    
    
        public LinearLayoutManagerAdapter(Context context) {
            this.context = context;
            list = new ArrayList<>();
        }
        ...
        @Override
        public int getItemCount() {
            return list.size();
        }
        ...
    }

    2.3.9.实现list_item与数据的绑定

    public class LinearLayoutManagerAdapter extends RecyclerView.Adapter<LinearLayoutManagerAdapter.ViewHolder>{
        ...
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.bind(list.get(position));
        }
        ...
        public class ViewHolder extends RecyclerView.ViewHolder{
            ...
            public void bind(User data) {
            }
        }
    }

    2.3.10.在ViewHolder中实现布局元素的绑定与数据的绑定

    public class ViewHolder extends RecyclerView.ViewHolder {
            private final TextView mTextView;
            private       User     data;
    
            public ViewHolder(View itemView) {
                super(itemView);
                mTextView = ((TextView) itemView.findViewById(R.id.text_view));
            }
    
            public void bind(User data) {
                mTextView.setText(data.getName());
    
                this.data = data;
            }
        }

    这里注意一下,请尽量把ViewHolder相关的布局元素绑定,数据绑定放到ViewHolder中来实现,而不要放到onCreateViewHolder(),onBindViewHolder()中去实现.

    2.3.11.其它相关代码

        //-----------------------数据增删相关-------------------------------
    
        public void addItem(User item) {
            int positionStart = list.size();
            list.add(item);
            notifyItemInserted(positionStart);
        }
    
        public void addItems(List<User> items) {
            int positionStart = list.size();
            list.addAll(items);
            notifyItemRangeInserted(positionStart, items.size());
        }
    
        public void clear() {
            int itemCount = list.size();
            list.clear();
            notifyItemRangeRemoved(0, itemCount);
        }

    注:这段代码是为了方便把数据增删和动画效果绑定要一起

    3.其它几种例子

    LinearLayoutManagerActivity

    GridLayoutManagerActivity

    InContainerActivity

  • 相关阅读:
    webpack2 前篇
    vue 的调试工具
    CSS 命名规范总结
    reset.css
    推荐几个精致的web UI框架
    自己是个菜鸟 自己查找的简单的适合初学的Makefile
    Linux下编译、使用静态库和动态库 自己测过的
    函数参数的传递 动态内存传递问题(指针的指针)
    二级指针 (C语言)
    find_if查找vector内对象的成员 作为菜鸟一直不会用也不敢用
  • 原文地址:https://www.cnblogs.com/anywherego/p/5416287.html
Copyright © 2011-2022 走看看