Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Android data Adapter ViewHolder

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Android data adapter ViewHolder how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Android data adapter ViewHolder how to use the article will have a harvest, let's take a look.

When using Listview or GridView, you often need a custom data adapter, which usually overrides getView (). In this method, there is a convertView parameter, which is the View used to load the data.

A simple but inefficient way for beginners

Public View getView (int position, View convertView, ViewGroup parent) {View item= inflater.inflate (R.layout.good_list_item, null, false); ImageView img = (ImageView) item.findViewById (R.id.img); TextView price = (TextView) item.findViewById (R.id.price); img.setImageResource (R.drawable.ic_launcher); price.setText ("$" + list.get (position) .price); return item;}

Every time you load view, you have to re-create a lot of view objects, and if there are 10, 000 pieces of data in a listview, this loading method will stop.

Using convertView

Using the Recycler mechanism of Android and using convertView to recycle View, the efficiency has been essentially improved. Each creation of a View is time-consuming, so the convertView passed in by the getview method should make full use of the judgment of! = null.

Public View getView (int position, View convertView, ViewGroup parent) {if (convertView==null) {convertView= inflater.inflate (R.layout.good_list_item, null, false);} TextView tv_price = (TextView) convertView.findViewById (R.id.price) ImageView iv = (ImageView) convertView.findViewByID (R.id.img); return convertView;}

Use ViewHolder

ViewHolder encapsulates the view that needs to be cached, and the setTag of convertView caches these for next call. When the layout of your listview is diversified, the role of viewholder is obvious, and the efficiency is improved again. The findViewById () method of View is also time-consuming, so you need to consider calling it only once, and then using the View.getTag () method to get the ViewHolder object.

Class ViewHolder {ImageView img; TextView price;} public View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder = new ViewHolder (); if (convertView==null) {convertView= inflater.inflate (R.layout.good_list_item, null, false); holder.img = (ImageView) convertView.findViewById (R.id.img); holder.price = (TextView) convertView.findViewById (R.id.price) ConvertView.setTag (holder);} else {holder = (ViewHolder) convertView.getTag ();} / / set holder holder.img.setImageResource (R.drawable.ic_launcher); holder.price.setText ("$" + list.get (position) .price); return convertView;}

Elegant use of ViewHolder

When using ViewHolder, you add the definition of View to ViewHolder over and over again with findViewById. If there is more than one view, is it annoying? the library base-adapter-helper seems to solve this problem.

The design idea is to use SparseArray to store references to view, instead of the original ViewHolder, without declaring a lot of View, concise and clear.

I also wrote a simple version of ViewHolder myself.

Public class ViewHolder {private final SparseArray views; private View convertView; private ViewHolder (View convertView) {this.views = new SparseArray (); this.convertView = convertView; convertView.setTag (this);} public static ViewHolder get (View convertView) {if (convertView = = null) {return new ViewHolder (convertView) } ViewHolder existedHolder = (ViewHolder) convertView.getTag (); return existedHolder;} public T getView (int viewId) {View view = views.get (viewId); if (view = = null) {view = convertView.findViewById (viewId); views.put (viewId, view);} return (T) view;}}

If you use it, it's super simple and simple:

Public View getView (int position, View convertView, ViewGroup parent) {if (convertView = = null) {convertView = LayoutInflater.from (context) .propagate (R.layout.good_list_item, null, false);} ViewHolder mViewHolder = ViewHolder.get (convertView); TextView price = mViewHolder.getView (R.id.price); / /. Other getView return convertView;} article on "how to use Android data Adapter ViewHolder" ends here. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use Android data adapter ViewHolder". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report