0

TLDR:

I'm setting myListView.setVisibility(View.GONE);, but it's not dissappearing until later... do I need to let it know somehow that I've changed it's visibility? Or do I need to also hide it's inner elements or something?

Description of Problem:

I have a normal news app. You see a list of articles for the "main" section, then you can click the options to select a new section.

When the user clicked, the section title changed, but the articles in the list would just sit there with "old" content until the new content is loaded, then it would flash to the new content.

This isn't ideal obviously. I'd like the list to disappear, show a loading animation, then, after the new data is retrieved (either from DB or online, then DB), it shows the new content.

I found this SO question which seemed like what I want, but...

I'm setting GONE immediately upon selection of the menu, then VISIBLE after it import the articles and loads the new ones... but it's not disappearing at all during that. I know the GONE code works, because if I remove my VISIBLE code, the articles never reappear.

Do I need to say "View.GONE", then tell it to update it's visibility or something?


My Code (MainActivity):

public static void sectionSelected()
{

    String selectedText = sectionsSpinner.getSelectedItem().toString();
    String[] selectedSection = Section.stringToSection(selectedText);

    //check if it was already the current section
    if(!Section.isEqual(Section.currentSection, selectedText))
    {

        //hides list of articles
        articleEntryListView.setVisibility(View.GONE);

        //sets new currentSection
        Section.currentSection = selectedSection; // Section.stringToSection(sectionsSpinner.getSelectedItem().toString());

        //imports articles (if it's been more than an hour since last import)
        articlesDataSource.importArticles(Section.currentSection, true, false);

        //loads article from database to the list
        loadArticlesIntoList(Section.currentSection);
    }
}

public static void loadArticlesIntoList(String[] section)
{
    //clears the list
    //articleEntryAdapter.clear(); //don't think I need this now that I'm just going to hide it
    //articleEntryAdapter.notifyDataSetChanged();

    //POPULATES THE LIST OF ARTICLES, THROUGH THE ADAPTER
    for(final Article a1 : articlesDataSource.getArticles(section))
    {
        articleEntryAdapter.add(a1);
    }
    articleEntryAdapter.notifyDataSetChanged();

    //shows list of articles
    articleEntryListView.setVisibility(View.VISIBLE);
}

ADDITION: here is my importAricles() code: http://pastebin.com/8j6JZBej

Community
  • 1
  • 1
Dave
  • 28,833
  • 23
  • 113
  • 183
  • How fast are the operations in `importArticles`? Android executes that callback for the menu(real menu?) in one sweep and the `GONE`/`VISIBLE` cancel each other, so you don't see anything disappear. It's the same thing like trying to make a view appear(which was set to gone) on a button's `onClick` callback, pause for some time to simulate some work and then make the view gone again. Like in your case you'll not see that view(even for a brief period of time). – user Jul 19 '12 at 16:01
  • @Luksprog - The importArticles can take up to around 10 seconds (sometimes less certainly, but... a noticable timeframe). I can see my log "hiding now"... then delay as it downloads/imports the articles, then "showing now". The "menu" is a spinner at the top of the view. The thing is - even if I remove my "VISIBLE" part (ie it will hide, but never return), it doesn't disappear until after the import is complete. – Dave Jul 19 '12 at 16:19
  • What exactly are you doing on the `importArticles` method? Can you share some code? Also there is no need to call `articleEntryAdapter.notifyDataSetChanged();` after you set the visibility to `GONE` because the `ListView` is hidden. – user Jul 19 '12 at 17:40
  • Luksprog - I've added a link to my importArticles code. The notifyDataSetChange was just a random attempt to get this visibility thing working - I've removed it. – Dave Jul 19 '12 at 17:46
  • I don't see in your code nothing that would suggest that you do the operations from `importArticles` in another thread/`AsyncTask`. Am I correct? If this is case then I think it's normal that the list disappears only after the import because it has to wait for that code to terminate before the UI thread can actually make the list gone(as it doesn't happen instantaneous). If you actually make those call on another thread then I don't understand the behavior that you experience. – user Jul 19 '12 at 18:01

1 Answers1

0

You have to invalidate the view anytime you make a change to its appearance, so make a call to articleEntryListView.invalidate() after setting the visibility.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • That doesn't change anything except now my list data doesn't change when the new section is loaded. – Dave Jul 19 '12 at 15:39