Android Open Source - mobile2-android Load More Adapter






From Project

Back to project page mobile2-android.

License

The source code is released under:

Apache License

If you think the Android project mobile2-android 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 com.ecollege.android.adapter;
//w  w  w  . j av a2s  .c o m
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;

import com.ecollege.android.R;


public class LoadMoreAdapter extends BaseAdapter {

  public static final long LOAD_MORE_ITEM_ID = Long.MAX_VALUE - 1;//minus one in case max_value is used
  public static final long LOADING_ITEM_ID = Long.MAX_VALUE - 2;//minus one in case max_value is used
  private boolean canLoadMore = true;
  private boolean isLoadingMore = false;
  private ListAdapter baseAdapter;
  private Context context;
  private SimpleObserver baseObserver;
  
  public LoadMoreAdapter(Context context, ListAdapter baseAdapter, boolean canLoadMore) {
    this.baseAdapter = baseAdapter;
    this.canLoadMore = canLoadMore;
    this.context = context;
    baseObserver = new SimpleObserver(this);
    if (baseAdapter != null) {
      baseAdapter.registerDataSetObserver(baseObserver);
    }
  }
    
  public void setIsLoadingMore(boolean isLoadingMore) {
    this.isLoadingMore = isLoadingMore;
    this.notifyDataSetChanged();
  }
  
  public void update(ListAdapter baseAdapter, boolean canLoadMore) {
    assert baseAdapter != null;
    this.isLoadingMore = false;
    if (this.baseAdapter != null) {
      this.baseAdapter.unregisterDataSetObserver(baseObserver);
    }
    this.baseAdapter = baseAdapter;
    this.baseAdapter.registerDataSetObserver(baseObserver);
    this.canLoadMore = canLoadMore;
    this.notifyDataSetChanged();
  }

  public int getCount() {
    if (canLoadMore || isLoadingMore) return baseAdapter.getCount() + 1;
    return baseAdapter.getCount();
  }

  public Object getItem(int position) {
    if (position < baseAdapter.getCount()) {
      return baseAdapter.getItem(position);
    } else {
      return this;
    }
  }

  public long getItemId(int position) {
    if (position < baseAdapter.getCount()) {
      return baseAdapter.getItemId(position);
    } else {
      if (isLoadingMore) return LOADING_ITEM_ID;
      return LOAD_MORE_ITEM_ID;
    }
  }

  protected int loadMoreViewType() {
    return (baseAdapter.getViewTypeCount() - 1) + 1;
  }
  
  protected int loadingViewType() {
    return (baseAdapter.getViewTypeCount() - 1) + 2;
  }  
  
  public int getItemViewType(int position) {
    if (position < baseAdapter.getCount()) {
      return baseAdapter.getItemViewType(position);
    } else {
      if (isLoadingMore) return loadingViewType();
      return loadMoreViewType();
    }
  }
  
  public View getLoadMoreView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView =  ((Activity)context).getLayoutInflater().inflate(R.layout.load_more_item, null);
        }
        return convertView;    
  }
  
  public View getLoadingView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView =  ((Activity)context).getLayoutInflater().inflate(R.layout.loading_item, null);
        }
        return convertView;
  }  

  public View getView(int position, View convertView, ViewGroup parent) {
    if (position < baseAdapter.getCount()) {
      return baseAdapter.getView(position,convertView,parent);
    } else {
      if (isLoadingMore) {
        return getLoadingView(position, convertView, parent);
      }
      return getLoadMoreView(position, convertView, parent);
    }
  }

  public int getViewTypeCount() {
    return baseAdapter.getViewTypeCount() + 2;
  }  
  
  public void registerDataSetObserver(DataSetObserver observer) {
    super.registerDataSetObserver(observer);
  }

  public void unregisterDataSetObserver(DataSetObserver observer) {
    super.unregisterDataSetObserver(observer);
  }

  public boolean hasStableIds() {
    return baseAdapter.hasStableIds();
  }

  public boolean isEmpty() {
    return baseAdapter.isEmpty();
  }

  public boolean areAllItemsEnabled() {
    if (isLoadingMore) return false;
    return baseAdapter.areAllItemsEnabled();
  }

  public boolean isEnabled(int position) {
    if (position < baseAdapter.getCount()) {
      return baseAdapter.isEnabled(position);
    } else {
      if (isLoadingMore) return false;
      return true;
    }
  }
  
  private class SimpleObserver extends DataSetObserver {
    
    private LoadMoreAdapter adapter;
    
    public SimpleObserver(LoadMoreAdapter adapter) {
      this.adapter = adapter;
    }
    
    @Override
    public void onChanged() {
      adapter.notifyDataSetChanged();
    }

    @Override
    public void onInvalidated() {
      adapter.notifyDataSetInvalidated();
    }
    
  }

}




Java Source Code List

com.ecollege.android.AnnouncementActivity.java
com.ecollege.android.CourseActivity.java
com.ecollege.android.CourseAnnouncementsActivity.java
com.ecollege.android.CourseDiscussionsActivity.java
com.ecollege.android.CourseGradebookActivity.java
com.ecollege.android.CoursePeopleActivity.java
com.ecollege.android.CourseThreadActivity.java
com.ecollege.android.CoursesActivity.java
com.ecollege.android.DiscussionsActivity.java
com.ecollege.android.DropboxMessageActivity.java
com.ecollege.android.ECollegeApplication.java
com.ecollege.android.GradeActivity.java
com.ecollege.android.HomeActivity.java
com.ecollege.android.HtmlContentActivity.java
com.ecollege.android.LoginActivity.java
com.ecollege.android.MainActivity.java
com.ecollege.android.PersonActivity.java
com.ecollege.android.ProfileActivity.java
com.ecollege.android.SingleSignonActivity.java
com.ecollege.android.SplashActivity.java
com.ecollege.android.UserDiscussionActivity.java
com.ecollege.android.UserResponseActivity.java
com.ecollege.android.UserTopicActivity.java
com.ecollege.android.activities.ECollegeActivityHelper.java
com.ecollege.android.activities.ECollegeActivity.java
com.ecollege.android.activities.ECollegeDefaultActivity.java
com.ecollege.android.activities.ECollegeListActivity.java
com.ecollege.android.activities.ECollegeTabActivity.java
com.ecollege.android.adapter.ActivityFeedAdapter.java
com.ecollege.android.adapter.GroupedAdapter.java
com.ecollege.android.adapter.LoadMoreAdapter.java
com.ecollege.android.adapter.ParentAdapterObserver.java
com.ecollege.android.adapter.ResponseAdapter.java
com.ecollege.android.adapter.TopicsAdapter.java
com.ecollege.android.adapter.UberAdapter.java
com.ecollege.android.adapter.UberItem.java
com.ecollege.android.adapter.UpcomingEventsAdapter.java
com.ecollege.android.adapter.WaitingForApiAdapter.java
com.ecollege.android.errors.ECollegeAlertException.java
com.ecollege.android.errors.ECollegeException.java
com.ecollege.android.errors.ECollegePromptException.java
com.ecollege.android.errors.ECollegePromptRetryException.java
com.ecollege.android.tasks.ECollegeAsyncTask.java
com.ecollege.android.tasks.ServiceCallTask.java
com.ecollege.android.tasks.TaskPostProcessor.java
com.ecollege.android.util.CacheConfiguration.java
com.ecollege.android.util.DateTimeUtil.java
com.ecollege.android.util.FileCacheManager.java
com.ecollege.android.util.VolatileCacheManager.java
com.ecollege.android.view.HeaderView.java
com.ecollege.android.view.helpers.ResponseCountViewHelper.java