Simple ViewHolder and viewholder

Source: Internet
Author: User
Tags phoneview

Simple ViewHolder and viewholder

This method is written by a foreign company.

Provides a static method:

public class ViewHolder {    @SuppressWarnings("unchecked")    public static <T extends View> T get(View view, int id) {        SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();        if (viewHolder == null) {            viewHolder = new SparseArray<View>();            view.setTag(viewHolder);        }        View childView = viewHolder.get(id);        if (childView == null) {            childView = view.findViewById(id);            viewHolder.put(id, childView);        }        return (T) childView;    }}

Use the following in getView:

public View getView(int position, View convertView, ViewGroup parent) {    if (convertView == null) {        convertView = LayoutInflater.from(context)          .inflate(R.layout.banana_phone, parent, false);    }    ImageView bananaView = ViewHolder.get(convertView, R.id.banana);    TextView phoneView = ViewHolder.get(convertView, R.id.phone);    BananaPhone bananaPhone = getItem(position);    phoneView.setText(bananaPhone.getPhone());    bananaView.setImageResource(bananaPhone.getBanana());    return convertView;}
If you are interested, pay attention to the subscription Id love development:
No.: aikaifa
Or scan the QR code below:
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.