Load ListView inside TabHost's tab smoothly - android

I am trying to load a listview which gets data from a server inside a Tabhost's Tab. But everytime i switch to another tab and come back there is always a delay generating the ListView. Once the ListView is generated everything runs smoothly, bubt i would love to load the listview smoothly as well with no visible delay.
I have a method that i created, called loadStatuses(), which is called in the following places.
OnCreate and OnResumed.
private void loadStatuses() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
adapter = new MyCustomAdapter(getActivity().getApplicationContext());
adapter.setImageKey("Status");
mListView.setAdapter(adapter);
}
});
}
As you can see i am basically just re-creating the listview, is there away to make this much more smoother with no delay, good on memory and performance?

you do not need to call your loadStatuses at onRsumed(). just calling it at onCreate is sufficient.
I am trying to load a listview which gets data from a server inside a
Tabhost's Tab. But everytime i switch to another tab and come back
there is always a delay generating the ListView. Once the ListView is
generated everything runs smoothly, bubt i would love to load the
listview smoothly as well with no visible delay.
In order to achieve that you can use FragmentStatePageAdapter and setOffscreenPageLimit(Number of tabs); to not loading your content again. you can also use caches to prevent from downloading again and again when user switch the tabs.
Hi, the reason i am doing it this way is because instead of Using
setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) i am using the
navigation drawer, and using the tabhost to be able to use both the
navigation drawer AND tabs.
For this reason i am using
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD)
instead and inside the MainActivity, which in this case is "Home" in
the navigation drawer i have the TabHost.
Can i do this without using the actionbar?
Yes, you can use PagerSlidingTabStrip library. I answered step by step how to run it with:
PagerSlidingTabStrip implement guide
Also note ActionBar.Tab is Deprecated since API level 21

I think you should use Swipable Tab view because It is a standard way . You can follow following url :
http://developer.android.com/training/implementing-navigation/lateral.html
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
and load data from server in data model and then send it to adapter , so it will not load repeated when changing the tab.
Once load you can use without interruption.
If you need any further help, plz let me know.

Related

RecyclerView Adapter cannot update view immediately when adding new item by clicking the button

I have one Activity which contains viewpager inside and each viewpager tab contains one fragment . only one of these fragment has one recyclerview (which has inside cardView)and of course one adapter for the recyclerview . by clicking on floating action button which is in Main Activtiy layout another Activity open that contains two edit texts one for device id and one for device name and one button called Add .by clicking the add button it should add the device to the database and update adapter using notifyItemInserted(position) .... the problem here it add the device to the database immediately but the adapter not update the view immediately . it update after i scroll through the app using viewpager tabs or when i start the main activity from the beginning .... there is no error in the code and i have search for answer but i couldn't find anything to solve this issue ..
anyone face like this problem please advice
thanks
Try swapadapter() method for the same.
void swapAdapter (Adapter adapter,
boolean removeAndRecycleExistingViews)
Swaps the current adapter with the provided one. It is similar to setAdapter(Adapter) but assumes existing adapter and the new adapter uses the same RecyclerView.ViewHolder and does not clear the RecycledViewPool.
Have you tried to call your SetUpRecyclerView in onActivityResult() which I asume your are using. That is what I use when adding elements to My RecyclerViews.
When using FragmentPagerAdapter, Your pager keeps two tabs in memory, that's why you have to scroll for the changes to take effect. But for me calling my SetupRecyclerView works just fine.

AsyncTask throbber shows on previous tab of ViewPager

I'm making an app with 4 tabs, the third one is ListView with data parsed from xml by AsyncTask.
I've got a problem with throbber (circle progress bar) which shows on a previous tab and I can't find solution of it.
Does anybody knows how to prevent this kind of pre-loading when AsyncTask starts on previous tab, not on ListView (third) tab?
There is no way to prevent pre-loading fragments in ViewPager.
But you can use some external libraries to take that result.
For example, ViewPagerExtensions or ViewPagerIndicator.

Android : Which method is better for navigational Listview (fragment or Listactivity)

I am a novice in developing android app ! I am working with Android Studio!
Want to implement a tab-panel in which some of the tabs (as this) will contain listview to hold some data or image from a json Webservice! While the data comes from internet connectivity so there is performance is a issue for me!
I have got two types of way to implement tabs in android! One is listactivity & other is fragment! I just want to know which of these two way is efficient for implementing tabs on android??
A listActivity is an activity, and you cannot bind a different activity to each tab-panel, but you can put a different fragment on each panel (or use a single container, and just replace the fragments there with transactions).
You can also use another fragment to display the tab-interface at the top, which remains constant as the contents below changes. Something you couldn't do if you were switching activities.

Android veiwpager with fragments view flow issues

Im relatively new to android so im trying my hand at what i though would be an easy-ish app but I've ran into an issue to do with view/activity flow that i cant get an understanding on.
i have a fragmentActivity that uses a Viewpager to create tabs, each of those tabs is its own fragment class, thats all fine and working, but now i need to have one of the tabs display a list, when selected it takes you to another "view", my problem is how to create the first list and then how to handle tha clicking of an item in that list to take you to the new view so that the tabs stay in place and the back button doesn't exit the app.
currently ive swapped out the fragment with a list fragment that uses an arrayAdapter to build itself, this has worked as far as the list goes but i cant for the life of me figure out how to utelise its onclick() method to move on the the next screen, without as i said losing the tabs or having the back button simply exit.
so im not sure if A, the list fragment is the way to go, or B if it is how to move on to a new screen correctly
i can post code if needed but its a very general implementation of the classes mentioned so im not sure code will help
The callback you want to handle a click on an item in the list is onListItemClick.
As far as presenting a new screen, you can use a FragmentTransaction to replace your fragment with a new one, which gets a little hairy if you're doing this inside of a ViewPager. The code would look something like this:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.some_containging_view, new SomeFragment())
.addToBackStack(null).commit();
Otherwise, you could simply launch a new Activity, which is simpler, but will mean that the tabs won't be present on the new screen unless you duplicate them there.
An elegant solution was finally found here, it uses a wrapper fragment around my list fragment so the pager is just concerned with the wrapper, meanwhile within the wrapper i can perform fragment transactions to my hearts desire, one small note the linked code uses getChildFragmentManager() that needs to be changed to regular fragmentmanager() from support.app for the backstack stuff to work.

Destroy Pages on ViewPager and FragmentStatePagerAdapter

We are trying to create an Android application that is based on ViewPager that receive during runtime instructions to add and remove pages. Almost like a tabbed browser experience, that you can remove the current tab, or remove a specific tab.
Following Google documentation we use FragmentStatePagerAdapter, that is intended to be used in situations when there are a large number of pages, working more like a list view.
But when we try to:
- Remove a page that is not on the screen
- and create a new fragment object from the same class
- and on the same position of the page removed
We noticed that the Android platform recover the dead page and display that to the user. The new object that we just created simply does not run “onCreate”, “onCreateView” or “onActivityCreated”.
We are looking to ways to fix this issue forcing the platform to use our new fragment object from the same class. Any ideas?
We found out that if we destroy the current page the platform indeed destroyed the page and created a new object from the same class. Here is a small example replicating the problem and this behavior.
Source: http://dl.dropbox.com/u/8333012/SimpleAdapter/SimplePager.zip
Video: http://www.youtube.com/watch?v=-oaXXwU8PSI&hd=1
From this project when you touch the TextView on the first page it was designed to remove the second page (which is green) to a new blue page. You will see that even doing that from the first page the second page remains green. But when you press the back Android button on the second page (green) and touch the TextView the second page created will be the right blue color.
When you are dealing with ListView's and change the underlying data of your adapter you call notifyDataSetChanged() and any View reflecting the data set will refresh itself. Thats the way you should go with fragment pager adapter too.
In your case you do not notify the adapter. However, in case of FragmentPagerAdapter/FragmentStatePagerAdapter it does not make a difference because those adapters ignore notifyDataSetChanged() "by default". To make it work override the getItemPosition() in your adapter implementation
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
And as already said, after you add/remove fragments call (in your showOtherPage())
mAdapter.notifyDataSetChanged();

Categories

Resources