Created
August 13, 2017 20:34
-
-
Save Plumillon/2ea3ce6ebff63bd837ee0ecaf2ba5d0b to your computer and use it in GitHub Desktop.
RecyclerView with status which will show and hide corresponding views
This file contains 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
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import java.util.HashMap; | |
import java.util.Map; | |
import static com.plumillonforge.android.picniphy.views.StatusRecyclerView.Status.LOADING; | |
/** | |
* Created by Plumillon Forge. | |
*/ | |
public class StatusRecyclerView extends RecyclerView { | |
public enum Status { | |
IDLE, | |
LOADING, | |
EMPTY, | |
MORE | |
} | |
private Map<Status, View> views = new HashMap<>(); | |
private Status status = LOADING; | |
@NonNull | |
final AdapterDataObserver observer = new AdapterDataObserver() { | |
@Override | |
public void onChanged() { | |
super.onChanged(); | |
check(); | |
} | |
@Override | |
public void onItemRangeInserted(int positionStart, int itemCount) { | |
super.onItemRangeInserted(positionStart, itemCount); | |
check(); | |
} | |
@Override | |
public void onItemRangeRemoved(int positionStart, int itemCount) { | |
super.onItemRangeRemoved(positionStart, itemCount); | |
check(); | |
} | |
}; | |
public StatusRecyclerView(Context context) { | |
super(context); | |
} | |
public StatusRecyclerView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public StatusRecyclerView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
public void check() { | |
for (Status status : Status.values()) { | |
checkIf(status); | |
} | |
} | |
private void checkIf(Status status) { | |
View view = views.get(status); | |
if (view != null) { | |
view.setVisibility(GONE); | |
if (this.status == status) { | |
view.setVisibility(VISIBLE); | |
} | |
} | |
} | |
@Override | |
public void setAdapter(@Nullable Adapter adapter) { | |
replaceAdapter(adapter); | |
super.setAdapter(adapter); | |
} | |
@Override | |
public void swapAdapter(Adapter adapter, boolean removeAndRecycleExistingViews) { | |
replaceAdapter(adapter); | |
super.swapAdapter(adapter, removeAndRecycleExistingViews); | |
} | |
private void replaceAdapter(@Nullable Adapter adapter) { | |
final Adapter oldAdapter = getAdapter(); | |
if (oldAdapter != null) { | |
oldAdapter.unregisterAdapterDataObserver(observer); | |
} | |
if (adapter != null) { | |
adapter.registerAdapterDataObserver(observer); | |
} | |
} | |
public void setView(Status status, @Nullable View view) { | |
this.views.put(status, view); | |
check(); | |
} | |
@Nullable | |
public View getView(Status status) { | |
return views.get(status); | |
} | |
public void setStatus(Status status) { | |
this.status = status; | |
check(); | |
} | |
public Status getStatus() { | |
return status; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment