简体   繁体   English

setVisibility(View.GONE)在Fragment中不起作用

[英]setVisibility(View.GONE) does not work in Fragment

I have a FragmentActivity that's parsing JSON and updates the view. 我有一个解析JSON并更新视图的FragmentActivity。 OnCreate I have a ProgressBar initialized when my view is still in the load process. OnCreate我的视图仍在加载过程中时,我已经初始化了一个ProgressBar。 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. 我确实通过对progressBar变量执行toString()来确保其实例完全相同,并且完全相同。 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. 另外,我这里没有任何onClicks,加载内容时只需要消失即可。

I watched this tutorial and it works for the guy there. 我看了本教程 ,它对那里的人有用。

Method that's setting the visibility to GONE 将可见性设置为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 我的onCreate方法创建变量

    @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 onStart

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

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 如果运行asynctask,那么我从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! 不要onStart() 充气 ,它将起作用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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