Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ListView - calling the getView() on demand

I have a ListView which displays in every row an image pulled from the internet, and a string.

In general it works fine. However I want to take control on the way when the views (rows) are inflated. By default when the row is visible the getView() method of the adapter is called.

This of course is not one of the best behaviours because if I have a ListView with several hundreds of records, and I need to reach the ones at the bottom, while scrolling the ListView, the getView() method will be invoked for every row until I reach the footer.

So I want to call getView only after scrolling and the ListView is in paused/idle state, but I do not have idea how to go about this:

Here's how I begun:

listView.setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if(scrollState==SCROLL_STATE_IDLE){
            // Invoke get view only on visible items
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {

    }
});

This is the getView of my adapter:

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

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row, parent, false);

        holder = new ViewHolder();
        holder.title = (TextView) convertView.findViewById(R.id.textView);
        holder.image = (ImageView) convertView.findViewById(R.id.imageView);
        holder.position = position;

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    holder.title.setText(data.get(position).getTitle());
    holder.image.setImageResource(R.drawable.ic_launcher);
    new LoadImageAsync(data.get(position).getUrl(), holder.image).execute();

    return convertView;
}

Please, give me some directions on where should I look in order to achieve this: Call getView() only after scrolling, and only for visible items.

like image 389
Andy Res Avatar asked Aug 07 '12 13:08

Andy Res


People also ask

How to connect an adapter with ListView?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.

How to set ArrayAdapter in android?

Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.


1 Answers

I recently read an article on this... here. I haven't tried it but it seems like a sound theory.

Basically, they advocate adding a boolean that you use to track whether you are scrolling or not and use that as a flag in your adapter. If the boolean is true (you are scrolling), only draw the textviews in your layout. If false (you've stopped scrolling) you draw everything.

You tell the adapter to redraw the visible views when not scrolling by using notifyDataSetChanged().

like image 167
Barak Avatar answered Oct 01 '22 11:10

Barak