gpt4 book ai didi

android - setTag 和 getTag 问题 android listview

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

第一次启动 ListView 时,标题按预期显示。当我向下滚动并出现时,相同的 ListView ,标题消失。当我删除 set 和 getTag 代码时,这个问题就解决了。但这阻碍了 ListView 的性能。我在这里做错了什么导致了这个问题。

package com.mediaplayer.adapter;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;

public class ReadLisstAdapter extends BaseAdapter implements OnScrollListener {
private Activity activity;
ArrayList<SongInfo> song_array;
private LayoutInflater inflater = null;
BaseAdapter adapter;
ListView lv;
Thread t;
ImageDownloader imageLoader;
SongInfoDatabase database;
String searchString = "";
DecimalFormat format;
int min, sec, total;

public ReadLisstAdapter(Activity activity2, ArrayList<SongInfo> song_array,
ListView lv) {
activity = activity2;
this.song_array = song_array;
this.lv = lv;
imageLoader = new ImageDownloader(this,
activity.getApplicationContext());

database = new SongInfoDatabase(activity.getApplicationContext());
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.lv = lv;
imageLoader.loadImage(0, 10);
lv.setOnScrollListener(this);
format = new DecimalFormat("#.00");
}

public ArrayList<SongInfo> getUrlList() {
return song_array;
}

public int getCount() {
// TODO Auto-generated method stub
return song_array.size();
}

public ListView getListView() {
return lv;
}

public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

public class ViewHolder {
public TextView title;
public TextView duration;
public TextView album;
public TextView artist;
public ImageView image;
public TextView header;
int pos;
}

public View getView(final int arg0, View vi, ViewGroup arg2) {
ViewHolder holder;

if (vi == null) {

holder = new ViewHolder();
vi = inflater.inflate(R.layout.songlist_item, null);
holder.header = (TextView) vi.findViewById(R.id.label_header_textview);
holder.title = (TextView) vi.findViewById(R.id.song_textView);
holder.image = (ImageView) vi.findViewById(R.id.songlist_imageView);
holder.album = (TextView) vi.findViewById(R.id.song_album_textView);
holder.artist = (TextView) vi
.findViewById(R.id.song_artist_textView);
holder.duration = (TextView) vi
.findViewById(R.id.song_duration_textView);
holder.pos = arg0;
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}

try {


holder.title.setText(song_array.get(arg0).getTitle());
total = Integer.parseInt(song_array.get(arg0).getDuration()) / 1000;
min = total / 60;
sec = total % 60;
holder.duration.setText(min + ":" + sec);
holder.album.setText("from " + song_array.get(arg0).getAlbum());
holder.artist.setText("by " + song_array.get(arg0).getArtist());

Uri albumArtUri = Uri
.parse("content://media/external/audio/albumart");
final Uri uri = ContentUris.withAppendedId(albumArtUri,
Long.parseLong(song_array.get(arg0).getAlbum_id()));
holder.image.setImageBitmap(imageLoader.getDrawble(uri.toString()));
if (song_array.get(arg0).getTitle().trim().toUpperCase(Locale.US)
.charAt(0) != song_array.get(arg0 - 1).getTitle().trim()
.toUpperCase(Locale.US).charAt(0)) {
setSection(holder.header, song_array.get(arg0).getTitle());
} else {
holder.header.setVisibility(View.GONE);

}
} catch (Exception e) {
setSection(holder.header, song_array.get(arg0).getTitle());
}

return vi;
}

private void setSection(TextView text, String label) {
text.setBackgroundColor(0xffe47168);
text.setTextColor(Color.GREEN);
text.setText((label.substring(0, `enter code here`1) + "").toUpperCase());
text.setTextSize(15);
text.setPadding(5, 0, 0, 0);
text.setGravity(Gravity.CENTER_VERTICAL);

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub

}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
switch (scrollState) {
case SCROLL_STATE_IDLE:
imageLoader.loadImage(lv.getFirstVisiblePosition(),
lv.getLastVisiblePosition());
break;

}

}

}

最佳答案

holder.header.setVisibility(View.GONE) 但没有匹配的 setVisibility(View.VISIBLE) 使标题在 View 中再次可见被回收成为标题。

通常,您的 getView() 应该在回收 View 中重新配置所有内容。如果有用于某些自定义的 if 分支,则应该有一个恢复默认值的 else 分支。

关于android - setTag 和 getTag 问题 android listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17022657/

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