progressDialog in wrong tab

天涯浪子 提交于 2019-12-11 12:37:40

问题


I have 4 tabs in my application. One of them is for json parsing with progress dialog. But i see progres dialog at another tab.

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_parsing, container, false);
    lv = (ListView)view.findViewById(R.id.list);
    progressDialog = ProgressDialog.show(lv.getContext(), null, getResources().getString(R.string.connecting), true, true);
    MyTask myTask = new MyTask(FragmentParsing.this);
    myTask.execute(url);

    return view;
}

    @Override
public void onTaskComplete(ArrayList<String> array) {
    if(array != null) {
        //adapter.clear();
        arrayList.addAll(array);
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
        lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
    else
        Toast.makeText(getView().getContext(), getResources().getString(R.string.fuck), Toast.LENGTH_SHORT).show();
    if (progressDialog != null && progressDialog.isShowing()) {
        progressDialog.dismiss();
    }
}

And i want to see icons to left of text. Not above.

Code from MainActivity

private void setUI(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null){
            toolbar.setTitleTextColor(getResources().getColor(R.color.white));
            setSupportActionBar(toolbar);
        }
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_list_white_18dp).setText(R.string.listtab));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_camera_alt_white_18dp).setText(R.string.scalingtab));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_web_asset_white_18dp).setText(R.string.parsingtab));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_my_location_white_18dp).setText(R.string.maptab));

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        TabAdapter tabAdapter = new TabAdapter(fragmentManager, 4);
        viewPager.setAdapter(tabAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

回答1:


Its basically android pager workflow. It always creates one adjacent page view as soon as you swipe. You can have logger and check. If I'm not not wrong your fragment pager, you start your progress bar on onResume callback of your fragment.




回答2:


First:

public class MyApp extends Application { 
      public Boolean isLoaded = false; 
      public Boolean getIsLoaded(){  
           return isLoaded; 
      } 

     public void setIsLoaded(Boolean isLoaded){ 
       this.isLoaded = isLoaded;
     } 
}

And in your fragment side, you can perform this:

Boolean status;
status = ((MyApp) getActivity().getApplication()).getIsLoaded();
if(!status){
 //show your dialog
 //get your data
 ((MyApp) getActivity().getApplication()).setIsLoaded(true);
}

I hope you can take reference from the above code.




回答3:


Progress dialog is an independent window. It will show until discarded. If the context which created it is nullified, it will throw exception of leaked window. In your fragment's onPause, check for nullability of progress dialog and dismiss it. You can also check whether the task is still running if you want to show window again in onResume, provided user navigated back to this fragment. In any case, if you want progress to only be shown in specific fragment while loading views, you might want to consider using ProgressBar instead and set its visibility accordingly.




回答4:


why don't you use this override method of fragment it load data only once and for each fragment when it is open.

 @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {} 

as

 @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && !isApiCalled) {
          //call the api and load data
            isApiCalled = true;
        }
    }


来源:https://stackoverflow.com/questions/37138213/progressdialog-in-wrong-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!