将位图从listview中的网络图像视图传递到片段时,我遇到了一些奇怪的行为。
我从listview适配器中的url加载图像,然后当点击listview项时,该列表视图中的图像将用作片段中的背景。但是,当我这样做时,片段中会显示错误的图像,显示的图像是当前在屏幕上可见的列表视图底部的项目。
这是我的适配器设置listview:
public class EventListAdapter extends BaseAdapter {
Context context;
public static ArrayList<EventObject> eventList;
private LayoutInflater mInflater;
private Bitmap bitmap;
private NetworkImageView img_main;
private RelativeLayout layoutItem;
private TextView txt_event_name;
private TextView txt_event_details;
public EventListAdapter(Context context, ArrayList<EventObject> eventList) {
this.context = context;
this.eventList = eventList;
mInflater = LayoutInflater.from(context);
}
public void addItems(ArrayList<EventObject> eventList) {
this.eventList.addAll(eventList);
notifyDataSetChanged();
}
@Override
public int getCount() {
return eventList.size();
}
@Override
public Object getItem(int position) {
return eventList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("all")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
try {
// reuse of convertView
if (convertView == null) {
layoutItem = (RelativeLayout) mInflater.inflate(R.layout.view_event_list_item, parent, false);
} else {
layoutItem = (RelativeLayout) convertView;
}
img_main = (NetworkImageView) layoutItem.findViewById(R.id.img_main);
txt_event_details = (TextView) layoutItem.findViewById(R.id.txt_event_details);
txt_event_name = (TextView) layoutItem.findViewById(R.id.txt_event_name);
txt_event_name.setTypeface(FontClass.getOpenSansRegular(context));
txt_event_details.setTypeface(FontClass.getOpenSansLight(context));
img_main.setDefaultImageResId(R.drawable.ic_weekends_matter_black);
img_main.setImageUrl(eventList.get(position).getImageUrl(), new ImageLoader(NetworkManager.getInstance().requestQueue, new LruBitmapCache()));
txt_event_name.setText(eventList.get(position).getEventName());
txt_event_details.setText(eventList.get(position).getVenueName() + ", " + getFormattedDate(eventList.get(position).getEventDate()));
layoutItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((HomeActivity) context).setEventProfileFrag(eventList.get(position).getFullJsonObject(), ((BitmapDrawable)img_main.getDrawable()).getBitmap());
}
});
} catch (Exception e) {
e.printStackTrace();
}
notifyDataSetChanged();
return layoutItem;
}
private String getFormattedDate(String dateString) throws ParseException {
//"2016-06-27 16:00:00"
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.UK);
Date newDate = format.parse(dateString);
format = new SimpleDateFormat("EEEE dd MMMM", Locale.UK);
String date = format.format(newDate);
return date;
}
}
然后最后在片段中我从传递给片段的参数中检索图像:
mParam2 = BitmapFactory.decodeByteArray(getArguments().getByteArray(ARG_PARAM2), 0, getArguments().getByteArray(ARG_PARAM2).length);
img_header.setImageBitmap(mParam2);
答案 0 :(得分:1)
用下面的替换你的适配器类,它可能会起作用:
public class EventListAdapter extends BaseAdapter {
Context context;
public static ArrayList<EventObject> eventList;
private LayoutInflater mInflater;
private Bitmap bitmap;
private NetworkImageView img_main;
class ViewHolder{
private RelativeLayout layoutItem;
private TextView txt_event_name;
private TextView txt_event_details;
}
public EventListAdapter(Context context, ArrayList<EventObject> eventList) {
this.context = context;
this.eventList = eventList;
mInflater = LayoutInflater.from(context);
}
public void addItems(ArrayList<EventObject> eventList) {
this.eventList.addAll(eventList);
notifyDataSetChanged();
}
@Override
public int getCount() {
return eventList.size();
}
@Override
public Object getItem(int position) {
return eventList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("all")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
try {
// reuse of convertView
ViewHolder holder;
if (convertView == null) {
holder=new ViewHolder();
holder.layoutItem = (RelativeLayout) mInflater.inflate(R.layout.view_event_list_item, parent, false);
} else {
holder.layoutItem = (RelativeLayout) convertView;
}
holder.img_main = (NetworkImageView) layoutItem.findViewById(R.id.img_main);
holder.txt_event_details = (TextView) layoutItem.findViewById(R.id.txt_event_details);
holder.txt_event_name = (TextView) layoutItem.findViewById(R.id.txt_event_name);
holder.txt_event_name.setTypeface(FontClass.getOpenSansRegular(context));
txt_event_details.setTypeface(FontClass.getOpenSansLight(context));
holder.img_main.setDefaultImageResId(R.drawable.ic_weekends_matter_black);
holder.img_main.setImageUrl(eventList.get(position).getImageUrl(), new ImageLoader(NetworkManager.getInstance().requestQueue, new LruBitmapCache()));
holder.txt_event_name.setText(eventList.get(position).getEventName());
holder.txt_event_details.setText(eventList.get(position).getVenueName() + ", " + getFormattedDate(eventList.get(position).getEventDate()));
holder.layoutItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((HomeActivity) context).setEventProfileFrag(eventList.get(holder.getPosition()).getFullJsonObject(), ((BitmapDrawable)img_main.getDrawable()).getBitmap());
}
});
} catch (Exception e) {
e.printStackTrace();
}
notifyDataSetChanged();
return layoutItem;
}
private String getFormattedDate(String dateString) throws ParseException {
//"2016-06-27 16:00:00"
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.UK);
Date newDate = format.parse(dateString);
format = new SimpleDateFormat("EEEE dd MMMM", Locale.UK);
String date = format.format(newDate);
return date;
}
}