Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. public class MainFragment extends Fragment {
  2.  
  3.  
  4. private RecyclerView mRecyclerView;
  5.  
  6.  
  7. @Override
  8. public void onCreate(@Nullable Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10.  
  11.  
  12. }
  13.  
  14. @Override
  15. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  16. Bundle savedInstanceState) {
  17. View view = inflater.inflate(R.layout.fragment_main, container, false);
  18.  
  19. mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_recycler_view);
  20.  
  21. mRecyclerView.setHasFixedSize(true);
  22.  
  23. mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  24.  
  25. mRecyclerView.setAdapter(new MyAdapter(new String[]{"test one", "test two", "test three", "test four", "test five", "test six", "test seven"}));
  26.  
  27. return view;
  28. }
  29.  
  30. @Override
  31. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  32. super.onViewCreated(view, savedInstanceState);
  33.  
  34. }
  35.  
  36. }
  37.  
  38. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
  39.  
  40. private String[] mDataset;
  41.  
  42. // Provide a reference to the views for each data item
  43. // Complex data items may need more than one view per item, and
  44. // you provide access to all the views for a data item in a view holder
  45. static class MyViewHolder extends RecyclerView.ViewHolder {
  46. CardView mCardView;
  47. TextView mTextView;
  48.  
  49. MyViewHolder(View v) {
  50. super(v);
  51.  
  52. mCardView = (CardView) v.findViewById(R.id.card_view);
  53. mTextView = (TextView) v.findViewById(R.id.tv_text);
  54. }
  55. }
  56.  
  57. // Provide a suitable constructor (depends on the kind of dataset)
  58. public MyAdapter(String[] myDataset) {
  59. mDataset = myDataset;
  60. }
  61.  
  62. // Create new views (invoked by the layout manager)
  63. @Override
  64. public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
  65. int viewType) {
  66. // create a new view
  67. View v = LayoutInflater.from(parent.getContext())
  68. .inflate(R.layout.card_item, parent, false);
  69. // set the view's size, margins, paddings and layout parameters
  70. return new MyViewHolder(v);
  71. }
  72.  
  73. @Override
  74. public void onBindViewHolder(MyViewHolder holder, int position) {
  75. holder.mTextView.setText(mDataset[position]);
  76. }
  77.  
  78. @Override
  79. public int getItemCount() {
  80. return mDataset.length;
  81. }
  82. }
  83.  
  84. <?xml version="1.0" encoding="utf-8"?>
  85. <android.support.constraint.ConstraintLayout
  86. xmlns:android="http://schemas.android.com/apk/res/android"
  87. xmlns:app="http://schemas.android.com/apk/res-auto"
  88. xmlns:tools="http://schemas.android.com/tools"
  89. android:layout_width="match_parent"
  90. android:layout_height="match_parent">
  91. <android.support.v7.widget.RecyclerView
  92. android:id="@+id/rv_recycler_view"
  93. android:layout_width="0dp"
  94. tools:layout_constraintRight_creator="1"
  95. tools:layout_constraintBottom_creator="1"
  96. android:layout_marginStart="8dp"
  97. app:layout_constraintBottom_toBottomOf="parent"
  98. android:layout_marginEnd="8dp"
  99. app:layout_constraintRight_toRightOf="parent"
  100. tools:layout_constraintLeft_creator="1"
  101. app:layout_constraintLeft_toLeftOf="parent"
  102. android:layout_marginLeft="8dp"
  103. android:layout_marginRight="8dp"
  104. tools:listitem="@layout/card_item"
  105. android:layout_marginTop="8dp"
  106. app:layout_constraintTop_toTopOf="parent"
  107. android:layout_marginBottom="8dp"
  108. android:layout_height="0dp"
  109. android:scrollbars="vertical"
  110. android:background="@android:color/white">
  111. </android.support.v7.widget.RecyclerView>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement