

That is because the view item out of screen will be automatically recycled and bind as another view item currently on the screen. And if you scroll up, you will find that the numbers are different than the number at the same position before. If you scroll down the RecyclerView, you will see more random numbers. If you see a list of random numbers as below, congratulations! You have created a RecyclerView in this fragment! Now all the code changes are done, run the app again and click NEXT button on emulator. public class SecondFragment extends Fragment Result: RecyclerView with a list of random numbers view can retrieved from inflater.inflate(). Note that ReyclerView needs to be retrieved by using the view.findViewById() instead findViewById() directly. Under recyclerview/SecondFragment.java, add the following lines to inject recyclerView to fragment. Create a new file under /res/layout/: frame_textview.xml:

Step 3: Create a FrameLayout with TextViewĪfter creating RecyclerView, we need to define resource for each item (TextView) in RecyclerView. Since this is created under ConstraintLayout, define Constraint Widget in 4 directions under Layout.Define RecyclerView id as `recyclerview`.Drag RecyclerView from Palette to Layout in fragment_second.xml (using Design view).Go to /res/layout/fragment_second.xml, add the following lines to create RecyclerView: Īlternatively, we can also use Palette in Android Studio to create RecyclerView: We are going to create a RecyclerView resource in SecondFragment.

Once you run the app on emulator, you will see a single activity application with a button which allows you to switch between two fragments. Open Android Studio, Create new project -> select Basic Activity -> Finish Application creation.
