Reputation: 131
I have an activity named LibraryDemo which is having 3 tabs.
LibraryDemo.java onCreate Method.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library_demo);
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.accent_material_light));
toolbar = (Toolbar) findViewById(R.id.tool_bar_library_demo);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
Here is Songs.java code
public class SongsTab extends Fragment {
ListView lv;
Cursor cu;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.activity_songs_tab, container, false);
lv=(ListView)v.findViewById(R.id.songs_tab_listView);
cu=SongManager.populateQueries(v.getContext());
SongListViewAdapter adapt=new SongListViewAdapter(v.getContext(),cu);
lv.setAdapter(adapt);
return v;
}
}
As you can see I am having an adapter class which takes in a cursor argument and a Songmanager class which query for all the audio present in the device and put the details into a cursor and returns it. Problem is that in LibraryDemo activity when i swipe into the Songs tab it takes some time to load (which obviously is time taken to retrieve all audio details and loading them into ListView). As per my knowledge i can use background task to achieve performance gain. But I don't have clear Idea where should I use the background services. Mainly there are two process which are taking some time- retrieving all audio detail and loading them into the ListView. How should i manage these process to optimize the performance? Thank you.
Edited :
After going throught the answers and what I have googled , It seems using cursor is not a good thing to pass as an argument to an Adapter. Is it right? BTW i have tried to include AsyncTask but didn't see any difference. here is my code
public class SongsTab extends Fragment {
ListView lv;
Cursor cu;
View v;
SongListViewAdapter adapt;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_songs_tab, container, false);
lv = (ListView) v.findViewById(R.id.songs_tab_listView);
new MyTask().execute();
return v;
}
class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
cu = SongManager.populateQueries(v.getContext());
adapt = new SongListViewAdapter(v.getContext(), cu);
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
lv.setAdapter(adapt);
}
}
}
Upvotes: 1
Views: 91
Reputation: 4202
Background task can solve ui freezing problem - long time operation no longer happen in ui thread. Even so, you still have to wait for the operation to complete before you can show the real stuff rather than a "loading" placeholder.
Another idea is to do prefetching - you probably know what data you need to display in advance. E.g., when your app starts, or before you are going to that screen - You should prefetch the data and cache it somewhere.
So while user is still in few screen earlier, you are already prefetching data. Later, when user enters the screen, data should be already there. Or, at least, data fetching already started earlier.
Upvotes: 1
Reputation: 2881
There is many many ways to achieve what you want to do but basically, I'd use an AsyncTask
(docs here). And then, in the doInBackground
method of this Task, I'd populate your adapters and call the notifyDataSetChanged
of them.
To have a better overview of all possibilities, you have with the AsyncTask
, look at this short YouTube video (15min and it worth it). They're basically showing what you want to do but with a bit simpler model (just String
and ArrayAdapter
)
Then, you can have more complicated workflows with an empty view and/or a loading view (I appreciate the simplicity of this one and I'm even working with it in production, it makes your workflow clearer)
Upvotes: 1