Create Tablayout with Viewpager in Android. Tabs in Android. Tablayout with Fragments and Viewpager

Useful video : https://youtu.be/528-udFokS4

TABLAYOUT WITHOUT VIEWPAGER :
https://code8808.blogspot.com/2020/07/create-tablayout-without-viewpager-in.html


---------------------------------------------------------------------------------------------------------

NOTE : A new way of implementing viewpager2 

https://developer.android.com/training/animation/screen-slide-2
https://www.youtube.com/watch?v=iJpB5ju3tN8

Adapter is now "FragmentStateAdapter"


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">

<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout1"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:background="@color/purple_200"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabIndicatorColor="#ffff"
app:tabSelectedTextColor="#ffff">

<com.google.android.material.tabs.TabItem
android:id="@+id/calltab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call" />

<com.google.android.material.tabs.TabItem
android:id="@+id/mailtab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAIL" />

<com.google.android.material.tabs.TabItem
android:id="@+id/smstab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMS" />
</com.google.android.material.tabs.TabLayout>

<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager2_main"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/tablayout1" />


</androidx.constraintlayout.widget.ConstraintLayout>

package com.deepesh.fragmenttest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;


import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {

TabLayout tabLayout;
ViewPager2 viewPager2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tabLayout = findViewById(R.id.tablayout1);
viewPager2 = findViewById(R.id.viewpager2_main);

// set our custom adapter to viewpager
pagerAdapter adapter = new pagerAdapter(this);
viewPager2.setAdapter(adapter);


// connect tab items with viewpager
new TabLayoutMediator(tabLayout, viewPager2,
new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {

String[] tab_names = {"CALL","MAIL","SMS"};

tab.setText(tab_names[position]);

}
}).attach();

}


//============================================================

public static class pagerAdapter extends FragmentStateAdapter {

public pagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}

@NonNull
@Override
public Fragment createFragment(int position) {

switch (position) {

case 0:
return new CallFragment();
case 1:
return new MailFragment();
default:
return new SmsFragment();

}
}

@Override
public int getItemCount() {
return 3;
}
}


}








---------------------------------------------------------------------------------------------------------


To Attach ViewPager2 & TabLayout , you can use the below code of line to attach them inside the OnCreate( ) 


 viewpager2.setAdapter(new ViewPagerAdapter(Arrays.asList(images)));

TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
tabLayout,viewpager2,
(tab, position) -> tab.setText("OBJECT " + (position + 1)) );
tabLayoutMediator.attach();



---------------------------------------------------------------------------------------------------------


Here we need 2 listeners , one for Tab clicks on the TabItems and one on the Viewpager for Sliding.






Step1]  In the Lyout File  Create  TabLayout and Insert TabItems inside it. 
Then Add a Viewpager below it.
Give all of them id.

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>


Step3] Create the Fragments , you want to show in the Viewpager.



Step 4] MAKE A FRAGMENTPAGERADAPTER.

This adapter will be used to fill Fragments to the viewpager

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs


i] create a new Java class and make it extends the FragmentpagerAdapter.

ii] Implement its two methods i.e getcount() and getItem()

iii] Create  its superclass constructor.



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;
}
}







Comments

Popular posts from this blog

React Js + React-Redux (part-2)

React Js + CSS Styling + React Router (part-1)

ViteJS (Module Bundlers, Build Tools)