List View Items Changes Position When Scrolling Android

ListView Items change position on scroll

Change your getView() method to this:

Map<Integer, View> views = new HashMap<Integer, View>;

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

if (views.containsKey(position)) {
return views.get(position);
}

inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
View v = inflater.inflate(R.layout.fragment_list_news, null);

holder.name = (TextView) v.findViewById(R.id.tittle);
holder.tweet = (TextView) v.findViewById(R.id.news);
holder.avatar = (ImageView) v.findViewById(R.id.image);

holder.name.setText(news.get(position).getTitulo());
holder.tweet.setText(news.get(position).getCopete());
new ImagefetcherTask(position).execute(holder);

v.setTag(holder);
views.put(position, v);
return v;

}

List view items changes position when scrolling android?

In your adapter class override these two methods

@Override
public int getViewTypeCount() {

return getCount();
}

@Override
public int getItemViewType(int position) {

return position;
}

Note: For recyclerview just method getItemViewType(int position) will work.

Position of items in listview is changing when I scroll the listview

Move this outside of where you set up your convertview

programName.setText(values.programNames.get(position));
programTime.setText(values.programTimes.get(position));
new imageDownload(programImage).execute(values.programImageUrls.get(position));

So that it looks like this

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

ViewHolder viewHolder;

if (convertView == null) {

convertView = mInflater.inflate(R.layout.row_layout, null);

TextView programName = (TextView)convertView.findViewById(R.id.programName);
TextView programTime = (TextView)convertView.findViewById(R.id.programTime);
TextView programState = (TextView)convertView.findViewById(R.id.programState);
ImageView programImage =(ImageView)convertView.findViewById(R.id.programImage);

viewHolder = new ViewHolder();
viewHolder.p_Name = programName;
viewHolder.p_Time = programTime;
viewHolder.p_State = programState;
viewHolder.p_Image = programImage;

convertView.setTag(viewHolder);

}else{

viewHolder = (ViewHolder) convertView.getTag();
}

viewHolder.p_Name.setText(values.programNames.get(position));
viewHolder.p_Time.setText(values.programTimes.get(position));
new imageDownload(programImage).execute(values.programImageUrls.get(position));

return convertView;
}

Also, I would highly recommend using Picasso for your image download since it handles cacheing automatically for you. I would also look at injecting your ViewHolder with Butterknife and that will reduce your code up here as well

Listitems change position when scrolling in ListView in Android

There are multiple problems:

1. Instead of

@Override
public Object getItem(int position) {
return null;
}

you should write

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

2. Instead of

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {

grid = new View(mContext);
grid = inflater.inflate(R.layout.raw_offer, null);

// [all that initializing stuff]

} else {
grid = (View) convertView;
}

return grid;
}

you should write

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);

View grid;
if (convertView == null) {
grid = inflater.inflate(R.layout.raw_offer, parent, false);
} else {
grid = convertView;
}

// [all that initializing stuff]

return grid;
}

You can also have a look at the ViewHolder concept to improve the performance of your list:

  • https://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
  • https://www.androidcode.ninja/android-viewholder-pattern-example/

list view position is getting changed when scrolling on android

I solved the problem. i add the

  View view = null;
convertView = null; //in the get view and comments the else part of
if (convertView == null) {
}
/*else{
} */

ListView Changing Items During Scroll

I found the solution thanks to @frozenkoi. Ended up being the static variables inside the ViewHolder causing problems. They are now simply public and the class is static and the issue has been solved.

private static class MyViewHolder {
public TextView adTitle;
public TextView region;
public TextView time;
public ImageView thumbnail;
}

Android list items are changing when scrolling

The problem is ListView is recycling a non-footer row view when it calls getView() for the last item. You need to implement getItemViewType() and getViewTypeCount() in your adapter, to give ListView a hint about what views can be recycled for those items.

private static final int VIEW_TYPE_HEADER = 0;
private static final int VIEW_TYPE_FOOTER = 1;
private static final int VIEW_TYPE_DEFAULT = 2;
private static final int VIEW_TYPE_COUNT = 3;

@Override
public int getViewTypeCount() {
// The total number of row types your adapter supports.
// This should NEVER change at runtime.
return VIEW_TYPE_COUNT;
}

@Override
public int getItemViewType(int position) {
// return a value from zero to (viewTypeCount - 1)
if (position == 0) {
return VIEW_TYPE_HEADER;
} else if (position == getCount() - 1) {
return VIEW_TYPE_FOOTER;
}
return VIEW_TYPE_DEFAULT;
}

Inside of getView() you can call getItemViewType() and use a switch statement to build your rows:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
switch(getItemViewType(position)) {
case VIEW_TYPE_HEADER:
return getHeaderView(position, convertView, parent); // TODO implement this
case VIEW_TYPE_FOOTER:
return getFooterView(position, convertView, parent); // TODO implement this
case VIEW_TYPE_DEFAULT:
return getDefaultView(position, convertView, parent); // TODO implement this
}
}

Listview items change when scrolling in Android

I always face this issue with my listView adapter, when I have alot of conditions in getView(). First suggestions is to use minimum conditions in getView(). The other solution which I use is to initialize all view holder views in getView function as final variable and on every condtion add notifyDataSetChange(); Most of the time doing this solves my problem.
And in place of

if(convertView == null) {
//your code
} else {
convertView.getTag()
}

Use just this

if(converView == null) {
//inflate view
}

No else condition. Also initialize your all textViews and other views outside of this convertView if condition.

Note: This is not the best solution but doing this solves my problem most of the time



Related Topics



Leave a reply



Submit