我用 ArrayAdapter 创建 listView。每个项目都有很多视图。这是 getView() 方法的代码:
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.dialog_list_view, null);
holder = new ViewHolder();
holder.date = (TextView)convertView.findViewById(R.id.date);
holder.multi_chat = (ImageView)convertView.findViewById(R.id.multi_chat);
holder.my_userpick = (ImageView)convertView.findViewById(R.id.my_userpic);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.photo1 = (ImageView)convertView.findViewById(R.id.photo1);
holder.photo2 = (ImageView)convertView.findViewById(R.id.photo2);
holder.photo31 = (ImageView)convertView.findViewById(R.id.photo31);
holder.photo32 = (ImageView)convertView.findViewById(R.id.photo32);
holder.photo33 = (ImageView)convertView.findViewById(R.id.photo33);
holder.photo41 = (ImageView)convertView.findViewById(R.id.photo41);
holder.photo42 = (ImageView)convertView.findViewById(R.id.photo42);
holder.photo43 = (ImageView)convertView.findViewById(R.id.photo43);
holder.photo44 = (ImageView)convertView.findViewById(R.id.photo44);
holder.text = (TextView)convertView.findViewById(R.id.text);
holder.userpic = (ImageView)convertView.findViewById(R.id.userpic);
holder.userpick2 = convertView.findViewById(R.id.userpick2);
holder.userpick3 = convertView.findViewById(R.id.userpick3);
holder.userpick4 = convertView.findViewById(R.id.userpick4);
holder.wrapper = (RelativeLayout)convertView.findViewById(R.id.wrapper);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (dialogs != null) {
if (position == (this.getCount() - 1)) {
if (dialogs != null) {
if (dates.size() > 1) {
dialogs.getDialogs(dates.size()+20);
}
}
}
}
if (dates.get(position).getChatId() != 0) {
holder.name.setText(dates.get(position).getTitle());
holder.multi_chat.setVisibility(View.VISIBLE);
} else {
holder.name.setText(dates.get(position).getUidDates());
holder.multi_chat.setVisibility(View.GONE);
}
if (dates.get(position).getReadState() == 0) {
holder.wrapper.setBackgroundColor(getResources().getColor(R.color.conversation_list_no_read));
} else {
holder.wrapper.setBackgroundColor(android.R.color.white);
}
holder.text.setText(dates.get(position).getBody());
int date = dates.get(position).getDate();
java.util.Date time = new java.util.Date((long) date * 1000);
if ((time.getDate() == current_date.getDate()) && (time.getMonth() == current_date.getMonth()) && (time.getYear() == current_date.getYear())) {
holder.date.setText(time.getHours() + ":" + time.getMinutes());
} else if ((time.getDate() == (current_date.getDate() - 1)) && (time.getMonth() == current_date.getMonth()) && (time.getYear() == current_date.getYear())) {
holder.date.setText("вчера");
} else {
holder.date.setText(time.getDate() + "." + time.getMonth() + "." + (time.getYear()+1900));
}
if(!dates.get(position).getPhotoInText().equals("")) {
loaderWithoutCut.displayImage(dates.get(position).getPhotoInText(), holder.my_userpick, false);
holder.my_userpick.setVisibility(View.VISIBLE);
holder.text.setBackgroundDrawable(getResources().getDrawable(R.drawable.dialog_rect_bg));
holder.text.setPadding(10, 0, 10, 0);
holder.text.setGravity(Gravity.CENTER_VERTICAL);
holder.text.setSingleLine(true);
holder.text.setEllipsize(TextUtils.TruncateAt.END);
} else {
holder.my_userpick.setVisibility(View.GONE);
holder.text.setBackgroundColor(getResources().getColor(android.R.color.transparent));
holder.text.setPadding(0, 0, 0, 0);
holder.text.setSingleLine(false);
holder.text.setGravity(Gravity.TOP);
}
ArrayList photos = dates.get(position).getUidPhotos();
if (dates.get(position).getChatId() == 0) {
if (photos.size() == 1) {
loaderWithoutCut.displayImage((String) photos.get(0), holder.userpic, false);
holder.userpick2.setVisibility(View.GONE);
holder.userpick3.setVisibility(View.GONE);
holder.userpick4.setVisibility(View.GONE);
holder.userpic.setVisibility(View.VISIBLE);
}
} else {
if ((photos.size() < 3) && (photos.size()>0)) {
if (photos.size() == 1) {
holder.photo1.setVisibility(View.GONE);
loaderWithCut.displayImage((String) photos.get(0), holder.photo2, true);
} else {
holder.photo1.setVisibility(View.VISIBLE);
loaderWithCut.displayImage((String) photos.get(1), holder.photo2, true);
if (!((String) photos.get(1)).equals((String) photos.get(0))) {
loaderWithCut.displayImage((String) photos.get(0), holder.photo1, true);
} else {
holder.photo1.setVisibility(View.GONE);
}
}
holder.userpic.setVisibility(View.GONE);
holder.userpick2.setVisibility(View.VISIBLE);
holder.userpick3.setVisibility(View.GONE);
holder.userpick4.setVisibility(View.GONE);
}
if (photos.size() == 3) {
loaderWithCut.displayImage((String) photos.get(0), holder.photo31, true);
loaderWithoutCut.displayImage((String) photos.get(1), holder.photo32, false);
loaderWithoutCut.displayImage((String) photos.get(2), (ImageView) holder.photo33, false);
holder.userpic.setVisibility(View.GONE);
holder.userpick2.setVisibility(View.GONE);
holder.userpick3.setVisibility(View.VISIBLE);
holder.userpick4.setVisibility(View.GONE);
}
if (photos.size() >= 4) {
loaderWithoutCut.displayImage((String) photos.get(0), holder.photo41, false);
loaderWithoutCut.displayImage((String) photos.get(1), holder.photo42, false);
loaderWithoutCut.displayImage((String) photos.get(2), holder.photo43, false);
loaderWithoutCut.displayImage((String) photos.get(3), holder.photo44, false);
holder.userpic.setVisibility(View.GONE);
holder.userpick2.setVisibility(View.GONE);
holder.userpick3.setVisibility(View.GONE);
holder.userpick4.setVisibility(View.VISIBLE);
}
}
if (photos.size() == 0) {
holder.userpick2.setVisibility(View.GONE);
holder.userpick3.setVisibility(View.GONE);
holder.userpick4.setVisibility(View.GONE);
}
final ViewHolder holder2 = holder;
convertView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
TextView text = holder2.text;
TextView date = holder2.date;
TextView name = holder2.name;
if (hasFocus) {
text.setTextColor(getResources().getColor(android.R.color.white));
date.setTextColor(getResources().getColor(android.R.color.white));
name.setTextColor(getResources().getColor(android.R.color.white));
} else {
text.setTextColor(getResources().getColor(R.color.dialog_text));
date.setTextColor(getResources().getColor(R.color.dialog_date));
name.setTextColor(getResources().getColor(R.color.friends_text_color));
}
}
});
return convertView;
}
问题是 - 列表视图滚动工作非常缓慢。我该如何解决这个问题?布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" android:background="@drawable/list_drawable_settings">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical" android:id="@+id/wrapper">
<LinearLayout android:layout_width="52dip" android:id="@+id/images" android:layout_margin="6dip"
android:layout_height="58dip">
<com.vk.messenger.views.RoundedConersImageView android:id="@+id/userpic"
android:layout_height="wrap_content" android:layout_width="wrap_content"
/>
<LinearLayout android:layout_width="wrap_content" android:id="@+id/userpick2"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="26dip"
android:layout_height="58dip">
<com.vk.messenger.views.ScalledImageView android:scaleType="fitCenter"
android:cropToPadding="true" android:layout_width="26dip"
android:layout_height="58dip"
android:id="@+id/photo1"/>
</LinearLayout>
<LinearLayout android:layout_width="26dip"
android:layout_height="58dip">
<com.vk.messenger.views.ScalledImageView android:layout_width="26dip"
android:layout_height="58dip" android:scaleType="fitCenter"
android:cropToPadding="true"
android:id="@+id/photo2"/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:id="@+id/userpick3"
android:layout_height="wrap_content">
<ImageView android:layout_width="26dip" android:layout_height="58dip" android:id="@+id/photo31"
></ImageView>
<LinearLayout android:layout_width="26dip" android:id="@+id/userpick3"
android:layout_height="58dip"
android:orientation="vertical">
<ImageView android:layout_width="26dip"
android:scaleType="fitCenter" android:cropToPadding="true"
android:layout_height="26dip" android:id="@+id/photo32"
></ImageView>
<ImageView android:layout_width="26dip"
android:scaleType="fitCenter" android:cropToPadding="true"
android:layout_height="29dip" android:id="@+id/photo33"
></ImageView>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="52dip" android:id="@+id/userpick4"
android:layout_height="58dip" android:orientation="horizontal">
<LinearLayout android:layout_width="26dip"
android:layout_height="58dip"
android:orientation="vertical">
<ImageView android:layout_width="26dip"
android:layout_height="29dip" android:id="@+id/photo41"
></ImageView>
<ImageView android:layout_width="26dip"
android:layout_height="29dip" android:id="@+id/photo42"
></ImageView>
</LinearLayout>
<LinearLayout android:layout_width="26dip"
android:layout_height="58dip"
android:orientation="vertical">
<ImageView android:layout_width="26dip"
android:layout_height="29dip" android:id="@+id/photo43"
></ImageView>
<ImageView android:layout_width="26dip"
android:layout_height="29dip" android:id="@+id/photo44"
></ImageView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/images" android:layout_marginLeft="4dip">
<LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:id="@+id/left" android:layout_height="wrap_content">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:id="@+id/multi_chat" android:visibility="invisible" android:paddingTop="3dip" android:paddingRight="2dip"
android:src="@drawable/multi_chat"
android:layout_height="wrap_content" android:layout_width="wrap_content"
/>
<com.vk.messenger.views.TextViewMyriadPro android:id="@+id/name" android:text="" android:singleLine="true" android:ellipsize="end"
android:layout_centerVertical="true"
android:textColor="@color/friends_text_color" android:textSize="16dip" android:textStyle="bold"
android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"/>
<com.vk.messenger.views.TextViewHelvetica android:id="@+id/date" android:layout_marginLeft="6dip" android:gravity="right" android:layout_marginRight="6dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/dialog_date"
android:textSize="13dip"/>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_weight="1" android:layout_marginTop="2dip">
<com.vk.messenger.views.RoundedConersImageView android:id="@+id/my_userpic" android:visibility="gone" android:paddingRight="2dip"
android:layout_height="30dip" android:layout_width="30dip"/>
<com.vk.messenger.views.TextViewHelvetica android:id="@+id/text" android:text="текст" android:layout_below="@id/name"
android:layout_centerVertical="true" android:layout_marginLeft="3dip" android:layout_marginRight="6dip"
android:textColor="@color/dialog_text" android:textSize="13dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>