适配器Adapter

来源:互联网 发布:电容感应开关单片机 编辑:程序博客网 时间:2024/05/12 17:06
  • 1.谈到Adapter就忍不到要跳转到适配器模式,Adapter适配器可以简单理解为一个缓存绑定器,数据绑定视图,现在较流行的BataBinding框架该是对适配器模式的进一步封装了。

  • 2.结合官网,说说几个概念

    • 2.1.Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView. responsible for providing views that represent items in a data set.所以它作为绑定器,不仅需要提供item视图还需要提供item数据集,也就是为什么我们在其构造函数中往往需要提供item View 和data Set 的原因,item view 用于复用,data Set 会被adapter解析分割为item Data。

    • 2.2.下标存在两个量: The position of a data item within an Adapter. 一个是适配器中索引item data的position量;The index of an attached child view as used in a call to getChildAt(int). Contrast with Position. 一个是子视图item view 的索引量 index。

    • 2.3.缓存:数据缓存和视图缓存,重点讲解下视图缓存,即视图复用,A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction. 这段文字概述的很精辟了,有一点很重要的信息就是系统已经为我们复用视图提供了解决机制,不用像之前的listView那样需要手动来覆写viewholder,不过这也提供了一个优化性能的方案,即减少重复性的初始化布局构造。

    • 2.4.dirty thing 脏东西:脏数据和脏视图,是个业务概念,这个概念经常出现在数据库,尤其是事务回滚相关操作中,即依据脏数据所做的操作可能是不正确的,A dirty child view that must be rebound by the adapter before being displayed. And A Scrap child view that has entered into a temporarily detached state during layout. Scrap views may be reused without becoming fully detached from the parent RecyclerView, either unmodified if no rebinding is required or modified by the adapter if the view was considered dirty. 这个机制是由系统来保证的,其界定的界限也是由系统来制定的。

  • 3.使用RecyclerView 来代替ListView 已经成为了一个标准,其插拔式的体验,高度的解耦机制,使得层次更清晰了,说白了OO化了,不同层次的功能依次封装为了一个个内部类。

    • 3.1.LayoutManager: 实现item列表以何种形式显示。RecyclerView allows client code to provide custom layout arrangements for child views. 子item的整体列表布局,区别于单个item view 的具体样式。

    • 3.2.ViewHolder: A ViewHolder describes an item view and metadata about its place within the RecyclerView。

    • 3.3.ItemDecoration: An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter’s data set. This can be useful for drawing dividers between items, highlights, visual grouping boundaries and more. 装饰,用于区别item view 的,可以个性化设置风格线,其实在实际编程中,可以忽略,利用item View自身的布局样式来区分即可。

    • 3.4. ItemAnimator: defines the animations that take place on items as changes are made to the adapter. 各种交互事件的动画效果。

    /*    * 两个参数:    * @parame: currentPage 当前页数    * @parame: isRefreshOrFirstLoading 首次登陆或者刷新    * */    private void getDataFromServer(final int currentPage, final boolean isRefreshOrFirstLoading) {        BaseApplication.getApplication().getAppAction().getProductListByNormalSort(currentPage, PAGESIZE, new                ActionCallbackTripleListener<List<SortDataBean>, String, String>() {                    @Override                    public void onSuccess(List<SortDataBean> data, String pageIndex, String total) {                        dialog.cancel();                        CurrentPage = Integer.parseInt(pageIndex);                        totalCount = Integer.parseInt(total);                        if (data.isEmpty()) {                            isListToBottom = true;                        }                        if (totalCount == 0) {                            adapter.setEmptyView(true, getEmptyView());                            mSuperRecyclerViewImg.setAdapter(adapter);                        } else {                            if (adapter.getData().isEmpty()) {                                adapter.setNewData(data);                                adapter.openLoadMore(PAGESIZE);                                adapter.removeAllFooterView();                            } else {                                if (!isRefreshOrFirstLoading) {                                    Log.d(TAG, "下拉加载前:" + adapter.getData()                                            .size());                                    if (isListToBottom) {                                        mSuperRecyclerViewImg.hideMoreProgress();                                        adapter.loadComplete();                                        if (notLoadingView == null) {                                            notLoadingView = inflater.inflate(R.layout.not_loading,                                                    (ViewGroup) mSuperRecyclerViewImg.getParent                                                            (), false);                                        }                                        adapter.addFooterView(notLoadingView);//可以暂时先把这个给添加上                                    } else {                                        adapter.addData(data);                                        Log.d(TAG, "下拉加载后:" + adapter.getData()                                                .size());                                    }                                } else {                                    Log.d(TAG, "刷新");                                    if (!data.isEmpty()) {                                        isListToBottom = false;                                        adapter.setNewData(data);                                        adapter.openLoadMore(PAGESIZE);                                        adapter.removeAllFooterView();// 这几个必须添加上                                    }                                    Log.d(TAG, "第一次加载后:" + adapter.getData()                                            .size());                                }                            }                        }                        mSuperRecyclerViewImg.getSwipeToRefresh().setRefreshing(false);//隐藏刷新的图标                        mSuperRecyclerViewImg.hideMoreProgress(); //数据成功后,上拉加载和下拉刷新的图标去除                    }                    @Override                    public void onFailure(String errorEvent, String errorMessage) {                        Log.d(TAG, errorEvent);                        Log.d(TAG, errorMessage);                        if ("998".equals(errorEvent) && requestCount > 0) {                            requestCount--;                            refreshData(isRefreshOrFirstLoading);                        }else if ("003".equals(errorEvent)) {                            ToastUtil.networkUnavailable();                            handleError(isRefreshOrFirstLoading);                        } else {                            handleError(isRefreshOrFirstLoading);                        }                    }                });    }

参考文档:

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemDecoration.html
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemAnimator.html
http://blog.csdn.net/u013606974/article/details/51837314?locationNum=15
http://baike.baidu.com/link?url=MhK1b6mN8blfVHg2frG1Q4RVJgoQMTaxP9g5769xCbN2b3sL_glzNfRBKxkxs91Vww6HQ4JUJVBMy-COA4FfF_

0 0
原创粉丝点击