gpt4 book ai didi

android - 即使 View 存在,convertView 也会作为 null 传递

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:32 25 4
gpt4 key购买 nike

我开发了一个 android 应用程序,其中图像和文本显示在 GridView 中,当用户向下滚动时会显示接下来的十个项目(图像和文本)。在调用 adapter.notifyDataSetChanged() 之后调用适配器的 getView 方法时会出现问题。适配器回收数据,但位置在 GridView 中重新排列和重复。在添加条件以检查 convertView 是否为 null 之前,我没有遇到过这个问题。

Activity 类:

public class DynamicListViewActivity extends Activity implements
OnScrollListener {

int visibleElements;
int scrollState;
int count;
TextAdapter adapter = new TextAdapter();
int total=200;// total items limit in grid view


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.grid);
count=10;
GridView grid = (GridView) findViewById(R.id.gridview);
grid.setAdapter(adapter);
grid.setOnScrollListener(this);

}

public void onScroll(AbsListView view, int firstVisible, int visibleCount,
int totalCount) {
visibleElements= visibleCount;
}

public void onScrollStateChanged(AbsListView v, int s) {
Log.d("ScrollState", s+"");
scrollState=s;
isScrollStateComplete();
}

public void isScrollStateComplete(){
if(visibleElements>0 && scrollState==SCROLL_STATE_IDLE && total>count){
int diff=total-count;
count+=(diff>=10)?10:diff;//update count to next ten items
adapter.notifyDataSetChanged();
}
}


class TextAdapter extends BaseAdapter {
public int getCount() {
return count;
}

public Object getItem(int pos) {
return pos;
}

public long getItemId(int pos) {
return pos;
}

public View getView(int pos, View convertView, ViewGroup p) {

View v = convertView;
System.out.println("pos : "+pos+" boolean "+(v==null));// log to check position and convertView
if(v==null){
v =((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.textlayout,null);

ImageView iv = (ImageView) v.findViewById(R.id.album_thumbnail);
iv.setImageDrawable(getResources().getDrawable(R.drawable.icon));

TextView tvAlbumName = (TextView)v.findViewById(R.id.album_name);
tvAlbumName.setText("postion "+pos);

TextView tvAlbumDesc = (TextView)v.findViewById(R.id.album_description);
tvAlbumDesc.setText("");

}

return v;
}
}
}

在滚动 GridView 之前正确显示。日志:

08-05 14:24:34.440: INFO/ActivityManager(58): Starting activity: Intent {  act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mis.list.demo/.DynamicListViewActivity }
08-05 14:24:34.642: INFO/System.out(685): pos : 0 boolean true
08-05 14:24:34.710: INFO/System.out(685): pos : 0 boolean false
08-05 14:24:34.710: INFO/System.out(685): pos : 1 boolean true
08-05 14:24:34.730: INFO/System.out(685): pos : 2 boolean true
08-05 14:24:34.800: INFO/System.out(685): pos : 3 boolean true
08-05 14:24:34.860: INFO/System.out(685): pos : 4 boolean true
08-05 14:24:34.880: INFO/System.out(685): pos : 5 boolean true
08-05 14:24:34.910: INFO/System.out(685): pos : 6 boolean true
08-05 14:24:34.920: INFO/System.out(685): pos : 7 boolean true
08-05 14:24:34.960: INFO/System.out(685): pos : 0 boolean true
08-05 14:24:35.030: INFO/ActivityManager(58): Displayed activity com.mis.list.demo/.DynamicListViewActivity: 520 ms (total 520 ms)

第一次滚动结束后

08-05 14:26:15.740: DEBUG/ScrollState(685): 1
08-05 14:26:15.830: DEBUG/dalvikvm(685): GC_EXTERNAL_ALLOC freed 3624 objects / 257464 bytes in 71ms
08-05 14:26:16.210: INFO/System.out(685): pos : 8 boolean false
08-05 14:26:16.210: INFO/System.out(685): pos : 9 boolean true
08-05 14:26:16.250: DEBUG/ScrollState(685): 0
08-05 14:26:16.260: INFO/System.out(685): pos : 0 boolean true
08-05 14:26:16.271: INFO/System.out(685): pos : 0 boolean false
08-05 14:26:16.271: INFO/System.out(685): pos : 1 boolean false
08-05 14:26:16.271: INFO/System.out(685): pos : 2 boolean false
08-05 14:26:16.271: INFO/System.out(685): pos : 3 boolean false
08-05 14:26:16.271: INFO/System.out(685): pos : 4 boolean false
08-05 14:26:16.271: INFO/System.out(685): pos : 5 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 6 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 7 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 8 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 9 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 10 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 11 boolean true
08-05 14:26:16.371: DEBUG/dalvikvm(685): GC_EXTERNAL_ALLOC freed 644 objects / 33224 bytes in 41ms
08-05 14:26:45.270: WARN/KeyCharacterMap(685): No keyboard for id 0
08-05 14:26:45.270: WARN/KeyCharacterMap(685): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-05 14:26:45.341: INFO/System.out(685): pos : 12 boolean true
08-05 14:26:45.351: INFO/System.out(685): pos : 13 boolean true
08-05 14:26:45.371: INFO/System.out(685): pos : 14 boolean true
08-05 14:26:45.380: INFO/System.out(685): pos : 15 boolean true
08-05 14:26:45.450: INFO/System.out(685): pos : 16 boolean false
08-05 14:26:45.450: INFO/System.out(685): pos : 17 boolean false
08-05 14:26:45.460: INFO/System.out(685): pos : 18 boolean false
08-05 14:26:45.460: INFO/System.out(685): pos : 19 boolean false

但是网格显示了重复的值,而不是按应有的顺序显示。抱歉,我是新用户,不允许发布图片。

在日志中,我看到位置 0 convertView==null 为真。

如何设置这个权利,因为我计划下载图像来代替当前使用的 ImageView 的 android 图标。

请帮忙。

最佳答案

您对 convertView 的解释不正确。

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

View v = convertView;

//If convertView is null create a new view, else use convert view
if (v == null)
v = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.textlayout, null);

ImageView iv = (ImageView) v.findViewById(R.id.album_thumbnail);
iv.setImageDrawable(getResources().getDrawable(R.drawable.icon));

TextView tvAlbumName = (TextView)v.findViewById(R.id.album_name);
tvAlbumName.setText("postion "+pos);

TextView tvAlbumDesc = (TextView)v.findViewById(R.id.album_description);
tvAlbumDesc.setText("");

return v;
}

是你应该做的。

由于您所做的事情,在 convertView 不为 null 的情况下,您只是返回 convertView 而没有更新数据,因此您的图像被重复。仅在 convertView 为 null 的情况下才设置数据。

适配器和 ListView 系统之间的契约:如果传递的 convertView 为空,则创建一个新的,否则重新使用它。

关于android - 即使 View 存在,convertView 也会作为 null 传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6953866/

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