Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainFragment extends Fragment {
- private RecyclerView mRecyclerView;
- @Override
- public void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_main, container, false);
- mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_recycler_view);
- mRecyclerView.setHasFixedSize(true);
- mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
- mRecyclerView.setAdapter(new MyAdapter(new String[]{"test one", "test two", "test three", "test four", "test five", "test six", "test seven"}));
- return view;
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- }
- }
- public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
- private 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
- static class MyViewHolder extends RecyclerView.ViewHolder {
- CardView mCardView;
- TextView mTextView;
- MyViewHolder(View v) {
- super(v);
- mCardView = (CardView) v.findViewById(R.id.card_view);
- mTextView = (TextView) v.findViewById(R.id.tv_text);
- }
- }
- // Provide a suitable constructor (depends on the kind of dataset)
- public MyAdapter(String[] myDataset) {
- mDataset = myDataset;
- }
- // Create new views (invoked by the layout manager)
- @Override
- public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
- int viewType) {
- // create a new view
- View v = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.card_item, parent, false);
- // set the view's size, margins, paddings and layout parameters
- return new MyViewHolder(v);
- }
- @Override
- public void onBindViewHolder(MyViewHolder holder, int position) {
- holder.mTextView.setText(mDataset[position]);
- }
- @Override
- public int getItemCount() {
- return mDataset.length;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <android.support.v7.widget.RecyclerView
- android:id="@+id/rv_recycler_view"
- android:layout_width="0dp"
- tools:layout_constraintRight_creator="1"
- tools:layout_constraintBottom_creator="1"
- android:layout_marginStart="8dp"
- app:layout_constraintBottom_toBottomOf="parent"
- android:layout_marginEnd="8dp"
- app:layout_constraintRight_toRightOf="parent"
- tools:layout_constraintLeft_creator="1"
- app:layout_constraintLeft_toLeftOf="parent"
- android:layout_marginLeft="8dp"
- android:layout_marginRight="8dp"
- tools:listitem="@layout/card_item"
- android:layout_marginTop="8dp"
- app:layout_constraintTop_toTopOf="parent"
- android:layout_marginBottom="8dp"
- android:layout_height="0dp"
- android:scrollbars="vertical"
- android:background="@android:color/white">
- </android.support.v7.widget.RecyclerView>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement