Android : How control Volley request each listviews in viewpager?

on Saturday, September 13, 2014


My app has five tabs. These have listview which gets date from json using Volley(it was made refer to this). Also each tabs can be changed swipe(it based on this).

My app works fine but it takes long time to operate. When starting app, first tab's listview gets date. It goes on working until last tab's listview.

I thought setOffscreenPageLimit(4) might be problem. I added it to avoid loading twice(I asked this).

If possible, each listviews get dates from json when each tabs are selected like google play store.

Please teach me what is wrong if anyone knows mistakes.

Thank you watching and sorry bad English skills.



Main Activity



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayOptions(0,ActionBar.DISPLAY_SHOW_TITLE);

for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOffscreenPageLimit(4);

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){

@Override
public void onPageSelected(int position){
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2){
}
public void onPageScrollStateChanged(int arg0){
}
});
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft){
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft){
//on tab selected, show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft){
}






TabsPagerAdapter



public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new First_Fragment();
case 1:
return new Second_Fragment();
case 2:
return new Third_Fragment();
case 3:
return new Fourth_Fragment();
case 4:
return new Fifth_Fragment();
}
return null;
}
@Override
public int getCount() {
return 5;
}


}






First ~ Fifth Tabs(they are almost same code)



private static final String TAG = First_Fragment.class.getSimpleName();
private static final String url = "";
private CustomAdapter adapter;
private List<Movie> movieList = new ArrayList<Movie>();
private ProgressDialog pDialog;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first_, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new CustomAdapter(getActivity(),movieList);
setListAdapter(adapter);
getListView().setScrollingCacheEnabled(false);

pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();


// Creating volley request obj
JsonUTF8Request movieReq = new JsonUTF8Request(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();

// Parsing json
for (int i = 0; i < response.length(); i++) {
try {

JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("jacket"));
movie.setNumber(obj.getString("number"));
movie.setYear(obj.getString("release"));

// adding movie to movies array
movieList.add(movie);

} catch (JSONException e) {
e.printStackTrace();
}

}

// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();

}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}

0 comments:

Post a Comment