之前碼控件太多,由於項目時間問題,也就很少考慮到優化的一些細節問題,今天就簡單些了個關於ListView控件對在使用適配器時的優化效果:好了直接貼圖:先看下對比效果:
不判斷converView為空時,也就是每次拖動時都會去加載一次所花費的時間效果圖:
判斷了convertView為空時蔡重新加載的效果圖,也就是使用了其緩存的數據:
下面圖為使用了組件靜態化的方式,也就是使用空間換時間的方式,在其findById()放進臨時內存緩存裡,得出的時間效果圖
從以上圖可以看出,其代碼優化的必要性:
好了,不說廢話了,我直接把其核心代碼貼出來,希望能給一些熱愛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;
}
}