androidlistView性能优化

package com.example.ex_000_example;

专注于为中小企业提供成都做网站、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业喀喇沁免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

import java.util.ArrayList;

import android.os.Bundle;

import android.app.Activity;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.View;

import android.view.ViewGroup;

import android.view.View.OnClickListener;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

        //准备好的图片资源

private int[] p_w_picpathList = new int[] { R.drawable.qq0, R.drawable.qq1,

R.drawable.qq2, R.drawable.qq3, R.drawable.qq4, R.drawable.qq5,

R.drawable.qq6, R.drawable.qq7, R.drawable.qq8, R.drawable.qq9,

R.drawable.qq10, R.drawable.qq11, R.drawable.qq12, R.drawable.qq13,

R.drawable.qq14 };

private ArrayList dateList = new ArrayList();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 准备数据

for (int i = 0; i < p_w_picpathList.length; i++) {

MyDate myDate = new MyDate("数据来了"+i, p_w_picpathList[i]);

dateList.add(myDate);

}

ListView listView = (ListView) findViewById(R.id.listView1);

MyAdapter myAdapter = new MyAdapter();

// 设置适配器

listView.setAdapter(myAdapter);

}

        //减少控件查找次数

class ViewHolder {

TextView textView;

ImageView p_w_picpathView;

}

class MyAdapter extends BaseAdapter {

// 设定每行的样式以及显示

@Override

public View getView(int position, View convertView, ViewGroup parent) {

View inflate = null;

ViewHolder holder = null;

if (convertView == null) {

holder = new ViewHolder();

// 得到布局转换器

LayoutInflater inflater = getLayoutInflater();

// 将布局xml文件转换成布局对象

inflate = inflater.inflate(R.layout.list_item, null);

// 查找控件,一定要查找该布局对象上有的控件

holder.textView = (TextView) inflate

.findViewById(R.id.textView1);

holder.p_w_picpathView = (ImageView) inflate

.findViewById(R.id.p_w_picpathView1);

// 将保存控件的对象存入行布局对象的tag中

inflate.setTag(holder);

} else {

// 让那个已经完全消失的布局对象,去替代即将出现的

inflate = convertView;

// 从行布局对象中取出之前存好的控件

holder = (ViewHolder) inflate.getTag();

}

// 根据position从容器中取出数据

MyDate myDate = dateList.get(position);

// 将数据显示到控件上

holder.textView.setText(myDate.title);

holder.p_w_picpathView.setImageResource(myDate.p_w_picpath);

return inflate;

}

// 决定了listview显示的行数

@Override

public int getCount() {

// TODO Auto-generated method stub

return dateList.size();

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

    class MyDate{

String title;

int p_w_picpath;

public MyDate(String title, int p_w_picpath) {

super();

this.title = title;

this.p_w_picpath = p_w_picpath;

}

}

}


网站题目:androidlistView性能优化
URL标题:http://azwzsj.com/article/pdeddc.html