I know Viewpager is kind of not used much since Viewpager 2, but I was as a noob recently learning Tablayout with Fragments .
I followed few blogs and tutorial, and honestly saying I understood the 50% and rest 50% I have no idea about.
I can understand other parts of the code like the layout and the MainACtivity, but I really dont understand how the Adapter and the Viepager workout in a CYCLE.
There are a 2/3 methods in the Adapter which sync up with the Viewpager and stuff.
I am posting my code below.
(I am not posting my fragments , which are to be shown in the viewpager)
App Contains :
MainActivity.java
activity_main.xml
PageAdapter.java
(Code not given for fragments)
3 fragments named :
tab1_fragment , tab2_fragment & tab3_fragment.
--------------------------------------------------------------------------------------
MainActivity.java :
package com.deepesh.tabapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout1;
ViewPager viewPager1;
TabItem tab1,tab2,tab3;
PageAdapter pageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout1 = findViewById(R.id.tablayout1);
tab1 = findViewById(R.id.calltab1);
tab2=findViewById(R.id.mailtab2);
tab3=findViewById(R.id.smstab3);
viewPager1 = findViewById(R.id.Viewpager1);
pageAdapter=new PageAdapter(getSupportFragmentManager(),tabLayout1.getTabCount());
viewPager1.setAdapter(pageAdapter);
tabLayout1.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager1.setCurrentItem(tab.getPosition());
if (tab.getPosition()==0 || tab.getPosition()==1 || tab.getPosition()==2 ){
pageAdapter.notifyDataSetChanged();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager1.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout1));
}
}
Activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout1"
android:layout_width="409dp"
android:background="@color/colorPrimary"
android:layout_height="wrap_content"
app:tabSelectedTextColor="#ffff"
app:tabIndicatorColor="#ffff"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="287dp">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:text="Call"
android:id="@+id/calltab1"
android:layout_height="wrap_content"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:text="MAIL"
android:id="@+id/mailtab2"
android:layout_height="wrap_content"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:text="SMS"
android:id="@+id/smstab3"
android:layout_height="wrap_content"/>
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Viewpager1"
/>
</LinearLayout>
PageAdapter.java :
package com.deepesh.tabapp;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
public class PageAdapter extends FragmentPagerAdapter {
int tabcount;
public PageAdapter(@NonNull FragmentManager fm, int behavior) {
super(fm, behavior);
tabcount=behavior;
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0: return new tab1_fragment();
case 1: return new tab2_fragment();
case 2: return new tab3_fragment();
default: return null;
}
}
@Override
public int getCount() {
return tabcount;
}
}
What I have tried:
I searched alott on the web , but dindt find what I was looking for.