gpt4 book ai didi

android - ListView 中的 ImageView 大小问题

转载 作者:行者123 更新时间:2023-11-29 01:37:31 25 4
gpt4 key购买 nike

我遇到了一个错误,当我第一次从 URL 加载带有图像的 ListView 时,尺寸显示不正确。然而,这只是第一次发生。

如果我返回到上一个 fragment ,然后再次返回到 ListView,则图像大小是固定的(尽管我可以暂时看到它们在被修复之前有错误的大小)。

我使用 Android 查询检索图像,此示例用于我的 ListView : http://javatechig.com/android/android-listview-tutorial

SearchResultAdapter.java

public class SearchResultAdapter extends BaseAdapter{

private Context context;
private ArrayList listData;
private LayoutInflater layoutInflater;

public SearchResultAdapter(Context context, ArrayList listData) {
this.context = context;
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}

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

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

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

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

if (convertView == null) {

convertView = layoutInflater.inflate(R.layout.searchresult_single_item, null);
holder = new ViewHolder();

holder.adImage = (ImageView) convertView.findViewById(R.id.adImage);
holder.adTitle = (TextView) convertView.findViewById(R.id.adTitle);
holder.adPrice = (TextView) convertView.findViewById(R.id.adPrice);
holder.adSize = (TextView) convertView.findViewById(R.id.adSize);
holder.adDetails = (TextView) convertView.findViewById(R.id.adDetails);
holder.adMessage = (ImageView) convertView.findViewById(R.id.adMessage);
holder.adFavorite = (ImageView) convertView.findViewById(R.id.adFavorite);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}

AdItem adListItem = (AdItem) listData.get(position);

AQuery aq = new AQuery(convertView);
aq.id(holder.adImage).image(adListItem.getAdImageURL(), true, true, 50, 0, null, AQuery.FADE_IN_NETWORK, 1.0f);

ImageLoader imgLoader = new ImageLoader(context);
imgLoader.DisplayImage(adListItem.getAdImageURL(), holder.adImage);

holder.adTitle.setText(adListItem.getAdTitle());
holder.adPrice.setText(adListItem.getAdPrice());
holder.adSize.setText(adListItem.getAdSize());
holder.adDetails.setText(adListItem.getAdDetails());

return convertView;
}

static class ViewHolder {
ImageView adImage;
TextView adTitle;
TextView adPrice;
TextView adSize;
TextView adDetails;
ImageView adMessage;
ImageView adFavorite;
}

}

SearchResultFragment.java

public class SearchResultFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_searchresult, container, false);

ArrayList listData = getListData();
final ListView searchResult = (ListView) view.findViewById(R.id.listViewSearchResult);
searchResult.setAdapter(new SearchResultAdapter(getActivity(), listData));

searchResult.invalidateViews();
return view;
}

private ArrayList getListData() {

ArrayList results = new ArrayList();
AdItem adData = new AdItem();

adData.setAdImageURL("https://img.wg-gesucht.de/media/up/2014/36/3d3eb5b646560480_2014_09_07_19_26_33.sized.jpg");
adData.setAdTitle("Apartment");
adData.setAdPrice("€300");
adData.setAdSize("37m");
adData.setAdDetails("This is a wonderful apartment at a very reasonable price.");
results.add(adData);

adData = new AdItem();
adData.setAdImageURL("https://img.wg-gesucht.de/media/up/2014/46/a37ed74847504440_img_7409.sized.jpg");
adData.setAdTitle("Apartment");
adData.setAdPrice("€450");
adData.setAdSize("50m");
adData.setAdDetails("This is a wonderful apartment at a very reasonable price.");
results.add(adData);

adData = new AdItem();
adData.setAdImageURL("https://img.wg-gesucht.de/media/up/2014/46/4399903447385080_img_3106.sized.jpg");
adData.setAdTitle("Apartment");
adData.setAdPrice("€500");
adData.setAdSize("62m");
adData.setAdDetails("This is a wonderful apartment at a very reasonable price.");
results.add(adData);

return results;
}

}

这是我的行项目 fragment 布局:

<ImageView
android:id="@+id/adImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:minWidth="50dp"
android:minHeight="50dp"
android:layout_marginRight="20dp"/>

<TextView
android:id="@+id/adTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/adImage" />

<TextView
android:id="@+id/adPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="@id/adTitle"
android:layout_toRightOf="@id/adImage" />

<TextView
android:id="@+id/adSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="@id/adTitle"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/adPrice" />

<TextView
android:id="@+id/adDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_toRightOf="@id/adImage"
android:layout_below="@id/adPrice"/>

<ImageView
android:id="@+id/adFavorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>

<ImageView
android:id="@+id/adMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/adFavorite"/>

第一张图片是第一次加载 ListView 时的样子以及加载后的样子(第二张图片是它应该的样子)。

Before After

最佳答案

如果不需要保持这个最小宽度/高度,您可以将列表行中的 ImageView 属性更改为第二个图像结果:

<ImageView
android:id="@+id/adImage"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:scaleType="centerCrop"/>

关于android - ListView 中的 ImageView 大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27018378/

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