我有一个listview,其中我必须使用文本和标准图像加载不同的对象;但是在我给listview的arraylist中,有一些对象必须加载不同的“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;
}
}
我能做些什么来获得无延迟的滚动?非常感谢你
答案 0 :(得分:2)
您希望Google参考此文章:http://developer.android.com/training/improving-layouts/smooth-scrolling.html
特别是,您应该使用ViewHolder,因此项目只创建一次然后重新使用。你会看到性能大大提高。
答案 1 :(得分:0)
不要在自定义适配器中初始化该Typephase,而不是将其作为前一个类的参数传递。问题解决了我这样做。