您的位置:68399皇家赌场 > 集群主机 > 【皇家赌场】选择RecyclerView创制质量越来越好的

【皇家赌场】选择RecyclerView创制质量越来越好的

发布时间:2020-03-25 20:31编辑:集群主机浏览(70)

    在RecyclerView正视的适配器中,无论是适配器依然ViewHolder,从源码我们能够观看,都存在RecyclerView的无名内部类。相对于Listview,RecyclerView内置了万户千门缓存、RecyclerViewPool(从线程的角度,能够领略成相通线程池的东西,即多少个RecyclerView能够公用叁个view)、ViewHolder(已经完毕了复用,相对于Listview的BaseAdapter中getView方法要求开辟者自个儿引进复用难点方便广大卡塔尔国等等。这里大家简要说下五个方法:

    简言之类型Adapter

    private class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
    
        @Override
        public int getItemCount() {
            return COUNT;
        }
    
        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.textView.setText("TEXT_"   position);
        }
    
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_text, parent, false);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }
    
    }
    
    private static class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
    
        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textView);
        }
    }
    

    Android5.0宣布后为大家带来了新的控件RecyclerView。RecyclerView被叫作ListView和GridView的继承者,它应用一种统一的方式结合了那三种视图的应用并提供越来越多采用。通过利用RecyclerView结合差异的LayoutManager我们能够一次性达成线性网格瀑布流三种布局,如若急需,线性列表不再一定是笔直的,还足以横向滚动。其余,RecyclerView还包裹了更加好的ViewHolder和Adapter类,将本来接收ListView时必需由开荒者来促成的复用优化办事封装好。开荒者写越来越少的代码就能够幸免质量不好的标题。那篇短文通过对照ListView和RecyclerView的两样选择方法,来注明为什么大家理应先行利用RecyclerView。
    Android App中支出列表分界面能够做的很简短,每一种列表项仅显示一行文字;也足以做的很复杂,种种列表项满含标题,副标题,按键,复选框,还是可以够响应点击事件等等。这几个视图组件在加载时索要父视图通过findViewById(卡塔尔国二个八个创立。那些函数通过遍历整个布局能源来搜寻目的视图组件,假如客商每滑动叁回列表就遍历一回以至多遍,就能够耳熏目染App的品质,由此三种列表结构都使用ViewHolder来排除这些主题材料。别的,因为能够本着分歧的数据模型定制列表项,所以ListView或许RecyclerView需求有援助类来担当将数据模型适配到视图组件中,那就必要依据“适配器情势”来完结。ListView使用的是ArrayAdapter,而RecyclerView使用的则是Adapter。上边让我们由此对照,来看看RecyclerView的贯彻情势为啥比ListView的完成形式要好。

    接收RecycleView的时候须要在android studio中增多dependency
    compile 'com.android.support:recyclerview-v7:25.1.1'
    

    ------------------------------分割线------------------------------------

    单视图类型SimpleAdapter使用

    public class TextAdapter extends SimpleAdapter<String, TextAdapter.TextHolder> {
    
        public TextAdapter(Context context) {
            super(context);
        }
    
        @Override
        public void onBindViewHolder(TextHolder holder, int position) {
            holder.textView.setText(getItem(position));
        }
    
        @Override
        public TextHolder onCreateViewHolder(ViewGroup parent) {
            View convertView=inflater.inflate(R.layout.item_text, parent, false);
            return new TextHolder(convertView);
        }
    
        static class TextHolder extends ViewHolder{
    
            public TextView textView;
    
            public TextHolder(View itemView) {
                super(itemView);
                textView=(TextView) itemView.findViewById(R.id.textView);
            }
        }
    }
    

    此地大家运用了多个泛型,一个是ViewHolder中帮忙的数据类型String,其余多少个就是大家须要成立的ViewHolder,那样在onCreateViewHolder方法的再次回到值就能够自动重回大家自定义的ViewHolder,有关泛型更加多的学问可以仿效Java泛型使用深入剖判,单视图类型Adapter的选取比RecyclerView的Adapter还要实惠广大。

    2. 代码示例

    皇家赌场,比如大家要得以落成如下所示的列表分界面:

    皇家赌场 1

    示范列表

    只要求在Fragment或Activity中编辑早先化代码,并定义好ViewHolder和艾达pter就可以,注意RecyclerView多出去一步设置LayoutManager。

    public class CrimeListFragment extends Fragment {
        RecyclerView mCrimeRecyclerView;
        CrimeAdapter mCrimeAdapter;
    
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
            mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view);
            mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            updateUI();
            return view;
        }
    
        private class CrimeHolder extends RecyclerView.ViewHolder ... {
            private TextView mTitleTextView;
            private TextView mDateTextView;
            private CheckBox mSolvedCheckBox;
    
            public CrimeHolder(View itemView) {
                super(itemView);
                itemView.setOnClickListener(this);
                mTitleTextView = (TextView) itemView.findViewById(R.id.list_item_crime_title_text_view);
                mDateTextView = (TextView) itemView.findViewById(R.id.list_item_crime_date_text_view);
                mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.list_item_crime_solved_check_box);
            }
    
            public void bindCrime(Crime crime) {
                mTitleTextView.setText(crime.getTitle());
                mDateTextView.setText(crime.getDate().toString());
                mSolvedCheckBox.setChecked(crime.isSolved());
            }
    
            ...
        }
    
        private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
            private List<Crime> mCrimes;
    
            public CrimeAdapter(List<Crime> crimes) {
                mCrimes = crimes;
            }
    
            @Override
            public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(getActivity());
                View view = inflater.inflate(R.layout.list_item_crime, parent, false);
                return new CrimeHolder(view);
            }
    
            @Override
            public void onBindViewHolder(CrimeHolder holder, int position) {
                Crime crime = mCrimes.get(position);
                holder.bindCrime(crime);
            }
    
            @Override
            public int getItemCount() {
                return mCrimes.size();
            }
        }
    
        private void updateUI() {
            mCrimeAdapter = new CrimeAdapter(CrimeLab.get(getActivity()).getCrimes());
            mCrimeRecyclerView.setAdapter(mCrimeAdapter);
        }
    }
    

    选拔RecyclerView写出来的代码要比ListView更易于理解,所以在类型支出中刚烈提出使用那些新的零器件。

    提起底是RecycleView的布局图。

    皇家赌场 2

    image.png

    皇家赌场 3

    Paste_Image.png

    viewType卡塔尔国前回调用viewType,目标是为着依照viewType不一样创设差别的视图。大家得以经过在onCreateViewHolder创制视图的时候,对viewType实行判定,借使加多了尾部,在position

    0的时候回调尾部的viewType给onCreateViewHolder,进而创建尾部。后面部分创造方法于此类同,直接看下代码,适配器的兑现:

    package cn.wsy.recyclerdemo;import android.content.Context;import android.support.v7.widget.GridLayoutManager;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;import java.util.List;/** * Created by wsy on 2016/8/4. */public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> { private RecyclerView mRecyclerView; private List<String> data = new ArrayList<>(); private Context mContext; private View VIEW_FOOTER; private View VIEW_HEADER; //Type private int TYPE_NORMAL = 1000; private int TYPE_HEADER = 1001; private int TYPE_FOOTER = 1002; public MyAdapter(List<String> data, Context mContext) { this.data = data; this.mContext = mContext; } @Override public MyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_FOOTER) { return new MyHolder(VIEW_FOOTER); } else if (viewType == TYPE_HEADER) { return new MyHolder(VIEW_HEADER); } else { return new MyHolder(getLayout(R.layout.item_list_layout)); } } @Override public void onBindViewHolder(MyHolder holder, int position) { if (!isHeaderView && !isFooterView) { if (haveHeaderView position--; TextView content =  holder.itemView.findViewById(R.id.item_content); TextView time =  holder.itemView.findViewById(R.id.item_time); content.setText(data.get); time.setText("2016-1-1"); } } @Override public int getItemCount() { int count = (data == null ? 0 : data.size; if (VIEW_FOOTER != null) { count  ; } if (VIEW_HEADER != null) { count  ; } return count; } @Override public int getItemViewType(int position) { if (isHeaderView) { return TYPE_HEADER; } else if (isFooterView) { return TYPE_FOOTER; } else { return TYPE_NORMAL; } } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { try { if (mRecyclerView == null && mRecyclerView != recyclerView) { mRecyclerView = recyclerView; } ifGridLayoutManager(); } catch (Exception e) { e.printStackTrace(); } } private View getLayout(int layoutId) { return LayoutInflater.from.inflate(layoutId, null); } public void addHeaderView(View headerView) { if (haveHeaderView { throw new IllegalStateException("hearview has already exists!"); } else { //避免出现宽度自适应 ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); headerView.setLayoutParams; VIEW_HEADER = headerView; ifGridLayoutManager(); notifyItemInserted; } } public void addFooterView(View footerView) { if (haveFooterView { throw new IllegalStateException("footerView has already exists!"); } else { ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); footerView.setLayoutParams; VIEW_FOOTER = footerView; ifGridLayoutManager(); notifyItemInserted(getItemCount; } } private void ifGridLayoutManager() { if (mRecyclerView == null) return; final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup = ((GridLayoutManager) layoutManager).getSpanSizeLookup(); ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { return (isHeaderView || isFooterView) ? ((GridLayoutManager) layoutManager).getSpanCount() : 1; } }); } } private boolean haveHeaderView() { return VIEW_HEADER != null; } public boolean haveFooterView() { return VIEW_FOOTER != null; } private boolean isHeaderView(int position) { return haveHeaderView() && position == 0; } private boolean isFooterView(int position) { return haveFooterView() && position == getItemCount() - 1; } public static class MyHolder extends RecyclerView.ViewHolder { public MyHolder(View itemView) { super; } }}
    

    粗略的伊始化RecycerView,以致安装适配器,如下:

     private void initRecyc() {// mRecyclerView.setLayoutManager(new GridLayoutManager; mRecyclerView.setLayoutManager(new LinearLayoutManager; adapter = new MyAdapter(data, this); mRecyclerView.setAdapter; adapter.addFooterView(LayoutInflater.from.inflate(R.layout.item_footer_layout,null)); adapter.addHeaderView(LayoutInflater.from.inflate(R.layout.item_header_layout,null)); }
    

    小编在拉长头顶后面部分的时候,发将来布局RecyclerView,要是格局是构造GridLayoutManager的时候,开采头顶会跑到第一格,也正是否和睦想要独立一行的效能,这里贴上海重机厂点代码,能够解决(轻便数学标题呀哈~):

     private void ifGridLayoutManager() { if (mRecyclerView == null) return; final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup = ((GridLayoutManager) layoutManager).getSpanSizeLookup(); ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { return (isHeaderView || isFooterView) ? ((GridLayoutManager) layoutManager).getSpanCount() : 1; } }); } }
    

    傻小孩b 共勉写给一同奋斗的您

    2016年8月4日

    皇家赌场 4皇家赌场 5

    二. 使用RecyclerView创造列表分界面

    在应用了RecyclerView之后这个干活儿都不用开荒者本身写了,因为RecyclerView都会给你做好。使用RecyclerView最佳的有些是,它降低了开采者不供给的专门的学问量,使得代码看上去更规范,工整。它将这么些优化办事封装到了库中,因此开荒者只要求在Adapter中重载多少个函数,就会成就高质量达成。那也从更加大程度上幸免初行家写出不太好的代码。

    我们以叁个加载天气的体系为例

    每三个RecycleView都必须要绑定一个归属它的Adapter,这一个Adapter用于加载最后展现在显示屏上的数据源,举例天气数据 mweatherData

    皇家赌场 6

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview_forecast);
    mForecastAdapter = new ForecastAdapter();
    mRecyclerView.setAdapter(mForecastAdapter);
    mForecastAdapter.setWeatherData(weatherData);
    

    新建的ForecastAdapter类必得三回九转RecyclerView.Adapter父类,且必得传递叁个继续于RecyclerView.ViewHolder的子类ForecastAdapterViewHolder用作泛型参数,ViewHolder代表了每贰个就要展现在RecyclerView上的子项。我们何足为奇把它看成RecyclerView.艾达pter的中间类来完成。每几个ViewHolder会绑定一个用来展现的子View图。

    皇家赌场 7

    public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder> {
        private String[] mWeatherData;
        public ForecastAdapter() { }
        public class ForecastAdapterViewHolder extends RecyclerView.ViewHolder {
            public final TextView mWeatherTextView;  
            public ForecastAdapterViewHolder(View view) {
                super(view);
                mWeatherTextView = (TextView) view.findViewById(R.id.tv_weather_data);
            }
        }
    }
    

    小编前阵子写了一个万能适配器,提供了上拉加载、上拉刷新的底子成效,主要的是一个底蕴baseAdapter能够支持ListView与RecyclerView,后期提供传送门,现在本人希图一步骤一步骤讲下自家的贯彻思路。

    RecyclerView中Adapter的使用

    在运用RecyclerView的Adapter的时候我们首先须求接二连三RecyclerView的一个静态内部类Adapter,然后重写四个章程,实际上上面七个章程是必须要重写的,因为都是画饼充饥方法。

    • getItemCount()
    • onBindViewHolder(VH holder, int position)
    • onCreateViewHolder(ViewGroup parent, int viewType)

    诚如情状下重写下面多少个办法就可以,不过借使存在多视图类型,在第八个点子
    onCreateViewHolder(卡塔尔(قطر‎方法中大家也足以见到有多少个参数是viewType,该参数成效便是指向性分裂的viewType供给制造区别的ViewHolder,由此还索要重写四个艺术getItemViewType(int position),针对多视图类型同BaseAdapter达成格局倒是很像,在BaseAdapter中那是须要除却还要重写三个格局getViewTypeCount(卡塔尔(قطر‎,不过在RecyclerView的Adapter中无需该方法。

    1. RecyclerView,Adapter和ViewHolder

    RecyclerView结合Adapter和ViewHolder,围绕“适配器形式”提供了一套满足“单一职务标准”的缓和方案。

    “三个类应该唯有三个产生变化的因由”

    内部RecyclerView作为ViewGroup的子类,负担显示列表项,各样列表项都是View的子对象。RecyclerView并不是有微微项就创设多少项,那样相当的轻巧搞垮应用。当客商滑动显示屏时,滑出视图的列表项会被回笼用于展现新的列表项,那正是“RecyclerView”那个名字的由来。ViewHolder不改变,还是承当托管和容纳视图组件,这么些没什么好说的。而Adapter的接纳就标准多了,只须求重载八个函数就可以:

    1. onCreateViewHolder(ViewGroup parent, int viewType)
    2. onBindViewHolder(VH holder, int position)
    3. getItemCount()

    个中onCreateViewHolder(卡塔尔国用于创制ViewHolder对象,onBindViewHolder(State of Qatar用于将数据模型绑定到ViewHolder中的视图组件上,getItemCount(卡塔尔国重临数据模型的数目。当RecyclerView须求出示视图时,就能经过它的Adapter来实现,大约流程如下:

    1. RecyclerView调用Adapter的getItemCount(卡塔尔方法询问数组列表中有多少多少;
    2. RecyclerView调用Adapter的createViewHolder(State of Qatar成立ViewHolder及其要出示的视图;
    3. RecyclerView调用Adapter的onBindViewHolder(卡塔尔(قطر‎方法,传入ViewHolder对象及岗位,Adapter找到对象地方对应的数码,用它绑定到ViewHolder对象容纳的视图上。

    在这之中createViewHolder(卡塔尔调用并不频仍。一旦创设了足足的ViewHolder,RecyclerView就不会再调用createViewHolder(卡塔尔,而是回笼利用旧的ViewHolder来节外省部存款和储蓄器开支。

    RecyclerView是GoogleV7包下新添的控件,用来替代ListView的使用,在RecyclerView标准化了ViewHolder雷同于ListView中convertView用来做视图缓.

    她能够透过安装LayoutManager来神速落成listview、gridview、瀑布流的功力,何况还足以安装横向和纵向显示,增添动漫功用也特别轻易(自带了ItemAnimation,能够设置加载和移除时的卡通,方便做出各类动态浏览的效能卡塔尔,也是官方推荐应用的.以下是官方的表达:

    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. 轻易说便是当你须要动态展示一组数据的时候就能须要利用它。

    本文由68399皇家赌场发布于集群主机,转载请注明出处:【皇家赌场】选择RecyclerView创制质量越来越好的

    关键词: 方法 尾部 头部 实战

上一篇:Android 版本更新(自定义接口公约卡塔尔国

下一篇:没有了