gpt4 book ai didi

android - 在 android ListView 中滚动时滞后

转载 作者:行者123 更新时间:2023-11-29 00:23:36 24 4
gpt4 key购买 nike

我有一个 ListView ,我必须在其中加载带有文本和标准图像的不同对象;但是在我提供给 ListView 的数组列表中,有些对象必须使用不同的“row.xml”加载。不知道有没有解释清楚,大概代码会说明一切:

这是我的适配器

    /**
*
*/


import java.util.List;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;



import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class EventAdapter extends ArrayAdapter<EventEntry> {

Typeface face;
private static final Integer LIST_HEADER = 0;
private static final Integer LIST_ITEM = 2;

public EventAdapter(Context context, int textViewResourceId, List<EventEntry> objects) {
super(context, textViewResourceId, objects);
face=Typeface.createFromAsset(context.getAssets(),"fonts/arial.ttf");

}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getViewOptimize(position, convertView, parent);
}

public View getViewOptimize(int position, View convertView, ViewGroup parent) {

//piece of code for header items
String headerText = getHeader(position);
if(headerText != null) {

View item = convertView;
if(convertView == null || convertView.getTag() != LIST_HEADER) {

item = LayoutInflater.from(getContext()).inflate(
R.layout.event_list_header, parent, false);
item.setTag(LIST_HEADER);

}

TextView headerTextView = (TextView)item.findViewById(R.id.lv_list_hdr);
headerTextView.setText(headerText);
return item;
}



ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_event, null);
viewHolder = new ViewHolder();
viewHolder.first = (TextView)convertView.findViewById(R.id.ev_tit_list);
viewHolder.second = (ImageView)convertView.findViewById(R.id.ev_image_list);
convertView.setTag(viewHolder);
} else {
try{
viewHolder = (ViewHolder) convertView.getTag();
}catch(ClassCastException e){
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_event, null);
viewHolder = new ViewHolder();
viewHolder.first = (TextView)convertView.findViewById(R.id.ev_tit_list);
viewHolder.second = (ImageView)convertView.findViewById(R.id.ev_image_list);
convertView.setTag(viewHolder);
}
}
EventEntry e = getItem(position);
if (e.getEvento()!=null)
return setEventItem(viewHolder, e.getEvento(), convertView);
else
return setSagraItem(viewHolder, e.getSagra(), convertView);


}

public View setEventItem(ViewHolder viewHolder, Entry2ev e, View convertView){

viewHolder.first.setTypeface(face, Typeface.BOLD );
viewHolder.first.setText(e.getTit());
viewHolder.second.setImageResource(e.getIm());

SVG svg_event = SVGParser.getSVGFromResource(getContext().getResources(), R.raw.eventi_principale);
viewHolder.second.setImageDrawable(svg_event.createPictureDrawable());
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {

viewHolder.second.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

}
return convertView;
}

public View setSagraItem(ViewHolder viewHolder, EntrySagreEvent e, View convertView){

viewHolder.first.setText(e.getName());
viewHolder.first.setTypeface(face, Typeface.BOLD );

viewHolder.second.setImageResource(R.drawable.shop);

return convertView;
}

private String getHeader(int position) {

if(getItem(position).getMonth()!=null) {
return getItem(position).getMonth();
}
else
return null;
}


private class ViewHolder {
public TextView first;
public ImageView second;
}
}

我该怎么做才能获得无延迟的卷轴?非常感谢

最佳答案

您想引用 Google 的这篇文章:http://developer.android.com/training/improving-layouts/smooth-scrolling.html

特别是,您应该使用 ViewHolder,这样项目只创建一次,然后重新使用。您会看到性能大大提高。

关于android - 在 android ListView 中滚动时滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21436411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com