android viewHolder

MainActivity.java

public class MainActivity extends Activity {
    List list = null;
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 1) {
                ListView lv = (ListView) findViewById(R.id.lv);
                lv.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return list.size();
                    }
                    @Override
                    public View getView(int position, View convertView,
                            ViewGroup parent) {
                        View v = null;
                        News n = list.get(position);
                        ViewHolder mHolder;
                        if (convertView == null) {
                            v = View.inflate(MainActivity.this,
                                    R.layout.item_listview, null);
                            mHolder = new ViewHolder();
                            // 把布局组件的封装至ViewHolder对象中
                            mHolder.titleTv = (TextView) v
                                    .findViewById(R.id.tv);
                            mHolder.detailTv = (TextView) v
                                    .findViewById(R.id.tv_content);
                            mHolder.commentTv = (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.titleTv.setText(n.getTitle());
                        mHolder.detailTv.setText(n.getDetail());
                        mHolder.commentTv.setText(n.getComment() + "条评论");
                        // 下载图片
                        mHolder.siv.setImageUrl(n.getImageUrl());
                        return v;
                    }
                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }
                    @Override
                    public Object getItem(int position) {
                        return null;
                    }
                });
            }
        };
    };
    class ViewHolder {
        // 条目的布局文件中有什么组件,这里就定义什么属性
        TextView titleTv;
        TextView detailTv;
        TextView commentTv;
        SmartImageView siv;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getNewsInfo();
    }
    private void getNewsInfo() {
        final String path = "http://qxu1192580117.my3w.com/news.xml";
        Thread thread = new Thread() {
            public void run() {
                URL url;
                try {
                    url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setReadTimeout(5000);
                    conn.setConnectTimeout(5000);
                    conn.setRequestMethod("GET");
                    if (conn.getResponseCode() == 200) {
                        InputStream is = conn.getInputStream();
                        // 使用pull解析器,解析输入流
                        parseNewsXml(is);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }
    private void parseNewsXml(InputStream is) throws XmlPullParserException,
            IOException {
        News news = null;
        XmlPullParser xpp = Xml.newPullParser();
        xpp.setInput(is, "utf-8");
        // 根据节点的事件类型进行判断,就可以知道当前节点
        int type = xpp.getEventType();
        while (type != XmlPullParser.END_DOCUMENT) {
            switch (type) {
            case XmlPullParser.START_TAG:
                String tagName = xpp.getName();
                if ("newslist".equals(tagName)) {
                    list = new ArrayList();
                } else if ("news".equals(tagName)) {
                    news = new News();
                } else if ("title".equals(tagName)) {
                    news.setTitle(xpp.nextText());
                } else if ("detail".equals(tagName)) {
                    news.setDetail(xpp.nextText());
                } else if ("comment".equals(tagName)) {
                    news.setComment(xpp.nextText());
                } else if ("image".equals(tagName)) {
                    news.setImageUrl(xpp.nextText());
                }
                break;
            case XmlPullParser.END_TAG:
                String tagName2 = xpp.getName();
                if ("news".equals(tagName2)) {
                    list.add(news);
                }
                break;
            default:
                break;
            }
            // 解析完当前节点,就把指针移动到下一个节点
            type = xpp.next();
        }
        handler.sendEmptyMessage(1);
    }
}  

ViewHolder性能优化

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 1) {
                ListView lv = (ListView) findViewById(R.id.lv);
                lv.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return list.size();
                    }
                    @Override
                    public View getView(int position, View convertView,
                            ViewGroup parent) {
                        View v = null;
                        News n = list.get(position);
                        ViewHolder mHolder;
                        if (convertView == null) {
                            v = View.inflate(MainActivity.this,
                                    R.layout.item_listview, null);
                            mHolder = new ViewHolder();
                            // 把布局组件的封装至ViewHolder对象中
                            mHolder.titleTv = (TextView) v
                                    .findViewById(R.id.tv);
                            mHolder.detailTv = (TextView) v
                                    .findViewById(R.id.tv_content);
                            mHolder.commentTv = (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.titleTv.setText(n.getTitle());
                        mHolder.detailTv.setText(n.getDetail());
                        mHolder.commentTv.setText(n.getComment() + "条评论");
                        // 下载图片
                        mHolder.siv.setImageUrl(n.getImageUrl());
                        return v;
                    }
                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }
                    @Override
                    public Object getItem(int position) {
                        return null;
                    }
                });
            }
        };
    };
    class ViewHolder {
        // 条目的布局文件中有什么组件,这里就定义什么属性
        TextView titleTv;
        TextView detailTv;
        TextView commentTv;
        SmartImageView siv;
    }

你可能感兴趣的:(android viewHolder)