gpt4 book ai didi

Android SectionList 和 Holder

转载 作者:行者123 更新时间:2023-11-29 21:47:07 24 4
gpt4 key购买 nike

我有带部分的列表,但是这个 ListView 很慢。

我需要任何支架,但我不知道怎么做。

当我只有一种 View 时很简单,但在这里我遇到了问题。

我创建了 holder,但是当 ListView 显示时,我看到:

"标题","标题","标题","项目","项目"

"标题","标题","标题","项目"

"标题","标题","标题","项目","项目","项目"

这是我的代码:

public class EntryAdapter extends ArrayAdapter<Item> {

private ArrayList<Item> items;
private LayoutInflater vi;
Context context;
private String nazwa;

public EntryAdapter(Context context,ArrayList<Item> items, String nazwa) {
super(context,0, items);
this.items = items;
this.context = context;
this.nazwa = nazwa;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

然后我得到 View :

      @Override
public View getView(int position, View convertView, ViewGroup parent) {

final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
convertView = vi.inflate(R.layout.list_item_section, null);

convertView.setOnClickListener(null);
convertView.setOnLongClickListener(null);
convertView.setLongClickable(false);

final TextView sectionView = (TextView) convertView.findViewById(R.id.list_item_section_text);
}else{
Obiekt ei = (Obiekt)i;
convertView = vi.inflate(R.layout.row_list, null);
final TextView title = (TextView)convertView.findViewById(R.id.row_tytul);
final TextView odleglosc = (TextView)convertView.findViewById(R.id.row_odleglosc);
final ImageView obrazek = (ImageView)convertView.findViewById(R.id.row_ikona_mala);
final LinearLayout pole = (LinearLayout)convertView.findViewById(R.id.LinearLayout1);

}
}
return convertView;
}

最佳答案

I have list with section, but this ListView is very slow.

您总是在 getView() 方法中扩充新 View 。膨胀 View 通常需要很长时间。

您想通过检查“convertView”是否为 null 来重新使用以前的 View 。这将提高性能。注意,您还必须实现 getItemViewType()getViewTypeCount(),因为您有两个不同的 View :

public class MyAdapter extends ArrayAdapter<Item> {

private List<Item> items;
private int TYPE_SECTION = 0;
private int TYPE_OBIEKT = 1;
private static LayoutInflater vi;

public MyAdapter(Context context, List<Item> _items) {
super(context, 0);
vi = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
items = _items;
}

@Override
public int getCount() {
return items.size();
}


/**
* @see android.widget.BaseAdapter#getViewTypeCount()
* Determines how many different views you have
*/
@Override
public int getViewTypeCount() {
return 2; // We have two different kinds of views
}

/**
* @see android.widget.BaseAdapter#getItemViewType(int)
*/
@Override
public int getItemViewType(int position) {
Item item = items.get(position);
if (item.isSection())
return TYPE_SECTION;

return TYPE_OBIEKT;
}

/**
* @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {

// Get the item
final Item item = items.get(position);

if (getItemViewType(position) == TYPE_SECTION) {

// Return a SectionView object
return getSectionView(position, convertView, parent, (SectionItem) item);
}

// Return an Obiekt object
return getObiektView(position, convertView, parent, (Obiekt) item);
}

/**
* SectionView specific
*
* @param position
* @param convertView
* @param parent
* @param item
* @return
*/
public View getSectionView(int position, View convertView, ViewGroup parent, SectionItem item) {

SectionHolder holder;
if(convertView == null){

holder = new SectionHolder();

// Inflate a new View
convertView = vi.inflate(R.layout.list_item_section, null);
holder.sectionView = (TextView) convertView.findViewById(R.id.list_item_section_text);

// Set the ObiektHolder as the tag, to retrieve it for re-using
convertView.setTag(holder);

} else {
holder = (SectionHolder) convertView.getTag();
}

// Holder is never null here
holder.sectionView.setText(item.getSectionTitle());

return convertView;

}

public View getObiektView(int position, View convertView, ViewGroup parent, Obiekt item) {

ObiektHolder holder;
if(convertView == null){

holder = new ObiektHolder();

// Inflate a new View
convertView = vi.inflate(R.layout.row_list, null);
holder.title = (TextView)convertView.findViewById(R.id.row_tytul);
holder.odleglosc = (TextView)convertView.findViewById(R.id.row_odleglosc);
holder.obrazek = (ImageView)convertView.findViewById(R.id.row_ikona_mala);
holder.pole = (LinearLayout)convertView.findViewById(R.id.LinearLayout1);

// Set the ObiektHolder as the tag, to retrieve it for re-using
convertView.setTag(holder);

} else {
holder = (ObiektHolder) convertView.getTag();
}

// Holder is never null here
holder.title.setText(item.getObiektTitle());

return convertView;
}

private static class SectionHolder {
TextView sectionView;
}

private static class ObiektHolder {
TextView title;
TextView odleglosc;
ImageView obrazek;
LinearLayout pole;
}
}

注意:已经过测试,现在可以使用了!

关于Android SectionList 和 Holder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15497847/

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