У меня проблема со списком.Исчезающие изображения ListView
В моем приложении есть ListFragment
, в котором содержится список уведомлений, которые в основном представляют собой текст и изображение.
Они также могут быть отключены при нажатии. При щелчке я устанавливаю поле «active» структуры Notification
на false и вызывает метод NotifyDataSetChanged
. Но когда я просматриваю его вверх и вниз, некоторые изображения исчезают и могут снова появляться снова. Но не тогда, когда я не прокручиваю его.
Все изображения в ресурсах, и я никогда не устанавливал видимость элементов списка.
Вот некоторые из моего кода:
Пользовательские ArrayAdapter
public class NotificationsArrayAdapter extends ArrayAdapter<Notification> {
private final Context context;
private final String[] message_strings;
private final Drawable[] images;
private final LayoutInflater layoutInflater;
private ArrayList<Notification> items;
public NotificationsArrayAdapter (Context context, Resources resources, ArrayList<Notification> items) {
super(context, R.layout.customer_notifications_list_item, items);
this.context = context;
this.items = items;
this.message_strings = new String[] {"message1", "message2", "message3", "message4"};
this.images = new Drawable[] {ContextCompat.getDrawable(context, R.drawable.notifications_blue_circle),
ContextCompat.getDrawable(context, R.drawable.notifications_red_circle),
ContextCompat.getDrawable(context, R.drawable.notifications_grey_circle),
ContextCompat.getDrawable(context, R.drawable.notifications_green_circle)
};
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
v = layoutInflater.inflate(R.layout.customer_notifications_list_item, parent, false);
}
Notification item = getItem(position);
if (item != null) {
TextView textView = (TextView) v.findViewById(R.id.notifications_list_item_text);
ImageView imageView = (ImageView) v.findViewById(R.id.notifications_list_item_image);
if (textView != null) {
if (item.getActive()) {
textView.setTextColor(Color.parseColor("#FFFFFF"));
}
else {
textView.setTextColor(Color.parseColor("#BBBBBB"));
}
textView.setText(message_strings[item.getType()]);
}
if (imageView != null) {
if (item.getActive()) {
imageView.setImageDrawable(images[item.getType()]);
}
else {
imageView.setImageAlpha(0);
}
}
}
return v;
}
}
Фрагмент:
public class CustomerNotificationsFragment extends ListFragment {
public CustomerNotificationsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_customer_notifications, container, false);
ArrayList<Notification> notifications = new ArrayList<Notification>();
for (int i = 0; i < 20; ++i) {
notifications.add(new Notification(1, "blabal"));
}
NotificationsArrayAdapter notificationsArrayAdapter = new NotificationsArrayAdapter(getActivity(), getResources(), notifications);
setListAdapter(notificationsArrayAdapter);
return view;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Notification item = (Notification) this.getListAdapter().getItem(position);
item.setActive(false);
((ArrayAdapter) getListAdapter()).notifyDataSetChanged();
}
}
Спасибо, это помогло, но есть еще одна вещь: когда я проверяю элемент и начинаю прокручивать, появляется только одно изображение и исчезает, все остальные в порядке. Он исчезает только тогда, когда список находится ниже определенного положения. Вы знаете, что может быть? – Daniil
все еще есть что-то не так с вашим ответом: setImageAlpha находится в диапазоне 0..255, поэтому его следует вызывать с аргументом 255. Если у кого-то будет такая же проблема, он вообще не увидит картинку. – Daniil
О, ты прав! Там :) – Nikiole91