viewholder

说说ViewHolder的另一种写法

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 17:34:02
每为一个AdapterView写Adapter适配器,你是否都会创建 一个static的ViewHolder类来负责临时存储ItemView?不用回答我也已经听见了你的抱怨,那么下面为大家介绍另外一种我经常使用的写法,从此和无尽的ViewHolder说拜拜,而且不会影响页面加载的性能。 Android为开发者提供了ListView和GridView这两个常用于显示列表的控件。在不使用ViewHolder的情况下过程中,列表子View数量不多的情况下,可能不大看的出列表加载性能上的差异。但是相信在菜鸟阶段的时候,每一位都亲测过在上述情况下,列表展示较多数据时加载是何等的坑爹,会出现滑动不畅呀的现象。出现的原因呢就是因为加载了过多的子View,每一个子View都需要查找id呀,界面重绘呀等操作,这样导致了内存开销过大以及手机绘图的效率跟不上,才会出现前面我们说的滑动较卡的现象。所以Android为我们开发者提供了标准的ViewHolder的写法来重用列表的ItemView,来避免出现上述两个问题。最标准的写法,就是为每一个AdapterView的子View新建一个对应的ViewHolder,同时声明为prtivate final static。 好了,背景介绍完毕,上代码。 上面两张图片展示了ViewHolder和它的使用。可以看到这里定义的ViewHolder代码是十分的简单

Android ViewHolder简洁写法及替代findViewById方法

邮差的信 提交于 2019-12-10 16:58:33
ViewHolder简洁写法,避免适配器中重复定义ViewHolder: import android.util.SparseArray; import android.view.View; @SuppressWarnings({"unchecked"}) public class ViewFindUtils { /** * ViewHolder简洁写法,避免适配器中重复定义ViewHolder,减少代码量用法: * <p/> * <pre> * if (convertView == null) * { * convertView = View.inflate(context, R.layout.ad_demo, null); * } * TextView tv_demo = ViewHolderUtils.get(convertView, R.id.tv_demo); * ImageView iv_demo = ViewHolderUtils.get(convertView, R.id.iv_demo); * </pre> */ public static <T extends View> T hold(View view, int id) { SparseArray<View> viewHolder = (SparseArray<View>) view.getTag(); if

viewholder 简洁写法

拈花ヽ惹草 提交于 2019-12-10 16:58:14
ViewHolder是什么就不解释了。 大家通常怎么写ViewHolder呢? ViewHolder holder = null; if(convertView == null){ convertView = mInflater.inflate(R.layout.xxx null); holder = new ViewHolder(); holder.tvXXX = (TextView)findViewById(R.id.xxx); //...一连串的findViewById } else{ holder = (ViewHolder) convertView.getTag(); } private static class ViewHolder{ TextView tvXXX; //很多view的定义 } 这么写一次还行,但问题是总有很多很多的ViewAdapter要这么写,每次都repeat,repeat,repeat 累啊。 所以,有这么一种简洁的写法分享给大家,先声明,从国外网站上看的,不是自己原创的,但确实很喜欢这个简洁的设计。 ViewHolder这么写(只提供一个静态方法,其实可以加一个私有构造函数防止外部实例化),代码很简单,看过就明白了 public class ViewHolder { @SuppressWarnings("unchecked") public

ViewHolder模式超简洁写法,很cool!

社会主义新天地 提交于 2019-12-10 16:37:30
ViewHolder是什么就不解释了。大家通常怎么写ViewHolder呢? ViewHolder holder = null; if(convertView == null) { convertView = mInflater.inflate(R.layout.xxx null); holder = new ViewHolder(); holder.tvXXX = (TextView)findViewById(R.id.xxx); //...一连串的findViewById } else { holder = (ViewHolder) convertView.getTag(); } private static class ViewHolder{ TextView tvXXX; //很多view的定义 } 这么写一次还行,但问题是总有很多很多的ViewAdapter要这么写,每次都repeat,repeat,repeat 累啊。所以,有这么一种简洁的写法分享给大家,先声明,从国外网站上看的,不是自己原创的,但确实很喜欢这个简洁的设计。 ViewHolder这么写(只提供一个静态方法,其实可以加一个私有构造函数防止外部实例化),代码很简单,看过就明白了 public class ViewHolder { // I added a generic return type to

第十次作业

那年仲夏 提交于 2019-12-10 13:22:58
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#E6E6E6" android:orientation="vertical" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="名称:" android

第十次作业

只愿长相守 提交于 2019-12-08 18:58:28
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#E6E6E6" android:orientation="vertical" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="名称:" android

第十次作业

谁都会走 提交于 2019-12-08 17:57:32
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#E6E6E6" android:orientation="vertical" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="名称:" android

第十次作业

感情迁移 提交于 2019-12-08 00:37:27
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#E6E6E6" android:orientation="vertical" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="名称:" android

Android之史上最强ListView优化提案

五迷三道 提交于 2019-12-07 20:18:27
Android之史上最强ListView优化提案 www.MyException.Cn 网友分享于:2015-08-04 浏览:0次 Android之史上最强ListView优化方案 在android开发中Listview是一个很重要的组件,它以列表的形式根据数据的长自适应展示具体内容,用户可以自由的定义listview每一列的布局,但当listview有大量的数据需要加载的时候,会占据大量内存,影响性能。 本文的重点即是从如下几个方面介绍如何对ListView进行优化。 1、convertView重用 Android SDK中这样讲: the old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view 利用好 convertView 来重用 View,切忌每次 getView() 都新建。ListView 的核心原理就是重用 View,如果重用 view 不改变宽高

【Android】RecyclerView

南楼画角 提交于 2019-12-07 14:26:26
#RecyclerView ##RecyclerView简明 RecyclerView 在v7.21+包中,是一个用来展示大量数据的组件,或者说,就是ListView的改善版本(注:现阶段的功能没有ListView完善,因此想完全取代ListView的话并不明智)。 相比ListView,RecyclerView的扩展性更好,因此也更适合与android新曾的组件配合使用,这样使用起来更得心应手。 RecyclerView与ListView的原理差不多,本质上都是以适配器为核心。只不过ListView缓存的是view,viewHolder附着在view上,而RecyclerView缓存的是viewHolder,view包含在viewHolder内。 如果我们将ListViewAdapter稍作修改,也可以实现这种设计: abstract class LvAdapter<VH extends LvAdapter.ViewHolder> extends BaseAdapter { public class ViewHolder { private View view; public ViewHolder(View view) { this.view = view; } public View getView() { return view; } } @Override public