gpt4 book ai didi

android - Listview 创建所有元素而不是可见元素

转载 作者:行者123 更新时间:2023-11-29 21:09:03 25 4
gpt4 key购买 nike

我有一个问题,我没有在 stackoverflow(或到目前为止的任何其他地方)上找到任何解决方案。

总结:

我有 scrollview 和它里面的一个 ListView,它的可见性消失了(可以用 TextView 切换)。

当我设置适配器内容时,在我的 Activity 中,onScroll get 调用带有 4 个可见项,但适配器的 getView get 调用整个数据集(181 项),这带来了很多性能问题/imageref_ashmem 创建失败.. .(当然删除图像加载删除创建失败但很好 :D)

=== Activity ===

onScroll 中的 Activity 结果

firstVisibleItem    0   
visibleItemCount 4
totalItemCount 181

===布局提取===

<ScrollView 
android:id="@+id/UserProfileView_ScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:background="#00FFFFFF" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF" >

<RelativeLayout
android:id="@+moreDetails/UserProfileView_BottomLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+profile/UserProfileView_MoreDetails"
android:background="#FFFFFFFF" >

<TextView
android:id="@+id/UserProfileView_About"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ff111111"
android:visibility="gone" />

<ListView
android:id="@+id/UserProfileView_MoreDetails_ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:footerDividersEnabled="false"
android:divider="@color/unactivatedLightGray"
android:dividerHeight="1dp"
android:listSelector="@android:color/transparent"
android:visibility="gone" />

</RelativeLayout>

</RelativeLayout>

</ScrollView>

BottomLayout 的高度以编程方式设置为屏幕宽度的 3/4。

=== 适配器 ===

public class CommonAdapter extends BaseAdapter {

public List<Common> list;
private LayoutInflater inflater;
private AQuery aq;

public CommonAdapter(Context context, List<Common> list) {
this.inflater = LayoutInflater.from(context);
this.list = list;
this.aq = new AQuery(context);
}

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

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

private class ViewHolder {
ImageView thumbImg;
TextView name;
}

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

if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_view_item, null);
holder.thumbImg = (ImageView) convertView.findViewById(R.id.CommonCell_Thumb);
holder.name = (TextView) convertView.findViewById(R.id.CommonCell_Name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

Common obj = list.get(position);

if (list.get(position).getPicture() != null)
aq.id(holder.thumbImg).image(obj.getPicture());
else
aq.id(holder.thumbImg).image(Util.Thumb(list.get(position).getId()), options);
holder.name.setText(obj.getName());

return convertView;
}
}

编辑:每次我滚动时,它都会再次解析整个数据集,因此 181 调用 getView。

最佳答案

我假设您的 aq.id(stuff) 加载了一个异步图像。

如果是这样,请检查您的 layout/list_view_item.xml,它的高度可能设置为 wrap_content 并且 ImageView 也没有固定高度。这意味着加载图像时该行的高度为零或非常小。因此,所有 182 行一次都适合 ListView,并且会为每一行调用 getView()。

为每一行或 ImageView 设置一个固定的 heightminHeight ListView 将只调用 getView() 尽可能多的行适合 ListView。

希望对你有帮助

关于android - Listview 创建所有元素而不是可见元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23532347/

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