Android Open Source - SeeKampf Rest List Fragment






From Project

Back to project page SeeKampf.

License

The source code is released under:

GNU General Public License

If you think the Android project SeeKampf listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.avedo.seekampf.fragments;
/*  ww  w  . j  a v a  2 s .com*/
import net.avedo.seekampf.R;
import net.avedo.seekampf.R.layout;
import net.avedo.seekampf.core.CustomAdapter;
import net.avedo.seekampf.core.VolleyActivity;
import net.avedo.seekampf.models.BaseModel;
import net.avedo.seekampf.utils.AuthGsonRequest;
import net.avedo.seekampf.utils.VolleyErrorHelper;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import android.app.ListFragment;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

public abstract class RestListFragment<ServiceObject extends BaseModel> extends ListFragment {
  public static final int MAXIMUM_RETRIES = 3;
  public static final int DEFAULT_LIMIT = 25;
  
  protected SharedPreferences settings;
  protected Resources res;
  
  protected AuthGsonRequest<ServiceObject[]> request;
  protected CustomAdapter<ServiceObject> adapter;
  protected boolean isFetching = false;
  protected boolean hasMoreEntries = true;
  protected int retryCount = 0;

    protected int limit = DEFAULT_LIMIT;
  protected int offset = 0;

  @Override
  public void onActivityCreated(Bundle paramBundle) {
    // Fetch the application preferences handle ...
    this.settings = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
    
    // ... and the application resources.
    this.res = getResources();
    
    super.onActivityCreated(paramBundle);
    getListView().setDividerHeight(2);
        
        // Set on scroll listener.
        getListView().setOnScrollListener(new OnRestListScrollListener());
  }

  @Override
  public void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);
  }

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.list_fragment, container, false);
    }

  @Override
  public void onListItemClick(ListView paramListView, View paramView,
      int paramInt, long paramLong) {
    super.onListItemClick(paramListView, paramView, paramInt, paramLong);
  }
    
    @Override
    public void onStart() {
      super.onStart();
      fetchData();
    }

    @Override
    public void onDestroyView() {
      VolleyActivity.getInstance().cancelPendingRequests(fetchServiceTag());
      isFetching = false;
        super.onDestroyView();
    }
    
    protected void fetchData() {
      // Break if already fetching data or there is nothing left to load.
      if (isFetching || !hasMoreEntries) {
        return;
      }
      
      // Start fetching data ...
      isFetching = true;

    // ... and generate the service url.
    String serviceURL = fetchServiceUrl();
      
    // Setup the request object.
    request = new AuthGsonRequest<ServiceObject[]>(Request.Method.GET, 
        serviceURL + "&offset=" + offset + "&limit=" + limit,
        fetchServiceObjClass(), null, new Response.Listener<ServiceObject[]>() {
            @Override
            public void onResponse(ServiceObject[] serviceObjects) {
                      if (RestListFragment.this.getView() != null && serviceObjects != null) {
                          // Reset the retry counter, ...
                          retryCount = 0;
                          
                          // ... add the alliance to the list adapter, ...
                          if (adapter == null) {
                            fetchServiceAdapter(serviceObjects);
                            adapter.setNotifyOnChange(true);
                              getListView().setAdapter(adapter);
                          } else {
                            adapter.addAll(serviceObjects);
                          }
                          
                          // ... update offset and reset fetching status.
                          offset += serviceObjects.length;
                          isFetching = false;
                          
                          // Finally update the entries flag.
                          hasMoreEntries = !(serviceObjects.length < limit);
                      }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                    retryCount++;
                    
                    if (retryCount < MAXIMUM_RETRIES) {
                      VolleyActivity.getInstance().addToRequestQueue(request, fetchServiceTag());
                    }
                        
                    Toast.makeText(getActivity(), 
                        VolleyErrorHelper.getMessage(error, getActivity()), 
                        Toast.LENGTH_LONG).show();
            }
        });
    
    VolleyActivity.getInstance().addToRequestQueue(request, fetchServiceTag());
    }

    protected abstract void fetchServiceAdapter(ServiceObject[] serviceObjects);
    protected abstract Class<ServiceObject[]> fetchServiceObjClass();
    protected abstract String fetchServiceUrl();
    protected abstract String fetchServiceTag();

    protected class OnRestListScrollListener implements AbsListView.OnScrollListener {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
      // Do nothing here!
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
      // Calculate the last visible item ...
      int lastVisibleItem = firstVisibleItem + visibleItemCount;
      
      // ... and fetch new ones if necessary.
      if (lastVisibleItem == totalItemCount) {
        fetchData();
      }
    }
    }
}




Java Source Code List

net.avedo.seekampf.BuildConfig.java
net.avedo.seekampf.core.ChangeLog.java
net.avedo.seekampf.core.CustomAdapter.java
net.avedo.seekampf.core.MainActivity.java
net.avedo.seekampf.core.OceanView.java
net.avedo.seekampf.core.OverScrollerCompat.java
net.avedo.seekampf.core.RestDetailsActivity.java
net.avedo.seekampf.core.ScaleGestureDetectorCompat.java
net.avedo.seekampf.core.VolleyActivity.java
net.avedo.seekampf.core.Zoomer.java
net.avedo.seekampf.fragments.AboutFragment.java
net.avedo.seekampf.fragments.AllianceListFragment.java
net.avedo.seekampf.fragments.AuctionListFragment.java
net.avedo.seekampf.fragments.HomeDetailsFragment.java
net.avedo.seekampf.fragments.HomeFragment.java
net.avedo.seekampf.fragments.IslandDetailsFragment.java
net.avedo.seekampf.fragments.IslandListFragment.java
net.avedo.seekampf.fragments.MessageDetailsFragment.java
net.avedo.seekampf.fragments.MessageListFragment.java
net.avedo.seekampf.fragments.OceanFragment.java
net.avedo.seekampf.fragments.PlayerListFragment.java
net.avedo.seekampf.fragments.RestDetailsFragment.java
net.avedo.seekampf.fragments.RestListFragment.java
net.avedo.seekampf.fragments.SettingsFragment.java
net.avedo.seekampf.models.Alliance.java
net.avedo.seekampf.models.Auction.java
net.avedo.seekampf.models.BaseModel.java
net.avedo.seekampf.models.Island.java
net.avedo.seekampf.models.Message.java
net.avedo.seekampf.models.Player.java
net.avedo.seekampf.utils.AuthGsonRequest.java
net.avedo.seekampf.utils.Constants.java
net.avedo.seekampf.utils.Interfaces.java
net.avedo.seekampf.utils.VolleyErrorHelper.java