简体   繁体   中英

setVisibility(View.GONE) does not work in Fragment

I have a FragmentActivity that's parsing JSON and updates the view. OnCreate I have a ProgressBar initialized when my view is still in the load process. Then after the loading is done, I set

progressBar.setVisibility(View.GONE);

but it would not take affect. I did make sure that its exactly same instance by doing toString() on the progressBar variable and its exactly same. I also made sure that its actually reaches the point. Also I donn't have any onClicks here, it simply needs to dissapear when content is loaded.

I watched this tutorial and it works for the guy there.

Method that's setting the visibility to GONE

  public void updateUI(){
   if(movies != null) {
       for (int i = 0; i < array1.length; i++) {
           //Log.v("PATH in array1 " , movies.get(i).getPath());
           array1[i] = (movies.get(i).getPath());
       }

       gridView.setAdapter(imageAdapter);


   }else{
       Log.v("MOVIE ARRAY", "- I HAVE NO DATA");
   }

    Log.v("HIDE", "BEFORE");
    Log.v("PROGRESS_BAR", progressBar.toString());
    progressBar.setVisibility(View.GONE);

    Log.v("HIDE", "AFTER");

}

My onCreate method that creates the variable

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.fragment_main, null);
    this.progressBar = (ProgressBar)v.findViewById(R.id.progress_bar);

    Log.v("PROGRESS_BAR", progressBar.toString());

    if (savedInstanceState != null){
        Log.v("onCreate ", "EXISTING STATE");
        this.movies = savedInstanceState.getParcelableArrayList("movies");
    }else{
        Log.v("onCreate ", "EMPTY STATE");
    } 

    setHasOptionsMenu(true);


}

onStart

 @Override
public void onStart() {
    super.onStart();
    Log.v("onStart ", "START");
    //Log.v("Movies Size ", String.valueOf(movies.size()));
            update();
}

update()

 private void update(){

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String sortType = prefs.getString(getString(R.string.pref_sort_key), getString(R.string.pref_sort_default));

    if (sortType.equals(sortTypeSaved)){
        Log.v("Equals", "YAY");
        Log.v(sortType.toString(), sortTypeSaved.toString());

        if (movies == null || movies.isEmpty()){

            Log.v("movies ARRAY", "EMPTY");
            DownloadJsonDataTask asyncDownload = new DownloadJsonDataTask();
            asyncDownload.execute(sortType);
        }else{
            Log.v("Equals", "NOPE");
            Log.v("movies ARRAY", " not EMPTY");
            updateUI();
        }
    }else{
        Log.v(sortType.toString(), sortTypeSaved.toString());
       // if (movies == null || movies.isEmpty()){
        DownloadJsonDataTask asyncDownload = new DownloadJsonDataTask();
            asyncDownload.execute(sortType);
      //  }else{
       // }
    }

in case if asynctask was run then i call it from onPostExacute

  @Override
    protected void onPostExecute(List<Movie> movies) {
        updateUI();
    }

Here is the screen of the log where you can see that its definately the same instance 在此处输入图片说明

Please let me know what am I doing wrong, I have been on this all day and cannot find the solution. Thank you

I wonder how I missed it the first time.You are inaflating the view two time. Once at

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.fragment_main, null);

and then again at

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

Which is why it is not working. Do not inflate in onStart() and it will work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM