Skip to content

Instantly share code, notes, and snippets.

@gumil
Created May 25, 2016 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gumil/149f8ccccc77954faae55c467bc2e02e to your computer and use it in GitHub Desktop.
Save gumil/149f8ccccc77954faae55c467bc2e02e to your computer and use it in GitHub Desktop.
RecyclerView with SwipeRefreshLayout, Loading, and Error Message
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import ph.mobext.core.base.app.R;
public class RecyclerLoadingView extends FrameLayout implements
SwipeRefreshLayout.OnRefreshListener {
private static final String TAG = RecyclerLoadingView.class.getSimpleName();
private RecyclerView recyclerView;
private ProgressBar progressBar;
private TextView textView;
private SwipeRefreshLayout swipeRefresh;
private ListLoadingViewListener listener;
private LinearLayoutManager layoutManager;
private boolean isSwipeEnabled;
private RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (isSwipeEnabled) {
swipeRefresh.setEnabled(layoutManager.findFirstCompletelyVisibleItemPosition() == 0);
}
}
};
private RecyclerView.Adapter adapter;
public interface ListLoadingViewListener {
void onRefresh();
}
public RecyclerLoadingView(Context context) {
super(context);
}
public RecyclerLoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
swipeRefresh = (SwipeRefreshLayout) LayoutInflater.from(context).inflate(R.layout.view_list_loading, this, false);
addView(swipeRefresh);
recyclerView = (RecyclerView) swipeRefresh.findViewById(R.id.list_loading_recyclerview);
recyclerView.setOnScrollListener(onScrollListener);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
progressBar = (ProgressBar) swipeRefresh.findViewById(R.id.list_loading_progress);
textView = (TextView) swipeRefresh.findViewById(R.id.list_loading_text);
swipeRefresh.setOnRefreshListener(this);
}
public void showLoading() {
recyclerView.setVisibility(GONE);
progressBar.setVisibility(VISIBLE);
textView.setVisibility(GONE);
swipeRefresh.setRefreshing(false);
}
public void showList() {
recyclerView.setVisibility(VISIBLE);
progressBar.setVisibility(GONE);
textView.setVisibility(GONE);
}
public void showText(String text) {
recyclerView.setVisibility(GONE);
progressBar.setVisibility(GONE);
textView.setVisibility(VISIBLE);
textView.setText(text);
swipeRefresh.setRefreshing(false);
}
public void setAdapter(RecyclerView.Adapter adapter) {
this.adapter = adapter;
recyclerView.setAdapter(this.adapter);
showList();
swipeRefresh.setRefreshing(false);
}
public void enableSwipeToRefresh(boolean enabled) {
swipeRefresh.setEnabled(enabled);
isSwipeEnabled = enabled;
}
public void setListener(ListLoadingViewListener listener) {
this.listener = listener;
}
@Override
public void onRefresh() {
if (null != listener) {
listener.onRefresh();
}
}
public void setLayoutManager(LinearLayoutManager layoutManager) {
this.layoutManager = layoutManager;
recyclerView.setLayoutManager(layoutManager);
}
public RecyclerView.Adapter getAdapter() {
return adapter;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list_loading_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/list_loading_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:id="@+id/list_loading_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/warn_no_adapter"
android:visibility="gone"/>
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment