Android的 - 自定义列表视图滚动很慢 [英] Android - Custom Listview Scrolling is Very Slow
问题描述
我有一个ListView有时夸大一个XML(TextView的项目)或其它XML(TextView的+图像项)。我跟着这些指示,以获得更好的性能。
I've got a listview which sometimes inflate a XML (TextView Item) or another XML (TextView+Image Item). And i followed these instructions to get a better performance.
http://www.google。 COM /事件/ IO / 2010 /会话/世界-列表视图中,android.html
https://dl.google.com/ / Android的世界 - 列表视图 - android.pdf googleio / 2010
http://android.amberfog.com/?p=296
这是现在好,但仍无法获得最佳的性能,仍然滚动速度很慢。这是我的code:
It's now better, but still can't get best performance and still scroll is working slowly. Here's my code :
GetView:(// ViewHolder 0 - >部分项目 ViewHolder 1 - >参赛作品)
GetView : (//ViewHolder 0 -> Section Item ViewHolder 1 -> Entry Item)
@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
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:
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>
我需要的任何建议。谢谢你。
I need any suggestions. Thanks.
推荐答案
如果这个被接受,我想我需要包括这里的调试:
If this is accepted I think I need to include the debug here:
-
删除
ellipsize
和fadingedge
,并为双方的TextView设置宽度为match_parent
,如果滚动仍然缓慢,尝试下。
Remove the
ellipsize
andfadingedge
, and for both TextView set the width tomatch_parent
, if scrolling still slow, try next.
scaleType =fitXY
与 WRAP_CONTENT
上的ImageView实际上听起来挺,如果你问发生冲突我尝试专门设置的宽度和高度可能48dp或55dp,那么 fitXY
是有道理的。
scaleType="fitXY"
together with wrap_content
on ImageView actually sounds quite, conflicting if you ask me, try specifically setting the width and height to maybe 48dp or 55dp, then fitXY
makes sense.
下面的函数来适当BaseAdapter类的例子:
Here's an example of functions to a proper BaseAdapter class:
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.
}
这篇关于Android的 - 自定义列表视图滚动很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!