Created
July 26, 2016 12:52
-
-
Save rafayali/19b471f66e764aa850586e5a18f99d6b to your computer and use it in GitHub Desktop.
Base recyclerview adapter class containing a progressbar item by default.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.got2goto.android.components; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ProgressBar; | |
import com.got2goto.android.BaseApplication; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Abstract RecyclerView adapter class containing ProgressBar item and other helper functions. | |
* | |
* Created by Rafay Ali on 7/18/2016. | |
*/ | |
public abstract class AbstractRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ | |
public static final int ITEM_TYPE_PROGRESS_BAR = 0; | |
protected boolean isProgressBarVisible = false; | |
protected ProgressBar progressBar; | |
protected Context context; | |
protected List<Object> itemsList = new ArrayList<>(); | |
public AbstractRecyclerViewAdapter(Context context){ | |
this.context = context; | |
progressBar = new ProgressBar(this.context); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
if (itemsList.get(position) instanceof ProgressBar) | |
return ITEM_TYPE_PROGRESS_BAR; | |
return -1; | |
} | |
@Override | |
public int getItemCount() { | |
return itemsList.size(); | |
} | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
switch (viewType){ | |
case ITEM_TYPE_PROGRESS_BAR: | |
View progressBarView = ((LayoutInflater) BaseApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(getProgressBarLayoutResId(), parent, false); | |
return new ProgressBarViewHolder(progressBarView); | |
} | |
return null; | |
} | |
public void addItem(Object item){ | |
itemsList.add(item); | |
} | |
public void addAllItems(List itemsList){ | |
this.itemsList.addAll(itemsList); | |
} | |
public void removeItem(Object item){ | |
itemsList.remove(item); | |
} | |
public void removeItemAtPosition(int position){ | |
itemsList.remove(position); | |
} | |
public void removeAllItems(){ | |
itemsList.clear(); | |
} | |
public void showProgressBar(){ | |
if (!isProgressBarVisible) | |
itemsList.add(progressBar); | |
isProgressBarVisible = true; | |
} | |
public void hideProgressBar(){ | |
if (isProgressBarVisible) | |
itemsList.remove(progressBar); | |
isProgressBarVisible = false; | |
} | |
public abstract int getProgressBarLayoutResId(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment