Solved - Android ViewPager and TabLayout are not working fast

霸气de小男生 提交于 2019-11-30 08:16:40
Jotiram Chavan

try to set

pager.setOffscreenPageLimit(no_of_fragments or pages);

Do all your task in asyctask on creation of fragments

I will suggest you to add the another method.

@Override
public void setMenuVisibility(boolean menuVisible) {
    super.setMenuVisibility(menuVisible);

    if (menuVisible) {
       Log.e("SetMenuVisibleFragmentA" , menuVisible + " ");
    }

}

public void setMenuVisibility (boolean menuVisible)

Set a hint for whether this fragment's menu should be visible. This is useful if you know that a fragment has been placed in your view hierarchy so that the user can not currently seen it, so any menu items it has should also not be shown.

Parameters

menuVisible :: The default is true, meaning the fragment's menu will be shown as usual. If false, the user will not see the menu.

Your

onActivityCreated(Bundle savedInstanceState1)"

in EquityFragment.java is weird. First of all, don't deactivate the StrictMode (because you ignore the thrown "NetworkOnMainThread" Exception which is most likely the reason for your code to be slow...)

Then look at the else Part:

 } else {
    new Thread(new Runnable() {
            @Override
            public void run() {
                // do some work here
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //   if (!isViewShown) {
                        new FetchAllData(getActivity(), 5).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        //    }
                    }
                });
            }
        }).start();
}

You start a new Thread, where you should do your network stuff. Instead of doing it, you return to the ui thread (if you do network stuff here the app will hang") in your new thread and do it on the ui thread. (I don't know what exactly FetchAllData does).

} else {
    new FetchAllData(getActivity(), 5).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

should do the same Job.

In your FetchAllData class, you do all the JSON parsing in onPostExecute. Do the parsing in doInBackground and only set the adapter in onPostExecute.

It seems that you do too much work in onPostExecute method. Try to parse JSON and set data in background and notify the change in UIthread may be workable.

  1. Move the code from OnActivityCreated() to onViewCreated().

  2. Use Gson if Possible.(It had saved valuable seconds in my project.)

  3. Post your adapters code if possible. If you are doing too many operation in getView() avoid it.

  4. In your AsyncTask, you are using UI elements. I know we can access UI components in onPostExecute(), but try ro run that code on UI thread by using runOnUIThread(). It improves performance.

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