Is it possible to refresh the view of a Fragment

后端 未结 3 883
滥情空心
滥情空心 2020-12-31 01:12

I changed the locale of my application programmatically, like following:

Resources res = context.getResources();

 DisplayMetri         


        
相关标签:
3条回答
  • 2020-12-31 01:48

    You can simply detach and attach fragment as below

        Fragment currentFragment = getFragmentManager().findFragmentByTag("FRAGMENT");
        FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
        fragTransaction.detach(currentFragment);
        fragTransaction.attach(currentFragment);
        fragTransaction.commit();
    

    This will refresh the view and locale will change

    0 讨论(0)
  • 2020-12-31 01:48

    I finally found the solution to it! Just run your view update code in the UI thread

    Example -

     getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    user_arrayList.add(name);
                    adapter.notifyDataSetChanged();
                    tv_total_users.setText("Total Users : "+user_arrayList.size());
                }
            });
    

    Finally found this solution after trying for almost 4-5 hours!

    0 讨论(0)
  • 2020-12-31 02:01

    You could create a listener, which will be called when the locale changes. This will then remove the Fragment, and re add the Fragment. Your new locale should then be picked up.

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