How can I delay loading views in my list adapter when the user scrolls quickly

I have images on my list that are downloaded from the Internet. I want to put something in my getView () so that it doesn't load images if the user scrolls fast or fast?

How can I start doing this?

+4
source share
1 answer

You can defer loading views in the ListView by following these steps.

First, you must make your ListView Object and YOUR_COSTOM_ADAPTER_OBJECT class variables. then install onScroll Listener for your ListView

Get scroll speed from ListView onScroll Listener :
Get the scroll speed from your onScroll listener and, when the speed is faster, tell the adapter not to load images.

Code example:

 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 the appropriate changes:

 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; } } 

Hope this works for you.

+2
source

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


All Articles