Tab / viewpager / sliding / fragments /
Sliding Tabs
compile 'com.android.support:design:25.3.1' compile 'com.android.support:support-v4:25.3.1'2. Take some Fragments [Blank]:
tab1, tab2, tab3 e.t.c3.Now the Host Activity [ex: MainActivity.java ] should implement all the tabs: [and ovverride methods]
public class MainActivity extends AppCompatActivity implements
Tab1.OnFragmentInteractionListener,
Tab2.OnFragmentInteractionListener,
Tab3.OnFragmentInteractionListener,
Tab4.OnFragmentInteractionListener {
here my host activity is mainActivity and I have 4 tabs so .....
4. take ViewPager & TabLayout in Host Layout [ex: activity_main.xml] :
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize">
</android.support.design.widget.TabLayout>
5. Declare tabLayout with id and add all tabs:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Call"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 4"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
6. Now Create a PageAdapter Class: which helps viewPager
PagerAdapter.java
package com.nirjhor3029.offlinecalling;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.Switch;
/** * Created by nirjhor on 2/26/2018. */
public class PagerAdapter extends FragmentPagerAdapter {
public int nNoOfTabs;
public PagerAdapter(FragmentManager fm, int nNoOfTabs) {
super(fm);
this.nNoOfTabs = nNoOfTabs;
}
@Override public Fragment getItem(int position) {
switch (position)
{
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
case 3:
Tab4 tab4 = new Tab4();
return tab4;
default:
return null;
}
}
@Override public int getCount() {
return nNoOfTabs;
}
}
7. viewPager + Adapter:
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
8. tabLayout.setOnTabSelectedListener:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override public void onTabUnselected(TabLayout.Tab tab) {
}
@Override public void onTabReselected(TabLayout.Tab tab) {
}
});
💗💗💗💗💗💗💗💗💗💗💗




মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন