Skip to main content

Reload GridView without scrolling - Android

How to prevent GridView from scrolling while reloading data?

Common mistake developers make is they create new Adapters and set it to the GridView to reload or refresh the content.

This will result in the grid scrolling towards the top which in turn makes your App less user friendly.

You can prevent GridView from scrolling up while refreshing data by re-using the Adapter created and just reloading the data.

For this, add a method say refillItems() in your Adapter implementation class as given below:

public void refillItems(List<HashMap<String,String>> items) {
    _items.clear();
    _items.addAll(items);
    notifyDataSetChanged();
}

Now call refillItems() from your Activity class whenever new set of data is available(or at fixed intervals):

GridView gView = (GridView) findViewById(R.id.grid);
if (gView.getAdapter() == null) {
    MyAdapter myAdapter = new MyAAdapter(MyActivity.this, items);
    gView.setAdapter(myAdapter);
} else {
    MyAdapter myAdapter = ((MyAdapter)gView.getAdapter());
    myAdapter.refillItems(items);
}

This will make sure the grid is not scrolled back to the top.

Comments

Post a Comment

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to our feed and get articles like this delivered automatically to your feed reader? Like our Facebook Page.

Popular

Induction Cooker Showing an Error Code? Induction Cooker Error Codes Explained

Are you searching for Induction Cook-top error codes? Here you can find the error codes of all popular induction cooktops and how to troubleshoot it.

These are for your reference only, do not try to open your cooktop without proper safety measures, we advise you to call the service person if any servicing is needed.

If you want to know how Induction Cook-top works, read our previous article titled What is Induction Cooker? How Induction Cooker Works?.

Whirlpool Induction Cooker Error CodesError CodeErrorSolutionF0An internal error was detected.Disconnect power. Wait 5 seconds before reconnecting power. If the symbol appears again, call for service.F2The surface cooking area is too hot and has turned off.Remove the pans from the surface cooking area. "F2" will disappear when the surface cooking area has cooled. If you turn the surface cooking area back on and "F2" reappears, the cooktop is still too hot. Turn off the surface cooking area and allow it to cool.F4The po…

HDFC Bank introduces Missed Call Service to know Account Balance

Missed call is a powerful business tool in developing countries like India where customers give a miss call to specific phone numbers for getting account details, providing feedback, voting etc. On receiving a missed call from a registered phone number, the underlying app performs a phone number lookup and sends the data to the caller via text message(SMS) or records the call details for future processing.HDFC Bank recently introduced missed call service for its retail customers which allows to retrieve bank account details, mini statement etc. by simply giving a miss call to their toll free numbers.Following services are now available: 1800 270 3333 - Account Balance 1800 270 3355 - Mini Statement 1800 270 3366 - Request for new Cheque Book 1800 270 3377 - Request for Bank account statement Also you can download HDFC Mobile Banking Application by giving a missed call to : 1800 270 3344. Other banks providing missed call serviceAxis bank(known as Axis Dial) - 09225892258Bank of India…

Top Travel Websites Reviewed - Makemytrip, Yatra, ClearTrip, Ezeego1 and TravelGuru

Disclaimer: This is an independent review on some of the most popular travel websites, I am not associated with any of these travel websites nor their promoters. If you do not agree with any of the comparisons made here, post your concerns as a comment so that the same will be reviewed and the content under question will removed from this blog.

Websites Reviewed:MakeMyTrip - http://www.makemytrip.com/Yatra.com - http://www.yatra.com/ClearTrip.com - http://www.cleartrip.com/EzeeGo1 - http://www.ezeego1.co.in/TravelGuru.com - http://www.travelguru.com/Search Engine Visibility :: Google.co.inKeyword :: Book FlightRankSiteRemarks1Makemytrip.com Google shows the following as group results: Domestic Airlines - International Flights - Indigo Airlines - Air India, which is a guge plus!2Yatra.com Same here, Domestic Flights - Trains - International Flights - Hotels are shown as group results3Cleartrip.com4Ezeego1.co.in5Travelguru.com Keyword rich title but not user friendly - Hotels India Disco…