1

I'm very confusing with this.

Question is simple, I'm trying to resize the ImageView height, to do, I get the display width and add on it 0.25 of percentage.

Problem, if I set the new value of height outside of the post() ImageView method, the position parameter is deliver in getView() wrong. If I do it inside post() the first elements showed are not rescaled.

Read comments inside code.

public static class ViewHolder {

        ImageView imgAvatar;

        public void loadAvatar(SocialService socialService, long userId) {

            try {

                // SocialService.loadAvatar(..) is working with UniversalImageLoader.
                socialService.loadAvatar(this.imgAvatar, userId, true, false);
            } catch (Exception ex) {

                Log.e("APPERROR", ex.getMessage());
            }
        }
    }

 @Override
public View getView(int position, View view, ViewGroup parent) {

    ViewHolder holder;

    final User user = this.users.get(position);

    if (view == null) {

        holder = new ViewHolder();

        view = inflater.inflate(R.layout.adapter_list, parent, false);

        holder.imgAvatar = (ImageView) view.findViewById(R.id.people_list_avatar);

        // With this commented snippet of code, the first 4 elements (showed in the top of the listview) are not rescaled.
        // Position is deliver ok.
        // The rest of elements that are going showing while scrolling works pretty fine.
        // If scroll down and come back to the top then the 4 top first elements are showing rescaled.
        /*final ImageView imgAvatarAux = holder.imgAvatar;

        holder.imgAvatar.post(new Runnable() {

            @Override
            public void run() {

                imgAvatarAux.getLayoutParams().height =
                        (int) ((Display.deviceWidth(PeopleAdapter.this.context) / 2) * 1.25F);
            }
        });*/

        view.setTag(holder);

        // HERE IS THE QUESTION.
        // If I remove this line of code, position is deliver ok, but if it works, position is deliver senseless. WHY?
        holder.imgAvatar.getLayoutParams().height = (int) ((Display.deviceWidth(PeopleGridAdapter.this.context) / 2) * 1.25F);

        holder.loadAvatar(this.socialService, user.getId());
    }

    holder = (ViewHolder) view.getTag();

    ...

    if (position == 0) {

        // An interface method for another purposes...
        this.waitFinish.onFinish();
    }

    return view;
}
Dani
  • 4,001
  • 7
  • 36
  • 60
  • Please clarify this "if I set the new value of height outside of the post() ImageView method, the position parameter is deliver in getView() wrong" – Anton Malyshev Aug 22 '15 at 08:57
  • Of course, this line -> holder.imgAvatar.getLayoutParams().height = (int) ((Display.deviceWidth(PeopleGridAdapter.this.context) / 2) * 1.25F); – Dani Aug 22 '15 at 09:02

1 Answers1

1

Post your ListView Item layout adapter_list. Code fix [operations on list items outside if(){}else{}]:

    // HERE IS THE QUESTION.
    // If I remove this line of code, position is deliver ok, but if it works, position is deliver senseless. WHY?
    holder.imgAvatar.getLayoutParams().height = (int) ((Display.deviceWidth(PeopleGridAdapter.this.context) / 2) * 1.25F);
    view.setTag(holder);

} else {

    holder = (ViewHolder) view.getTag();
}

    holder.loadAvatar(this.socialService, user.getId());
dieter_h
  • 2,707
  • 1
  • 13
  • 19
  • That was my first, but If I call holder.loadAvatar() always the image is called every time and ListView is too much lagged. – Dani Aug 22 '15 at 09:05
  • Image is loaded via HTTP? – dieter_h Aug 22 '15 at 09:08
  • Is loaded with UniversalImageLoader, the load process -> Memory cache (if not) -> Disk cache (If not) -> HTTP (if not) -> Default resource drawable. – Dani Aug 22 '15 at 09:12
  • Furthermore, there is one customized listview which is scrolling every time. – Dani Aug 22 '15 at 09:14
  • It is possible, that the best way to solve the problem, is achieve that the firsts items showed at the first can be resized. Then everything will work fine. – Dani Aug 22 '15 at 09:20
  • See how works ListView in Android: http://stackoverflow.com/a/14108676/5216567 Check by log how many times is called getView() in your adapter... – dieter_h Aug 22 '15 at 09:35
  • dieter_h, That is the problem, the 4 first items are being taken from recycler every time, because the position parameter in getView(..) is being deliver wrong when set the new heigh to the ImageView, then, every time, until getCount() value, the items are the same. – Dani Aug 22 '15 at 09:55