Skip to content

Instantly share code, notes, and snippets.

@nesquena
Created May 14, 2015 06:09
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save nesquena/a988aac278cff59a9a69 to your computer and use it in GitHub Desktop.
Save nesquena/a988aac278cff59a9a69 to your computer and use it in GitHub Desktop.
Displaying Progress with Custom Adapter
/**
* A child class shall subclass this Adapter and
* implement method getDataRow(int position, View convertView, ViewGroup parent),
* which supplies a View present data in a ListRow.
*
* This parent Adapter takes care of displaying ProgressBar in a row or
* indicating that it has reached the last row.
*
*/
public abstract class GenericAdapter<T> extends BaseAdapter {
// the main data list to save loaded data
protected List<T> dataList;
protected Activity mActivity;
// the serverListSize is the total number of items on the server side,
// which should be returned from the web request results
protected int serverListSize = -1;
// Two view types which will be used to determine whether a row should be displaying
// data or a Progressbar
public static final int VIEW_TYPE_LOADING = 0;
public static final int VIEW_TYPE_ACTIVITY = 1;
public GenericAdapter(Activity activity, List<T> list) {
mActivity = activity;
dataList = list;
}
public void setServerListSize(int serverListSize){
this.serverListSize = serverListSize;
}
/**
* disable click events on indicating rows
*/
@Override
public boolean isEnabled(int position) {
return getItemViewType(position) == VIEW_TYPE_ACTIVITY;
}
/**
* One type is normal data row, the other type is Progressbar
*/
@Override
public int getViewTypeCount() {
return 2;
}
/**
* the size of the List plus one, the one is the last row, which displays a Progressbar
*/
@Override
public int getCount() {
return dataList.size() + 1;
}
/**
* return the type of the row,
* the last row indicates the user that the ListView is loading more data
*/
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return (position >= dataList.size()) ? VIEW_TYPE_LOADING
: VIEW_TYPE_ACTIVITY;
}
@Override
public T getItem(int position) {
// TODO Auto-generated method stub
return (getItemViewType(position) == VIEW_TYPE_ACTIVITY) ? dataList
.get(position) : null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return (getItemViewType(position) == VIEW_TYPE_ACTIVITY) ? position
: -1;
}
/**
* returns the correct view
*/
@Override
public View getView(int position, View convertView, ViewGroup parent){
if (getItemViewType(position) == VIEW_TYPE_LOADING) {
// display the last row
return getFooterView(position, convertView, parent);
}
View dataRow = convertView;
dataRow = getDataRow(position, convertView, parent);
return dataRow;
};
/**
* A subclass should override this method to supply the data row.
* @param position
* @param convertView
* @param parent
* @return
*/
public abstract View getDataRow(int position, View convertView, ViewGroup parent);
/**
* returns a View to be displayed in the last row.
* @param position
* @param convertView
* @param parent
* @return
*/
public View getFooterView(int position, View convertView,
ViewGroup parent) {
if (position >= serverListSize && serverListSize > 0) {
// the ListView has reached the last row
TextView tvLastRow = new TextView(mActivity);
tvLastRow.setHint("Reached the last row.");
tvLastRow.setGravity(Gravity.CENTER);
return tvLastRow;
}
View row = convertView;
if (row == null) {
row = mActivity.getLayoutInflater().inflate(
R.layout.progress, parent, false);
}
return row;
}
}
@ssrishabh96
Copy link

how to set trigger?

@Ni3Narale
Copy link

is any one found hot to trigger this class...

@waitray
Copy link

waitray commented Jun 3, 2016

boolean getAllResult = false;
public void setGetAllResult(boolean getAllResult) {
    this.getAllResult = getAllResult;
}

change "position >= serverListSize && serverListSize > 0" to "if(!getAllResult)" , and u can set "getAllResult" if u get all record from server or db.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment