Refreshing a view inside a fragment

后端 未结 5 813
太阳男子
太阳男子 2020-12-23 22:20

I have searched the numerous questions that look like this one, but haven\'t found my answer in any of them.

I have an activity that has 3 tabs accessible through th

相关标签:
5条回答
  • 2020-12-23 22:52

    I had a similar (although not identical) problem that I could solve in the following way:

    In the fragment I added

    public void invalidate() {
        myView.post(new Runnable() {
            @Override
            public void run() {
                myView.invalidate();
            }
        });
    }
    

    and I called this method from the activity when I wanted to refresh the view myView of the fragment. The use of post() ensures that the view is only invalidated when it is ready to be drawn.

    0 讨论(0)
  • 2020-12-23 23:06

    I've found a workaround to refresh the view inside my fragment. I recreated (new CustomView) the view every time the database has been updated. After that I call setContentView(CustomView view). It looks more like a hack, but it works and nothing else that I tried does.

    Although my problem was not actually solved, I gave the reward to Alex Lockwood. His advice on Loaders made my application better and it caused me to keep looking for a solution that I eventually found.

    0 讨论(0)
  • 2020-12-23 23:06

    I had the same issue. My solution was detach fragment and attach it again.

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Fragment f = getFragment(action);
            if(forceUpdate)
            {
                fragmentTransaction.detach(f);
                fragmentTransaction.attach(f);
            }
            fragmentTransaction.replace(R.id.mainFragment, f);
            fragmentTransaction.commit();
            currentAction = action;
    
    0 讨论(0)
  • 2020-12-23 23:09

    The fastest solution working for me:

     @Override
        public void onPause() {
            super.onPause();
            if (isRemoving() && fragmentView != null) {
                ((ViewGroup) fragmentView).removeAllViews();
            }
        }
    
    0 讨论(0)
  • 2020-12-23 23:11
    1. Don't try to call onCreateView() yourself... it's a lifecycle method and should be called only by the framework.

    2. Fragments are re-usable UI components. They have their own lifecycle, display their own view, and define their own behavior. You usually don't need to have your Activity mess around with the internal workings of a Fragment, as the Fragment's behavior should be self-contained and independent of any particular Activity.

      That said, I think the best solution is to have each of your Fragments implement the LoaderManager.LoaderCallbacks<D> interface. Each Fragment will initialize a Loader (i.e. a CursorLoader if you are using a ContentProvider backed by an SQLite database), and that Loader will be in charge of (1) loading the data on a background thread, and (2) listening for content changes that are made to the data source, and delivering new data to onLoadFinished() whenever a content change occurs.

      This solution is better than your current solution because it is entirely event-driven. You only need to refresh the view when data is delivered to onLoadFinished() (as opposed to having to manually check to see if the data source has been changed each time you click on a new tab).

    3. If you are lazy and just want a quick solution, you might be able to get away with refreshing the view in your Fragment's onResume() method too.

    0 讨论(0)
提交回复
热议问题