Created
April 19, 2018 06:08
-
-
Save iamdeveloper-lopez/d0b4077eefd462022f66202c5f460923 to your computer and use it in GitHub Desktop.
Easy implementation of RecyclerView and EmptyView
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.LayoutRes; | |
import android.support.annotation.Nullable; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.AttributeSet; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.FrameLayout; | |
import java.util.Objects; | |
public class CustomRecyclerView extends FrameLayout { | |
Context context; | |
private RecyclerView recyclerView; | |
@Nullable | |
private View emptyView; | |
public CustomRecyclerView(Context context) { | |
super(context); | |
init(context); | |
} | |
public CustomRecyclerView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context); | |
} | |
public CustomRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context); | |
} | |
public CustomRecyclerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(context); | |
} | |
private void init(Context context) { | |
RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT); | |
recyclerView = new RecyclerView(context); | |
recyclerView.setLayoutParams(layoutParams); | |
addView(recyclerView); | |
} | |
public void setLayoutManager(RecyclerView.LayoutManager layoutManager) { | |
recyclerView.setLayoutManager(layoutManager); | |
} | |
public void setAdapter(RecyclerView.Adapter adapter) { | |
recyclerView.setAdapter(adapter); | |
} | |
public void setHasFixedSize(boolean fixedSize) { | |
recyclerView.setHasFixedSize(fixedSize); | |
} | |
public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration) { | |
recyclerView.addItemDecoration(itemDecoration); | |
} | |
public void addOnScrollListener(RecyclerView.OnScrollListener onScrollListener) { | |
recyclerView.addOnScrollListener(onScrollListener); | |
} | |
public void setEmptyView(View emptyView) { | |
this.emptyView = Objects.requireNonNull(emptyView); | |
addView(this.emptyView); | |
} | |
public void setEmptyView(@LayoutRes int layout) { | |
this.emptyView = LayoutInflater.from(getContext()).inflate(layout, this, false); | |
addView(this.emptyView); | |
} | |
public void hideEmptyView() { | |
if (emptyView != null) emptyView.setVisibility(GONE); | |
} | |
public void showEmptyView() { | |
if (emptyView != null) emptyView.setVisibility(VISIBLE); | |
} | |
void checkIfEmpty() { | |
if (emptyView != null && recyclerView.getAdapter() != null) { | |
final boolean emptyViewVisible = recyclerView.getAdapter().getItemCount() == 0; | |
emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE); | |
recyclerView.setVisibility(emptyViewVisible ? GONE : VISIBLE); | |
} | |
} | |
public void check() { | |
checkIfEmpty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment