Skip to content

Instantly share code, notes, and snippets.

@adfleshner
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adfleshner/57763ceeaa0d7354f86f to your computer and use it in GitHub Desktop.
Save adfleshner/57763ceeaa0d7354f86f to your computer and use it in GitHub Desktop.
This is a simple way to A Fragment with A RecyclerView kind of like how ListFragments and ListActivities work. As well as adding in an empty view that is as easy as adding a layout file to the code. To use just add The RecylcerFragment java file and recycler_layout xml file to the appropriate locations and then extend RecyclerFragment and add a …
package your.package.path.fragments;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import your.package.path.R;
import your.package.path.SimpleStringRecyclerViewAdapter;
/**
* Created by aaronfleshner on 2/7/15.
*/
public class MyFragment extends RecyclerFragment {
private String[] mDataSet = {"boom"};
private SimpleStringRecyclerViewAdapter adapter;
public MyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//set the Layout manager
setLayoutManger(new LinearLayoutManager(getActivity()));
adapter = new SimpleStringRecyclerViewAdapter(mDataSet);
//Set Adapter
setRecyclerAdapter(adapter);
// Set empty_view
setEmptyView(R.layout.empty_view);
setEmptyViewAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recylcerLayout"
xmlns:android="http://schemas.android.com/apk/res/android">
<view xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="android.support.v7.widget.RecyclerView"
android:id="@+id/list" />
</RelativeLayout>
package your.package.path;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;
import your.package.path.R;
/**
* Created by aaronfleshner on 2/7/15.
*/
public class RecyclerFragment extends Fragment {
private RecyclerView list;
private View emptyView;
private RecyclerView.Adapter mAdapter;
private RelativeLayout layout;
private Animation mEnterAnimation, mExitAnimation;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.recycler_layout, container, false);
init(v);
return v;
}
private void init(View v) {
list = (RecyclerView) v.findViewById(R.id.list);
layout = (RelativeLayout) v.findViewById(R.id.recylcerLayout);
}
public RecyclerView getRecyclerView() {
return list;
}
public void setLayoutManger(RecyclerView.LayoutManager layout) {
list.setLayoutManager(layout);
}
public void setRecyclerAdapter(RecyclerView.Adapter adapter) {
mAdapter = adapter;
list.setAdapter(mAdapter);
checkIfRecyclerViewIsEmpty();
mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
super.onItemRangeChanged(positionStart, itemCount);
checkIfRecyclerViewIsEmpty();
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
checkIfRecyclerViewIsEmpty();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
super.onItemRangeRemoved(positionStart, itemCount);
checkIfRecyclerViewIsEmpty();
}
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
super.onItemRangeMoved(fromPosition, toPosition, itemCount);
checkIfRecyclerViewIsEmpty();
}
@Override
public void onChanged() {
super.onChanged();
checkIfRecyclerViewIsEmpty();
}
});
}
public void setEmptyView(int layout) {
emptyView = LayoutInflater.from(getActivity()).inflate(layout, null);
emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
this.layout.addView(emptyView);
checkIfRecyclerViewIsEmpty();
}
public void setEmptyViewAnimations(int enterAnimation, int exitAnimation) {
mEnterAnimation = AnimationUtils.loadAnimation(getActivity(), enterAnimation);
mExitAnimation = AnimationUtils.loadAnimation(getActivity(), exitAnimation);
mEnterAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
if (emptyView != null)
emptyView.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mExitAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (emptyView != null)
emptyView.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
public void setItemAnimator(RecyclerView.ItemAnimator animation) {
list.setItemAnimator(animation);
}
@Override
public void onResume() {
super.onResume();
}
private void animLogoExit(View logo) {
logo.startAnimation(mExitAnimation);
}
private void animLogoEnter(View logo) {
logo.startAnimation(mEnterAnimation);
}
public void checkIfRecyclerViewIsEmpty() {
if (emptyView != null) {
if (mAdapter.getItemCount() == 0) {
if (mEnterAnimation != null) {
animLogoEnter(emptyView);
} else {
emptyView.setVisibility(View.VISIBLE);
}
} else {
if (mExitAnimation != null) {
animLogoExit(emptyView);
} else {
emptyView.setVisibility(View.GONE);
}
}
}
}
}
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import your.package.path.R;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by aaronfleshner on 2/7/15.
*/
public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private ArrayList<String> mDataSet;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// each data item is just a string in this case
public TextView mTextView;
String mData;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.text);
v.setOnClickListener(this);
}
//Bind all of the data to the view
public void bindView(String data) {
mData = data;
mTextView.setText(mData);
}
// set the click listener
@Override
public void onClick(View v) {
if (mData != null)
Log.e("MyAdapter", mData);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public SimpleStringRecyclerViewAdapter(String[] myDataset) {
mDataSet = new ArrayList<String>(Arrays.asList(myDataset));
}
// Create new views (invoked by the layout manager)
@Override
public SimpleStringRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.bindView(mDataSet.get(position));
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataSet.size();
}
public void removeItem(int pos) {
mDataSet.remove(pos);
notifyItemRemoved(pos);
notifyItemRangeChanged(pos, mDataSet.size());
}
public void addItem(String item, int pos) {
mDataSet.add(pos, item);
notifyItemInserted(pos);
notifyItemRangeInserted(pos, mDataSet.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment