我有一个listview,它有时会膨胀XML(TextView项)或另一个XML(TextView + Image Item)。我遵循这些说明以获得更好的性能。
http://www.google.com/events/io/2010/sessions/world-of-listview-android.html
https://dl.google.com/googleio/2010/android-world-of-listview-android.pdf
http://android.amberfog.com/?p=296
它现在更好了,但仍然无法获得最佳性能,但仍然滚动工作缓慢。这是我的代码:
GetView:(// ViewHolder 0 - > Section Item ViewHolder 1 - >条目项目)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder[] viewHolders=new ViewHolder[2];
final ListItems.Item i = items.get(position);
if (i != null) {
if(i.isSection()){
TextView sectionView;
ListItems.SectionItem si = (ListItems.SectionItem)i;
if (v==null || ((ViewHolder[])v.getTag())==null || ((ViewHolder[])v.getTag())[0]==null)
{
v = vi.inflate(R.layout.order_list_item_section, null);
sectionView= (TextView) v.findViewById(R.id.list_item_section_text);
viewHolders[0]=new ViewHolder();
viewHolders[0].Title=sectionView;
v.setTag(viewHolders);
}
else
{
viewHolders[0]=((ViewHolder[]) v.getTag())[0];
sectionView=viewHolders[0].Title;
}
sectionView.setBackgroundColor(android.graphics.Color.BLACK);
sectionView.setText(si.getTitle());
}
else if(i.isEntryItem())
{
ListItems.EntryItem ei = (ListItems.EntryItem)i;
TextView title = null,subtitle = null;
ImageView img = null;
if (v==null || ((ViewHolder[])v.getTag())==null || ((ViewHolder[])v.getTag())[1]==null)
{
v = vi.inflate(R.layout.order_list_item_entry, null);
title = (TextView)v.findViewById(R.id.list_item_entry_title);
img=(ImageView)v.findViewById(R.id.list_item_entry_drawable);
subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);
viewHolders[1]=new ViewHolder();
viewHolders[1].Title=title;
viewHolders[1].SubTitle=subtitle;
viewHolders[1].TableImage=img;
v.setTag(viewHolders);
}
else
{
viewHolders[1]=((ViewHolder[]) v.getTag())[1];
title=viewHolders[1].Title;
subtitle=viewHolders[1].SubTitle;
img=viewHolders[1].TableImage;
}
title.setTextSize(20);
title.setTextColor(android.graphics.Color.BLACK);
title.setText(Html.fromHtml("<b><i>"+ei.title+"</b></i>"));
img.setImageBitmap(ei.tableImage);
img.setScaleType(ScaleType.FIT_XY);
img.setAdjustViewBounds(true);
if(!ei.subtitle.equals(""))
{
subtitle.setTextSize(15);
subtitle.setTextColor(android.graphics.Color.GRAY);
subtitle.setText(Html.fromHtml("<i>"+ei.subtitle+"</i>"));
}
}
ViewHolder :
static class ViewHolder {
TextView Title;
TextView SubTitle;
ImageView TableImage;
}
编辑:
order_list_item_section.xml
<?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:orientation="vertical">
<include
android:id="@+id/list_item_section_text"
layout="@android:layout/preference_category" />
</LinearLayout>
order_list_item_entry.xml:
<?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:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize"
android:background="#ffffff">
<ImageView
android:id="@+id/list_item_entry_drawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"/>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginLeft="0dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView android:id="@+id/list_item_entry_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="@+id/list_item_entry_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/list_item_entry_title"
android:layout_alignLeft="@id/list_item_entry_title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:singleLine="true"
android:textColor="?android:attr/textColorSecondary" />
</RelativeLayout>
</LinearLayout>
我需要任何建议。感谢。
答案 0 :(得分:2)
如果这被接受,我想我需要在这里包含调试:
删除ellipsize
和fadingedge
,对于TextView,将宽度设置为match_parent
,如果滚动仍然很慢,请尝试下一步。
scaleType="fitXY"
和wrap_content
实际上听起来相当矛盾,如果你问我,尝试将宽度和高度设置为48dp或55dp,然后fitXY
使感。
以下是适当的BaseAdapter类的函数示例:
public static final int SECTION = 2;
public static final int ENTRY_ITEM = 1;
private List<ListItems.Item> items; // I believe you have this List declared?
public YourClassConstructor(){
//Whatever you need to accept and assign, probably the list itself.
}
public int getCount() {
return items.size();
}
public Object getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return (long) position; // Or anything that can define your item
}
@Override
public int getViewTypeCount() {
return 2; // 2 because you have Section and EntryItem
}
@Override
public int getItemViewType(int position) {
int itemViewType;
ListItems.Item i = items.get(position);
if(i.isSection()){
itemViewType = SECTION;
}else{
itemViewType = ENTRY_ITEM;
}
return itemViewType;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent {
//Here your convertView will always be the one you need, either as a section or entry_item
//Unless there is no convertView available yet.
//So your (ViewHolder[])v.getTag())[0/1] will less likely be null.
}
答案 1 :(得分:0)
如果v为null,请仅v = vi.inflate(R.layout.order_list_item_section, null);
。它会有很大的帮助。