Android ListView GridView Gallery 适配器 性能优化

之前码控件太多,由于项目时间问题,也就很少考虑到优化的一些细节问题,今天就简单些了个关于ListView控件对在使用适配器时的优化效果:好了直接贴图:先看下对比效果:

不判断converView为空时,也就是每次拖动时都会去加载一次所花费的时间效果图:

Android ListView GridView Gallery 适配器 性能优化



判断了convertView为空时蔡重新加载的效果图,也就是使用了其缓存的数据:

Android ListView GridView Gallery 适配器 性能优化



下面图为使用了组件静态化的方式,也就是使用空间换时间的方式,在其findById()放进临时内存缓存里,得出的时间效果图

Android ListView GridView Gallery 适配器 性能优化



从以上图可以看出,其代码优化的必要性:

好了,不说废话了,我直接把其核心代码贴出来,希望能给一些热爱Android学习的人做更好的参考:

package com.geili.vancl.activity;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;


import com.geili.vancl.R;
import com.geili.vancl.util.MyLogger;


/**
 * 
 * @author jankey
 *
 */
public class CateDetailActivity extends Activity{


private static final MyLogger logger = MyLogger.getLogger("[====HomeActivity====]");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.catedetail);
findViewById();
}

private void findViewById(){
topBar();
setCateDetailDatas();
}

private Button mMen,mFemale;
private GridView mCateDetailList;
private void setCateDetailDatas(){
mMen = (Button) findViewById(R.id.men);
mFemale = (Button) findViewById(R.id.female);
mCateDetailList = (GridView) findViewById(R.id.cateDetailList);
mMen.setOnClickListener(listener);
mFemale.setOnClickListener(listener);
mCateDetailList.setAdapter(new CateDetailAdapter(CateDetailActivity.this));
}

private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.men:

break;
case R.id.female:

break;
}
}
};

/*public class CateDetailAdapter extends BaseAdapter{


private Context mContext;

public CateDetailAdapter(Context c) {
this.mContext = c;
}

@Override
public int getCount() {
return 100000;
}


@Override
public Object getItem(int position) {
return null;
}


@Override
public long getItemId(int position) {
return 0;
}


private ImageView mCatedetailImg,mStar;
private TextView mCateDetailPrice,mSalesTotal;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
long startDate = System.nanoTime();
LayoutInflater mInflate = LayoutInflater.from(mContext);
//if(convertView == null){
convertView = mInflate.inflate(R.layout.catedetail_item,null);
//}
mCatedetailImg = (ImageView) convertView.findViewById(R.id.catedetailImg);
mStar = (ImageView) convertView.findViewById(R.id.star);
mCateDetailPrice = (TextView) convertView.findViewById(R.id.cateDetailPrice);
mSalesTotal = (TextView) convertView.findViewById(R.id.salesTotal);
mCatedetailImg.setImageResource(R.drawable.splash);
mStar.setImageResource(R.drawable.speaker);
mCateDetailPrice.setText("56.00");
mSalesTotal.setText("299393");
long endDate = System.nanoTime();
long resultValue = (endDate - startDate)/1000L;
logger.v("resultValue Date: =====================> "+resultValue);
if(new CateDetailAdapter(mContext).getCount() < 100){
if(resultValue < 1000L){

}
}
return convertView;
}
}*/

/*public class CateDetailAdapter extends BaseAdapter{


private Context mContext;

public CateDetailAdapter(Context c) {
this.mContext = c;
}

@Override
public int getCount() {
return 100000;
}


@Override
public Object getItem(int position) {
return null;
}


@Override
public long getItemId(int position) {
return 0;
}


private ImageView mCatedetailImg,mStar;
private TextView mCateDetailPrice,mSalesTotal;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
long startDate = System.nanoTime();
LayoutInflater mInflate = LayoutInflater.from(mContext);
if(convertView == null){
convertView = mInflate.inflate(R.layout.catedetail_item,null);
}
mCatedetailImg = (ImageView) convertView.findViewById(R.id.catedetailImg);
mStar = (ImageView) convertView.findViewById(R.id.star);
mCateDetailPrice = (TextView) convertView.findViewById(R.id.cateDetailPrice);
mSalesTotal = (TextView) convertView.findViewById(R.id.salesTotal);
mCatedetailImg.setImageResource(R.drawable.splash);
mStar.setImageResource(R.drawable.speaker);
mCateDetailPrice.setText("56.00");
mSalesTotal.setText("299393");
long endDate = System.nanoTime();
long resultValue = (endDate - startDate)/1000L;
logger.v("resultValue Date: =====================> "+resultValue);
if(new CateDetailAdapter(mContext).getCount() < 100){
if(resultValue < 1000L){

}
}
return convertView;
}
}*/

public class CateDetailAdapter extends BaseAdapter{


private Context mContext;

public CateDetailAdapter(Context c) {
this.mContext = c;
}

@Override
public int getCount() {
return 100000;
}


@Override
public Object getItem(int position) {
return null;
}


@Override
public long getItemId(int position) {
return 0;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
long startDate = System.nanoTime();
ViewHolder mViewHolder; 
LayoutInflater mInflate = LayoutInflater.from(mContext);
if(convertView == null){
convertView = mInflate.inflate(R.layout.catedetail_item,null);
mViewHolder = new ViewHolder();
mViewHolder.mCatedetailImg = (ImageView) convertView.findViewById(R.id.catedetailImg);
mViewHolder.mStar = (ImageView) convertView.findViewById(R.id.star);
mViewHolder.mCateDetailPrice = (TextView) convertView.findViewById(R.id.cateDetailPrice);
mViewHolder.mSalesTotal = (TextView) convertView.findViewById(R.id.salesTotal);
convertView.setTag(mViewHolder);
}else{
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mCatedetailImg.setImageResource(R.drawable.splash);
mViewHolder.mStar.setImageResource(R.drawable.speaker);
mViewHolder.mCateDetailPrice.setText("56.00");
mViewHolder.mSalesTotal.setText("299393");
long endDate = System.nanoTime();
long resultValue = (endDate - startDate)/1000L;
logger.v("resultValue Date: =====================> "+resultValue);
/*if(new CateDetailAdapter(mContext).getCount() < 100){
if(resultValue < 1000L){

}
}*/
return convertView;
}
}

static class ViewHolder{
ImageView mCatedetailImg,mStar;
TextView mCateDetailPrice,mSalesTotal;
}

private Button mSearch;
private TextView mProduceName;
private void topBar(){
mSearch = (Button) findViewById(R.id.search);
mProduceName = (TextView) findViewById(R.id.produceName);
mProduceName.setText(getTopTitle());
mSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}

private String getTopTitle(){
String str = null;
if(str == null){
str = "T懈";
}
return str;
}
}

linux

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyszjg.html