Last active
December 18, 2019 06:39
-
-
Save sheharyarn/20f171e900eff32bf38fd8be1d30911d to your computer and use it in GitHub Desktop.
A simple Fragment with RecyclerView
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
/** | |
* RVFragment - Fragment with a simple RecyclerView that | |
* only takes Strings | |
* | |
* Usage: | |
* RVFragment rvf = new RVFragment(); | |
* | |
* @author Sheharyar Naseer | |
*/ | |
public class RVFragment extends Fragment { | |
String[] strings = {"1", "2", "3", "4", "5", "6", "7"}; | |
public RVFragment() {} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
RecyclerView rv = new RecyclerView(getContext()); | |
rv.setLayoutManager(new LinearLayoutManager(getContext())); | |
rv.setAdapter(new SimpleRVAdapter(strings)); | |
return rv; | |
} | |
/** | |
* A Simple Adapter for the RecyclerView | |
*/ | |
public class SimpleRVAdapter extends RecyclerView.Adapter<SimpleViewHolder> { | |
private String[] dataSource; | |
public SimpleRVAdapter(String[] dataArgs){ | |
dataSource = dataArgs; | |
} | |
@Override | |
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = new TextView(parent.getContext()); | |
SimpleViewHolder viewHolder = new SimpleViewHolder(view); | |
return viewHolder; | |
} | |
@Override | |
public void onBindViewHolder(SimpleViewHolder holder, int position) { | |
holder.textView.setText(dataSource[position]); | |
} | |
@Override | |
public int getItemCount() { | |
return dataSource.length; | |
} | |
} | |
/** | |
* A Simple ViewHolder for the RecyclerView | |
*/ | |
public static class SimpleViewHolder extends RecyclerView.ViewHolder{ | |
public TextView textView; | |
public SimpleViewHolder(View itemView) { | |
super(itemView); | |
textView = (TextView) itemView; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment