124697
124697

Reputation: 21893

How can I delay the loading of views in my list adapter while the user is scrolling fast

I have images in my listview that download from the internet. I want to put something in my getView() so that it doesn't download the images if the user is flinging/scrolling fast?

How can I start doing this?

Upvotes: 4

Views: 1232

Answers (1)

Salman Khakwani
Salman Khakwani

Reputation: 6714

You can delay the loading of views in your ListView by following these steps.

Firstly, you should make your ListView Object and YOUR_COSTOM_ADAPTER_OBJECT class variables. then, set a onScroll Listener for your ListView

Get Scrolling Speed from your ListView's onScroll Listener:
Get the scroll speed from your onScroll listener and when the speed gets faster, tell your adapter to not load images.

Example code:

class YourClass extends Activity
{
YOUR_CUSTOM_LISTVIEW_ADAPTER adapter;
ListView YOUR_LISTVIEW;
ArrayList<YOUR_DATA_TYPE> listViewData;

onCreate(...){
.....
.....
//ListView data Initialization
listViewData = new ArrayList<YOUR_DATA_TYPE>();
listViewData.clear();
YOUR_LISTVIEW = (ListView)findViewById(R.id.YOUR_LISTVIEW_ID);
//adapter Initialization
adapter = new YOUR_CUSTOM_LISTVIEW_ADAPTER(....);
YOUR_LISTVIEW.setAdapter(adapter);
}

private OnScrollListener onScrollListener = new OnScrollListener() {

private int previousFirstVisibleItem = 0;
private long previousEventTime = 0;
private double speed = 0;

@Override
public void onScroll(HtcAbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
{
    if (previousFirstVisibleItem != firstVisibleItem)
    {
        long currTime = System.currentTimeMillis();
        long timeToScrollOneElement = currTime - previousEventTime;
        speed = ((double)1/timeToScrollOneElement)*1000;

        previousFirstVisibleItem = firstVisibleItem;
        previousEventTime = currTime;

        Log.d("DBG", "Speed: " +speed + " elements/second");

        //Tell adapter to not load images if it has reached a specific speed
        if(speed>YOUR_DESIRED_VALUE)
        {
            doNotLoadImagesInListView();
        }
        else
        {
            loadImagesInListView();
        }
    }
}

@Override
public void onScrollStateChanged(HtcAbsListView view, int scrollState) 
{
}
};

void doNotLoadImagesInListView()
{
    adapter.isScrollFast = true;
    adapter.notifyDataSetChanged();
    YOUR_LISTVIEW.setAdapter(adapter);
}

void loadImagesInListView()
{
    adapter.isScrollFast = false;
    adapter.notifyDataSetChanged();
    YOUR_LISTVIEW.setAdapter(adapter);
}


}


Configure your Custom List Adapter to make changes accordingly:

public class yourCustomAdapter extends BaseAdapter
{
    public Boolean isScrollFast;

public yourCustomAdapter(..... , ....., Boolean isScrollFast)
{
    ....
    this.isScrollFast = isScrollFast;
    .....
}

@Override
public int getCount() 
{
    .....
}

@Override
public Object getItem(int position) 
{
    .....
}

@Override
public long getItemId(int position) 
{
    .....
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    .......
    if(isScrollFast==true)
    {
        //Don't Load Images
    }
    else
    {
        //Load Images normally
    }

    return vi;
}
}

I Hope this works for you.

Upvotes: 2

Related Questions