gpt4 book ai didi

android - Listview Holder 位置已改组

转载 作者:太空狗 更新时间:2023-10-29 12:49:20 25 4
gpt4 key购买 nike

我对 ListView 有问题,或者更准确地说 - 放置在其上的 ImageView。我的应用正在从 Youtube 下载缩略图。到那个地方的一切都很好。后来,在使用 getView() 时出现了一个奇怪的行为。当我在 if 条件下将数据加载到 holder 时:

    if(convertView == null){

convertView = mInflater.inflate(R.layout.list_item_user_video, null);
holder = new ViewHolder();

holder.title = (TextView) convertView.findViewById(R.id.userVideoTitleTextView);
holder.thumb = (UrlImageView) convertView.findViewById(R.id.userVideoThumbImageView);
Video video = videos.get(position);
holder.title.setText(video.getTitle());
holder.thumb.setImageDrawable(video.getThumbUrl());
convertView.setTag(holder);
}

我的应用程序速度很快,但是在快速滚动时每对 ImageView 和 TextView 的位置都被打乱了。当部分:

 Video video = videos.get(position);
holder.title.setText(video.getTitle());
holder.thumb.setImageDrawable(video.getThumbUrl());

在 if(convertView == null) 之外 ImageView 和 TextView 在它们应该在的地方,但是应用程序非常慢。我已经检查了所有内容,但我完全不知道如何解决这个问题。你可以帮帮我吗?如果您需要额外的信息,请询问。

谢谢。

VideosActivity.java

    public class VideosAdapter extends BaseAdapter{
// The list of videos to display
List<Video> videos;
// An inflator to use when creating rows
private LayoutInflater mInflater;


public VideosAdapter(Context context, List<Video> videos) {
super();
this.videos = videos;
this.mInflater = LayoutInflater.from(context);
}

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

@Override
public Object getItem(int position) {
return videos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}



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

if(convertView == null){

convertView = mInflater.inflate(R.layout.list_item_user_video, null);
holder = new ViewHolder();

holder.title = (TextView) convertView.findViewById(R.id.userVideoTitleTextView);
holder.thumb = (UrlImageView) convertView.findViewById(R.id.userVideoThumbImageView);
Video video = videos.get(position);
holder.title.setText(video.getTitle());
holder.thumb.setImageDrawable(video.getThumbUrl());
convertView.setTag(holder);
}

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





return convertView;
}
static class ViewHolder{
UrlImageView thumb;
TextView title;
Video video;
int id;
}
}

网址 ImageView .java

  public class UrlImageView extends LinearLayout {

private Context mContext;
private Drawable mDrawable;
private ProgressBar mSpinner;
private ImageView mImage;


public UrlImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

public UrlImageView(Context context) {
super(context);
init(context);
}


private void init(final Context context) {
mContext = context;

mImage = new ImageView(mContext);
mImage.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mImage.setVisibility(View.GONE);

mSpinner = new ProgressBar(mContext);
mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

mSpinner.setIndeterminate(true);

addView(mSpinner);
addView(mImage);
}


public void setImageDrawable(final String url) {
mDrawable = null;
mSpinner.setVisibility(View.VISIBLE);
mImage.setVisibility(View.GONE);
new Thread() {
public void run() {
try {
mDrawable = getDrawableFromUrl(url);
imageLoadedHandler.sendEmptyMessage(RESULT_OK);
} catch (MalformedURLException e) {
imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
} catch (IOException e) {
imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
}

};
}.start();
}


private final Handler imageLoadedHandler = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case RESULT_OK:
mImage.setImageDrawable(mDrawable);
mImage.setVisibility(View.VISIBLE);
mSpinner.setVisibility(View.GONE);
break;
case RESULT_CANCELED:
default:
// Could change image here to a 'failed' image
// otherwise will just keep on spinning
break;
}
return true;
}
});


private static Drawable getDrawableFromUrl(final String url) throws IOException, MalformedURLException {

return Drawable.createFromStream(((InputStream) new URL(url).getContent()), "name");
}


}

list_item_user_video.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >


<com.example.example.ui.widget.UrlImageView
android:id="@+id/userVideoThumbImageView"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_marginRight="10dp"
android:contentDescription="YouTube video thumbnail"
android:src="@drawable/ic_launcher"/>

<TextView
android:id="@+id/userVideoTitleTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Video Title Not Found" />

</LinearLayout>

最佳答案

从 VideosAdapter 的 getView() 方法中的 if/else 移动以下行

holder.title.setText(video.getTitle());
holder.thumb.setImageDrawable(video.getThumbUrl());

就在上面

return convertView;

关于android - Listview Holder 位置已改组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14162640/

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