Height of ListView Controls

I have a ListView whose items can be removed by scrolling them. When an item scrolls, its database row is deleted, as well as its object in the adapter dataset and notifyDataSetChanged() called. The problem is that these objects have different heights - one can be single-line, the second can have four lines. Therefore, when I have some elements in the list, let's say:

1. One line

2. Two lines

3. Three lines

4. One line

And the second is removed, the third goes to its place (as expected), but is cut to two lines. And when the third one is removed, the height of the fourth element increases in accordance with the deleted element.

I found a way to fix this, but it's probably wrong - it creates a new view, even if the convertView is not null.

I wonder if this can be achieved in another convenient way.

Current adapter code:

 public class CounterItemAdapter extends BaseAdapter{ private Activity activity; private ArrayList<CounterItem> data; private SQLiteOpenHelper helper; private static LayoutInflater inflater = null; public CounterItemAdapter(Activity activity, ArrayList<CounterItem> data, SQLiteOpenHelper helper) { this.activity = activity; this.data = data; this.helper = helper; inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return data.size(); } @Override public CounterItem getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return getItem(position).getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; //if(convertView == null) view = inflater.inflate(R.layout.counter_list_item, null); TextView nameView = (TextView)view.findViewById(R.id.nameView); //other, unrelated views were here final CounterItem counterItem; counterItem = getItem(position); nameView.setText(counterItem.getName()); return view; } } 
+4
source share
1 answer

Ok, I found a solution.

Added int lastValidPosition = -1; in the adapter, then adapter.lastValidPosition = position-1 in the delete callback method to adapter.notifyDataSetChanged(); , but in the getView() method of the adapter, I changed

 //if(convertView == null) view = inflater.inflate(R.layout.counter_list_item, null); 

to

 if(lastValidPosition < position | convertView == null){ view = inflater.inflate(R.layout.counter_list_item, null); lastValidPosition = position; } 

and it works great.

0
source

Source: https://habr.com/ru/post/1493940/


All Articles