We know about Activities and custom views. But there is another kind of activity: the Fragment.
You can define a Fragment as any Activity with different views and own events and components.
You usually embedded a Fragment with a Viewpager view to swipe all fragments to left and right.
With a Tab Layout you can select a fragment with different buttons in the Tab Layout.
To make this work we use the Fragment Adapter component.
So let's start.
Create the fragment activities
Click the '+' button.
* Give the name 'red'
* deactivate statusbar and toolbar to make a blank activity
and set the type to 'fragment' then save the new fragment.
Add now three more fragments with name 'blue', 'yellow' and 'green'.
(don't forget to set types fragment and deactivate statusbar and toolbar in all fragment-activities).
Here in the Library Manager click the AppCompbat library,
Continue in Main activity
Add Fragment Adapter component
Be sure to be in Main activity. Open the component tab and add the Fragment Adapter component.
* TabLayout tablayout1
* ViewPager viewpager1
Fragment activities:
the views
Open the red_fragment.xml.
Add a LinearV linear1.
* set width and height to 'match_parent'.
Open now each other fragment,
add a linear, set width and height to 'match_parent' and set the background color to the corresponding name (blue_fragment.xml -> background color BLUE,
yellow_fragment.xml ->background color YELLOW,
green_fragment.xml -> background color GREEN)
💡The colored linears are for demonstrating. You can create each fragment as you want.
Main activity:
the events
Open the onCreate event.
Add from the View palette flowing blocks:
* From AndroidX section the 'TabLayout setupWithViewPager' block
Set TabLayout to tablayout1 and ViewPager to viewpager1.
* From List section the 'ViewPager
setFragmentAdapter' block
Set ViewPager to viewpager1, FragmantAdapter to frag_adapt and count to 4.
Now the TabLayout tablayout1 is setup with ViewPager viewpager1 and the ViewPager viewpager1 is bound to Fragment Adapter frag_adapt with 4 entries also according to the TabLayout.
Now the TabLayout tablayout1 is setup with ViewPager viewpager1 and the ViewPager viewpager1 is bound to Fragment Adapter frag_adapt with 4 entries also according to the TabLayout.
Now it's time to determine the titles of the four TabLayout buttons and to determine the fragments to be shown in the ViewPager.
In the event section click the Component tab.
Add the events of the comment:
* frag_adapt Return Title
* frag_adapt Return Fragment
Add a if block and set it's condition to 'position'=''.
Scroll down the block list to 'TabLayout sk' and drag and drop the 'return title' block inside the if block.Copy this if-block combination 3 times to get four of it.
Set the position numbers according to the titles:
* position = 0 -> title : "RED"
* position = 1 -> title : "BLUE"
* position = 2 -> title : "YELLOW"
* position = 3 -> title : "GREEN"
Close the event and open the Return Fragment event.
Add the if block like in the Return Title event. Then scroll down the block list to 'fragments sk' and drag and drop the 'return fragment' block.Copy again this if-block combination three times to get four of it.
Set the position numbers according to the fragments:
* position = 0 -> Content: RedFragmentActivity
* position = 1 -> Content: BlueFragmentActivity
* position = 2 -> Content: YellowFragmentActivity
* position = 3 -> Content: GreenFragmentActivity
So, well done 👍
You can run the App.
No comments:
Post a Comment