使用listView的时候,通过ViewHolder进行缓存可以提升性能

JavaBean,创建了News对象的几个参数title,detail,comment,imageUrl

package com.ldw.newsView.domain;public class News {private String title;private String detail;private String comment;private String iamgeUrl;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getDetail() {return detail;}public void setDetail(String detail) {this.detail = detail;}public String getComment() {return comment;}public void setComment(String comment) {this.comment = comment;}public String getIamgeUrl() {return iamgeUrl;}public void setIamgeUrl(String iamgeUrl) {this.iamgeUrl = iamgeUrl;}@Overridepublic String toString() {return "News [title=" + title + ", detail=" + detail + ", comment="+ comment + ", iamgeUrl=" + iamgeUrl + "]";}}

布局文件

activity_main.xml主布局,用ListView进行填充

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><ListViewandroid:id="@+id/lv"android:layout_width="match_parent"android:layout_height="match_parent"/></RelativeLayout>

ListView的布局文件,每一个的布局

item_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><com.loopj.android.image.SmartImageView android:id="@+id/siv"android:layout_height="90sp"android:layout_width="70sp"android:src="@drawable/ic_launcher"android:layout_centerVertical="true"/><TextViewandroid:id="@+id/tv_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="XX"android:layout_toRightOf="@id/siv"android:textSize="22sp"android:singleLine="true"/><TextViewandroid:id="@+id/tv_detail"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Xomatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'ldd'omatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'ldd'X"android:layout_toRightOf="@id/siv"android:textSize="14sp"android:textColor="@android:color/darker_gray"android:layout_below="@id/tv_title"android:lines="2"/><TextView android:id="@+id/tv_comment"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="213评论"android:textColor="#ff0000"android:layout_alignParentRight="true"android:layout_below="@id/tv_detail"/></RelativeLayout>

主布局文件,首先调用getNewsInfo利用子线程来请求xml并解析xml文件中的元素,同时把元素放在List<News>对象中,然后在setAdapter中来通过ViewHolder来获取到List中的News对象来解析每一条的元素,并进行显示

package com.ldw.newsView;import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;import org.xmlpull.v1.XmlPullParser;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Xml;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;import com.ldw.newsView.domain.News;
import com.loopj.android.image.SmartImageView;public class MainActivity extends Activity {List<News> newsList;Handler handler = new Handler(){public void handleMessage(android.os.Message msg){ListView lv = (ListView) findViewById(R.id.lv);lv.setAdapter(new MyAdapter());}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getNewsInfo();//ListView lv = (ListView) findViewById(R.id.lv);//需保证解析结束以后,再设置适配器//lv.setAdapter(new MyAdapter());}class MyAdapter extends BaseAdapter{//得到元素的数量,确定listView条目的数量@Overridepublic int getCount() {// TODO Auto-generated method stubreturn newsList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {News news = newsList.get(position);View v = null;ViewHolder mHolder;if(convertView == null){v = View.inflate(MainActivity.this, R.layout.item_listview, null);//布局文件中所有组件的对象封装到ViewHolder对象中mHolder = new ViewHolder();mHolder.tv_title = (TextView) v.findViewById(R.id.tv_title);mHolder.tv_detail = (TextView) v.findViewById(R.id.tv_detail);mHolder.tv_comment = (TextView) v.findViewById(R.id.tv_comment);mHolder.siv = (SmartImageView) v.findViewById(R.id.siv);//把ViewHolder对象封装到View对象中v.setTag(mHolder);}else{v = convertView;mHolder = (ViewHolder) v.getTag();}mHolder.tv_title.setText(news.getTitle());mHolder.tv_detail.setText(news.getDetail());mHolder.tv_comment.setText(news.getComment() + "条评论");mHolder.siv.setImageUrl(news.getIamgeUrl());/*TextView tv_title = (TextView) v.findViewById(R.id.tv_title);tv_title.setText(news.getTitle());TextView tv_detail = (TextView) v.findViewById(R.id.tv_detail);tv_detail.setText(news.getDetail());TextView tv_comment = (TextView) v.findViewById(R.id.tv_comment);tv_comment.setText(news.getComment() + "条评论");SmartImageView siv = (SmartImageView) v.findViewById(R.id.siv); siv.setImageUrl(news.getIamgeUrl());*/return v;}class ViewHolder{//条目的布局文件中有什么组件,这里就定义有什么属性TextView tv_title;TextView tv_comment;TextView tv_detail;SmartImageView siv;}}private void getNewsInfo(){Thread t = new Thread(){@Overridepublic void run(){String path="http://192.168.0.102:8080/news.txt";try {URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);conn.setReadTimeout(5000);//发送http请求,获取相应的状态吗if(conn.getResponseCode() == 200){InputStream is = conn.getInputStream();//使用pull解析器解析is流parseNewsXml(is);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}};t.start();}private void parseNewsXml(InputStream is){XmlPullParser xp = Xml.newPullParser();try {xp.setInput(is, "utf-8");//对节点的事件类型进行判断,可以知道当前的节点类型int type = xp.getEventType();News news = null;while(type != XmlPullParser.END_DOCUMENT){switch(type){case XmlPullParser.START_TAG:if("newsList".equals(xp.getName())){newsList = new ArrayList<News>();}else if("news".equals(xp.getName())){news = new News();}else if("title".equals(xp.getName())){String title = xp.nextText();news.setTitle(title);}else if("detail".equals(xp.getName())){String detail = xp.nextText();news.setDetail(detail);}else if("comment".equals(xp.getName())){String comment = xp.nextText();news.setComment(comment);}else if("image".equals(xp.getName())){String image = xp.nextText();news.setIamgeUrl(image);}break;case XmlPullParser.END_TAG:if("news".equals(xp.getName())){newsList.add(news);}break;}//解析玩借点以后,把指针移动到下一个节点,返回他的时间类型type = xp.next();}//发消息让主线程设置listView适配器,因为消息不需要携带数据,发送空消息handler.sendEmptyMessage(1);for(News n : newsList){System.out.println(n.toString());}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

ViewHolder的使用相关推荐

  1. Android listview viewholder

    2019独角兽企业重金招聘Python工程师标准>>> Android ListView ViewHolder 利用adapter中的getView的 contentView 的复用 ...

  2. viewholder

    viewholder的作用是减少findViewById的调用,将控件的引用存入,通过steTag()来调用. 首先判断convertView有没有内容,没有就定义一个ViewHolder对象,将布局 ...

  3. Android之ViewHolder用法

    先声明一下ViewHolder在Android自定义的适配器中使用.目的:优化资源,节省空间,避免重复绘制view而引起的不必要的内存损耗. 我自己以前的写法: [html] view plainco ...

  4. ListView Viewholder的坑 线性布局的坑

    1.ListView Viewholder的坑 /*** 默认带图片的menu adapter*/public static class MenuImageAdapter extends BaseAd ...

  5. ViewHolder VS HolderView ?

    ViewHolder 模式在 Android 中大家应该都不陌生了,特别是在 ListView 中通过 ViewHolder 来减少 findViewById 的调用和 类型的转换. 而 Holder ...

  6. Android BaseAdapter和ViewHolder 优化 解决ListView的item抢焦点问题和item错乱问题

    首先赞下hyman大神 曾经仅仅是简单的重写个BaseAdapter,将getView方法保持抽象.而ViewHolder没有抽象过. .. ViewHolder (用了一个集合+泛型管理存取view ...

  7. Android:打造“万能”Adapter与ViewHolder

    ##写在前面 最近一直忙着各种结课大作业,重新看起Android还有种亲切感.前段时间写项目的时候,学习了一个万能Adapter与ViewHolder的写法.说是"万能"其实就是在 ...

  8. Android: RecyclerView.ViewHolder、Adapter

    1.简介  用户滑动屏幕切换视图时,上一个视图会回收利用,RecyclerView所做的就是回收再利用,循环往复. ViewHolder  ViewHolder的主要任务:容纳View视图. Adap ...

  9. Android踩坑日记:RecyclerView中EditText和ImageView的ViewHolder复用坑

    RecyclerView中EditText和ImageView的ViewHolder复用坑 RecyclerView作为ListView的升级版,目前来讲讲开发过程遇到的坑. RecyclerView ...

  10. Adapter.getView convertView(重用View) ViewHolder(避免findViewById)

    convertView:可能是缓存的View(退出显示区域的View被缓存起来) ViewHolder:避免进行findViewById

最新文章

  1. 教育部:国外经历不得作为高校招聘限制性条件
  2. npm install 报权限错误,permission denied
  3. 每隔一定时间执行一次函数_python — 轮询执行某函数的方法
  4. CVPR 2021 双目图像压缩最新进展
  5. 【Vulnhub靶机系列】DC2
  6. iOS - 上架的APP 生成二维码下载
  7. matlab 轴承 压力分布 绘图,数值计算绘制动压轴承压力场分布图
  8. IntelliJ IDEA 2020.x 入门到爱不释手
  9. 程序员自救指南:一不小心删库删表怎么办?
  10. startssl申请免费ssl证书
  11. 小Q系列故事——大笨钟
  12. 苹果6邮件服务器错了如何修改,苹果手机ID电子邮件设错了怎么改
  13. 关于数据库账号和密码加密问题
  14. Windows 10上的LockApp.exe是什么?
  15. //数据结构:先序、中序、后序遍历二叉树。输入数据:abd##eg###c#f#h##
  16. 迅为6818开发板资料-CAN总线和RFID驱动的配置方法
  17. 《调色师手册:电影和视频调色专业技法(第2版)》——数字样片:后期制作的开始...
  18. HTTP、HTTPS
  19. 路由器重温——WAN接入/互联-DCC配置管理1
  20. buuctf-pwn write-ups (6)

热门文章

  1. 无线通信设备安装工程概预算编制_祁东设备安装工程施工承包-设计安装_天霖工程...
  2. 雨林木风SP3YN9.9 装机版09年09月更新(终结版)
  3. CS131-Lecture1 课程介绍
  4. 前置加加与后置加加的区别
  5. flash cs6 快捷键
  6. Macromedia Flash 8 Video Encoder安装
  7. html5 左侧在线客服,在网页右侧漂浮的QQ在线客服代码,QQ在线状态代码(四种样式)...
  8. B2C商城系统优势开发源码
  9. GhostScript命令参数详解
  10. BIOS报警声_文伟_新浪博客