我有一个自定义列表视图,其中包含更多子视图。当我滚动列表视图时,它滚动非常慢,导致避免使用该应用程序。我是android的新手,请帮我如何在后台线程中加载数据,以便listview滚动顺畅
SearchListActivity.java
public class SearchListActivity extends ListActivity{
String from[];
static String longClickValue;
int to[];
static String name="",birthdate="",westerZodiac="",turnsAge="",nextBD="",eventType="";
static Bitmap bitMap=null;
//CustomCursorAdapter adapter;
CustomSimpleCursor adapter;
TextView emptyView;
Cursor cursor=null;
CursorLoader cursorLoader=null;
ImageView imageV;
@SuppressLint("NewApi") @SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_list_activity);
cursorLoader=new CursorLoader(this,BirthdayProvider.CONTENT_URI,null, null,null,null);
cursor=cursorLoader.loadInBackground();
from=new String[]{BirthdayProvider.NAME,BirthdayProvider.NUMBER,BirthdayProvider.CATEGORY,BirthdayProvider.EVENT_TYPE,BirthdayProvider.EVENT_DAY,BirthdayProvider.WESTERN_ZODIAC};
to=new int[]{R.id.title_name,R.id.sub_title_contact_number,R.id.sub_title_category,R.id.txtview_type_of_reminder,R.id.sub_title_birthdate,R.id.txtview_zodiac};
if (cursor.moveToFirst()) {
adapter=new CustomSimpleCursor(this, R.layout.list_row, cursor, from, to);
getListView().setAdapter(adapter);
}
else
{
emptyView=(TextView)getListView().findViewById(android.R.id.empty);
}
getListView().setLongClickable(true);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,int index, long arg3) {
TextView txtName=(TextView)v.findViewById(R.id.title_name);
name=txtName.getText().toString();
imageV=(ImageView)v.findViewById(R.id.ImageView02);
//imageV.setImageDrawable(getResources().getDrawable(R.drawable.rings_icon));
ImageView imgView=(ImageView)v.findViewById(R.id.list_image);
bitMap=((BitmapDrawable)imgView.getDrawable()).getBitmap();
TextView label=(TextView)v.findViewById(R.id.sub_title_contact_number);
longClickValue=label.getText().toString();
Intent intent=new Intent(getBaseContext(),ContactInfoMoreOption.class);
intent.putExtra("intentType","SearchListActivity");
intent.putExtra("longClickValue",longClickValue);
intent.putExtra("imgObj",imageV.toString());
startActivity(intent);
return true;
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
TextView txtName=(TextView)v.findViewById(R.id.title_name);
name=txtName.getText().toString();
ImageView imgView=(ImageView)v.findViewById(R.id.list_image);
bitMap=((BitmapDrawable)imgView.getDrawable()).getBitmap();
TextView txtBirthdate=(TextView)v.findViewById(R.id.sub_title_birthdate);
birthdate=txtBirthdate.getText().toString();
TextView txtTurnsAge=(TextView)v.findViewById(R.id.txtview_turns);
turnsAge=txtTurnsAge.getText().toString();
TextView txtWesterZodiac=(TextView)v.findViewById(R.id.txtview_zodiac);
westerZodiac=txtWesterZodiac.getText().toString();
TextView txtNextBD=(TextView)v.findViewById(R.id.txtview_next_bd);
nextBD=txtNextBD.getText().toString();
TextView txtEventType=(TextView)v.findViewById(R.id.txtview_type_of_reminder);
eventType=txtEventType.getText().toString();
Intent intent=new Intent(getBaseContext(),ContactInfoActivity.class);
startActivity(intent);
}
}
CustomSimpleCursor.java
public class CustomSimpleCursor extends SimpleCursorAdapter{
Typeface font;
TextView txtDaysRemainigValue,txtTurnsAge;
@SuppressWarnings("deprecation")
public CustomSimpleCursor(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
String todayBD = null;
//Zodiac icon
try {
ImageView thumbZodiacIcon = (ImageView) view.findViewById(R.id.ImageView01);
InputStream is = context.getResources().getAssets().open(cursor.getString(cursor.getColumnIndexOrThrow(BirthdayProvider.ZODIAC_ICON)));
Bitmap bit = /*BirthdayCalculation.ShrinkBitmap(is,200, 200);*/BitmapFactory.decodeStream(is);
thumbZodiacIcon.setImageBitmap(bit);
} catch (Exception e1) {
e1.printStackTrace();
}
// Event icon
try {
ImageView thumbEventIcon = (ImageView) view.findViewById(R.id.ImageView02);
InputStream is = context.getResources().getAssets().open(cursor.getString(cursor.getColumnIndexOrThrow(BirthdayProvider.EVENT_ICON)));
Bitmap bit =BitmapFactory.decodeStream(is);
thumbEventIcon.setImageBitmap(bit);
} catch (Exception e1) {
e1.printStackTrace();
}
//Contact photo
try {
ImageView thumbContact = (ImageView) view.findViewById(R.id.list_image);
byte bitmapData[]=cursor.getBlob(cursor.getColumnIndex(BirthdayProvider.PHOTO));
Bitmap bit = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
thumbContact.setImageBitmap(bit);
/*Animation localAnimation = AnimationUtils.loadAnimation(context,R.anim.fadein_image);
thumbContact.startAnimation(localAnimation);*/
} catch (Exception e1) {
e1.printStackTrace();
}
//Days remaining for BirthDay
txtDaysRemainigValue=(TextView)view.findViewById(R.id.txtview_days_left);
String year=cursor.getString(cursor.getColumnIndex(BirthdayProvider.EVENT_YEAR));
String month=cursor.getString(cursor.getColumnIndex(BirthdayProvider.EVENT_MONTH));
String date=cursor.getString(cursor.getColumnIndex(BirthdayProvider.EVENT_DATE));
String remainingDays=BirthdayCalculation.getDaysRemainingForNextBirthDay(year, month, date);
Calendar today=Calendar.getInstance();
int CMonth=(today.get(Calendar.MONDAY)+1);
int CDate=(today.get(Calendar.DAY_OF_MONTH));
txtDaysRemainigValue.setTextColor(Color.parseColor("#990000"));
txtDaysRemainigValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 27);
//checking whether the BD is on TODAY
if (remainingDays.equals("1") && (CDate==Integer.parseInt(date) && (CMonth)==Integer.parseInt(month))) {
Log.v("else if executed","else if executed");
Log.v("Remainin Days",remainingDays+" = Custom month"+cursor.getString(cursor.getColumnIndex(BirthdayProvider.NAME))+"="+(CMonth)+"="+month);
Log.v("Remainin Days",remainingDays+" = Custom date"+ cursor.getString(cursor.getColumnIndex(BirthdayProvider.NAME))+"="+CDate+"="+date);
txtDaysRemainigValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
txtDaysRemainigValue.setTextColor(Color.parseColor("#00CC33"));
remainingDays="today".toLowerCase();
}
//checking whether the BD is on TOMORROW
else if (remainingDays.equals("1")) {
Log.v("Remainin Days",remainingDays+" = Custom month"+cursor.getString(cursor.getColumnIndex(BirthdayProvider.NAME))+"="+(CMonth)+"="+month);
Log.v("Remainin Days",remainingDays+" = Custom date"+ cursor.getString(cursor.getColumnIndex(BirthdayProvider.NAME))+"="+CDate+"="+date);
txtDaysRemainigValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
txtDaysRemainigValue.setTextColor(Color.parseColor("#FFFF00"));
remainingDays="tomorrow".toLowerCase();
}
//checking how many days remaining BD
else{
remainingDays=BirthdayCalculation.getDaysRemainingForNextBirthDay(year, month, date);
txtDaysRemainigValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 27);
txtDaysRemainigValue.setTextColor(Color.parseColor("#990000"));
}
txtDaysRemainigValue=(TextView)view.findViewById(R.id.txtview_days_left);
font=Typeface.createFromAsset(context.getAssets(), "DS_DIGIB.ttf");
txtDaysRemainigValue.setTypeface(font);
//Log.v("Custom month",cursor.getString(cursor.getColumnIndex(BirthdayProvider.NAME))+"="+(CMonth)+"="+month);
//Log.v("Custom date", cursor.getString(cursor.getColumnIndex(BirthdayProvider.NAME))+"="+CDate+"="+date);
txtDaysRemainigValue.setText(remainingDays);
//Turns age
font=Typeface.createFromAsset(context.getAssets(), "athletic.ttf");
txtTurnsAge=(TextView)view.findViewById(R.id.txtview_turns);
//txtTurnsAge.setTypeface(font);
String turnsAge=BirthdayCalculation.getAge(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(date));
txtTurnsAge.setText(turnsAge);
//Event week
try {
TextView txtEventWeek=(TextView)view.findViewById(R.id.txtview_next_bd);
String eventWeek=BirthdayCalculation.getDayOfWeek(year, month, date);
font=Typeface.createFromAsset(context.getAssets(), "Comic Sans.ttf");
txtEventWeek.setTypeface(font);
txtEventWeek.setText(eventWeek);
} catch (Exception e) {
e.printStackTrace();
}
// Contact Name
TextView txtNumber=(TextView)view.findViewById(R.id.sub_title_contact_number);
font=Typeface.createFromAsset(context.getAssets(), "Roboto_Light.ttf");
txtNumber.setTypeface(font);
TextView txtBirtgDay=(TextView)view.findViewById(R.id.sub_title_birthdate);
font=Typeface.createFromAsset(context.getAssets(), "Roboto_Light.ttf");
txtBirtgDay.setTypeface(font);
TextView txtZodiac=(TextView)view.findViewById(R.id.TextView02);
font=Typeface.createFromAsset(context.getAssets(), "Roboto_Light.ttf");
txtZodiac.setTypeface(font);
TextView txtZodiacValue=(TextView)view.findViewById(R.id.txtview_zodiac);
font=Typeface.createFromAsset(context.getAssets(), "Comic Sans.ttf");
txtZodiacValue.setTypeface(font);
TextView txtNextBD=(TextView)view.findViewById(R.id.TextView01);
font=Typeface.createFromAsset(context.getAssets(), "Roboto_Light.ttf");
txtNextBD.setTypeface(font);
TextView txtTurnsAge=(TextView)view.findViewById(R.id.list_image_arrow);
font=Typeface.createFromAsset(context.getAssets(), "athletic.ttf");
txtTurnsAge.setTypeface(font);
TextView txtDayRemainig=(TextView)view.findViewById(R.id.TextView04);
font=Typeface.createFromAsset(context.getAssets(), "athletic.ttf");
txtDayRemainig.setTypeface(font);
TextView txtTurnsAgeValue=(TextView)view.findViewById(R.id.txtview_turns);
font=Typeface.createFromAsset(context.getAssets(), "DS_DIGIB.ttf");
txtTurnsAgeValue.setTypeface(font);
}
}
答案 0 :(得分:1)
要提高列表视图的速度,您必须完成以下步骤:
使用ViewHolder模式,因为view.findViewById操作缓慢。
您之前可以加载所有需要的字体。你每次都加载相同的字体。例如Typeface.createFromAsset(context.getAssets(), "Roboto_Light.ttf")
您之前可以加载所有生肖图标,因为我认为生肖图标有限制数
如果照片是大图像,则可以从其他线程加载图像。
我认为这已经足够了。
答案 1 :(得分:0)
根据你的代码试试这个
public void bindView(View view, Context context, Cursor cursor) {
switch (view.getId) {
case R.id.ImageView01:
// your code here
break;
case R.id.ImageView02:
// your code here
break;
}
}