I want to show tab bar icon left side of text - android

I want to show icon left side of the text but the problem is that the icon is showing above the text. it's taking much more space.
I want keep the icon left sode of the text(title) . what should i do?
The main activity is given below
java.class
public class Tabsadar extends AppCompatActivity {
ExpandableRelativeLayout
expandableLayout1,
expandableLayout2,
expandableLayout3,
expandableLayout4;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_action_name,
R.drawable.thanatottho,
R.drawable.proyojonio
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabsadar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new tabsadarc1(), "ফিচার");
adapter.addFrag(new tabsadarc2(), "তথ্যাবলি");
adapter.addFrag(new tabsadarc3(), "প্রয়োজনীয়");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}'
i want something like that
in my case something happend like that
any solution?

Just use app:tabInlineLabel="true"
Like:-
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabInlineLabel="true"
app:tabGravity="fill"/>

As per your requirement I'll suggest you to create a custom view for tabs.
custom_tab_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/_6sdp"
android:background="#drawable/ic_menu_gallery"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/_3sdp"
android:textColor="#drawable/tab_text_selector"
android:textSize="#dimen/_15sdp" />
</LinearLayout>
</RelativeLayout>
And this layout of custom tab with PagerAdapter as
//On Create
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
View customView = mPagerAdapter.getCustomView(getActivity(), i);
mainTabLayout.getTabAt(i).setCustomView(customView);
}
//Pager Adapter
public static class ViewPagerAdapter extends SmartFragmentStatePagerAdapter {
private List<Map<String, Object>> maps = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public View getCustomView(Context context, int pos) {
View mView = LayoutInflater.from(context).inflate(R.layout.custom_tab_view, null);
TextView mTextView = (TextView) mView.findViewById(R.id.textView);
ImageView imageView = (ImageView) mView.findViewById(R.id.imageView);
mTextView.setGravity(Gravity.CENTER);
mTextView.setTypeface(Typeface.createFromAsset(context.getAssets(),
"fonts/aller_regular.ttf"));
mTextView.setText(getPageTitle(pos));
return mView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new CompletedTaskFragment();
case 1:
return new PendingTaskFragment();
}
return null;
}
#Override
public int getCount() {
return 2;
}
}
Hope it'll help you.

Related

how to override and display title of tabs in viewpager2 in tablayout

I am using a TabLayout inside a Fragment. The content of the TabLayout (made of 2 fragments) is displayed but not the title of the tabs. I believe it's a layout problem but i'm not sure on how to fix it.
Also what would be the overrided method for ViewPager2 that was used in ViewPager (getPagetitle) ?
main Fragment
public class CommunityFragment extends Fragment {
private TabLayout tbL;
private ViewPager2 vp;
private RecipeViewPagerAdapter rvpa;
public CommunityFragment() {
// Required empty public constructor
}
public static CommunityFragment newInstance(){
return new CommunityFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_community, container, false);
tbL = rootView.findViewById(R.id.tabL);
vp = rootView.findViewById(R.id.pager);
rvpa = new RecipeViewPagerAdapter(this);
vp.setAdapter(rvpa);
rvpa.addFragment(new AllFragment(),"All time");
rvpa.addFragment(new DayFragment(),"Today");
new TabLayoutMediator(tbL, vp, new TabLayoutMediator.TabConfigurationStrategy() {
#Override public void onConfigureTab(#NonNull TabLayout.Tab tab, int position) {
//////title????
}
}).attach();
return rootView;
}
}
Adapter
public class RecipeViewPagerAdapter extends FragmentStateAdapter {
private ArrayList<Fragment> listF = new ArrayList<>();
private ArrayList<String> listT = new ArrayList<>();
public RecipeViewPagerAdapter(#NonNull Fragment fragment) {
super(fragment);
}
#NonNull
#Override
public Fragment createFragment(int position) {
return listF.get(position);
}
//new method ????
public CharSequence getPageTitle(int position){
return listT.get(position);
}
#Override
public int getItemCount() {
return listT.size();
}
public void addFragment(Fragment frag, String title){
listF.add(frag);
listT.add(title);
}
}
fragment layout
<LinearLayout 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"
android:orientation="vertical"
tools:context=".fragments.CommunityFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorColor="#color/yellowCool"/>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
put this in your main activity....
myAdapter=new FragmentAdapter(this);
myViewPager2.setAdapter(myAdapter);
String [] tabTtiles={"Home","Chat","Notification","Account"};
new TabLayoutMediator(myTabLayout, myViewPager2,(myTabLayout, position) ->
myTabLayout.setText(tabTtiles[position])).attach();
Your tabLayout doesn't display. Use this:
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" // add this line
/>
TabLayoutMediator:
new TabLayoutMediator(tbL, vp, new TabLayoutMediator.TabConfigurationStrategy() {
#Override public void onConfigureTab(#NonNull TabLayout.Tab tab, int position) {
if(position == 0)
tab.setText("All time");
else
tab.setText("Today");
}
}).attach();
You can use the following adapter for the viewPager2:
public class ViewPagerAdapter extends FragmentStateAdapter {
private static final int CARD_ITEM_SIZE = 10;
public ViewPagerAdapter(#NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
#NonNull #Override public Fragment createFragment(int position) {
return CardFragment.newInstance(position);
}
#Override public int getItemCount() {
return CARD_ITEM_SIZE;
}
}
on the mainActivity you need to have something inline with the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.view_pager);
tabLayout = findViewById(R.id.tabs);
viewPager.setAdapter(new ViewPagerAdapter(this));
new TabLayoutMediator(tabLayout, viewPager,
new TabLayoutMediator.TabConfigurationStrategy() {
#Override public void onConfigureTab(#NonNull TabLayout.Tab tab, int position) {
tab.setText("Tab " + (position + 1));
}
}).attach();
}
}
As per documentation:
The TabLayoutMediator object also handles the task of generating page
titles for the TabLayout object, which means that the adapter class
does not need to override getPageTitle():
And to do that:
TabLayout tabLayout = view.findViewById(R.id.tab_layout);
new TabLayoutMediator(tabLayout, viewPager,
(tab, position) -> tab.setText("OBJECT " + (position + 1))
).attach();

Fragment Not Replaced In ViewPager

I am trying to replace a fragment displayed in a tab using TabLayout and ViewPager as shown below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="in.netstreamtech.phonebuylogin.LoginRegisterActivity"
android:paddingLeft="32.5dp"
android:paddingTop="27dp"
android:paddingRight="30dp">
<LinearLayout
android:id="#+id/main_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/signInReOtpHeader"
android:layout_width="268dp"
android:layout_height="122dp"
android:includeFontPadding="true"
android:maxLines="2"
android:text="Welcome \nback."
android:layout_gravity="center_vertical"
android:textColor="#1aa4b8"
android:textSize="35sp" />
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="wrap_content"
android:layout_height="47dp"
android:minHeight="?attr/actionBarSize"
android:elevation="6dp" />
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
From my Fragment:
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof RegisterMainToActivity) {
listener = (RegisterMainToActivity) context;
}
}
Then later in the same fragment:
listener.passData(userRegistration);
The Interface:
public interface RegisterMainToActivity {
void passData(UserRegistration data);
}
In My Activity(LoginRegisterActivity):
public class LoginRegisterActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener, RegisterMainToActivity {
//This is our tablayout
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
private LoginPager adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_register);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
createViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
createTabIcons();
}
private void createTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.login_cutom_tab_view, null);
tabOne.setText("SIGN IN");
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.login_cutom_tab_view, null);
tabTwo.setText("REGISTER");
tabLayout.getTabAt(1).setCustomView(tabTwo);
}
private void createViewPager(ViewPager viewPager) {
adapter = new LoginPager(getSupportFragmentManager());
adapter.addFrag(new Login(), "SIGN IN");
adapter.addFrag(new RegisterMain(), "REGISTER");
viewPager.setAdapter(adapter);
}
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//tabLayout.setBackground(getResources().getDrawable(R.drawable.login_tab_selected));
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//tabLayout.setBackground(getResources().getDrawable(R.drawable.login_tab_unselected));
}
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
#Override
public void passData(UserRegistration data) {
RegisterOtp registerOtpFrag = new RegisterOtp();
adapter.replaceFragment(registerOtpFrag, 1, "REGISTER");
//adapter = new LoginPager(getSupportFragmentManager());
//adapter.addFrag(new Login(), "SIGN IN");
//adapter.addFrag(new RegisterOtp(), "REGISTER");
//adapter.clearFragments();
//adapter.addFrag(new Login(), "SIGN IN");
//adapter.addFrag(new RegisterOtp(), "REGISTER");
//viewPager.setAdapter(adapter);
createTabIcons();
}
// public void attemptRegister(View v){
// TextView registerName = (TextView) v.getContext()findViewById(R.id.register_name);
// Toast.makeText(
// getApplicationContext(),
// registerName.getText(),
// Toast.LENGTH_SHORT
// ).show();
// }
}
In the function passData , I am trying to replace a fragment. I want this added to the backstack.
In my adapter class:
public class LoginPager extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public LoginPager(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public void replaceFragment(Fragment fragment, int index, String title) {
mFragmentList.remove(index);
mFragmentList.add(index, fragment);
mFragmentTitleList.add(title);
notifyDataSetChanged();
}
public void clearFragments()
{
mFragmentList.clear();
notifyDataSetChanged();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public int getItemPosition(Object object) {
return this.POSITION_NONE;
}
}
I have tried many variations, including trying to clear the adapter and recreate the fragments. Whatever I do I am not able to replace the desired Fragment(RegisterMain with RegisterOtp). Ideally I should just be able to replace it... Please help!!!
FragmentStatePagerAdapter works for me.
public class LoginPager extends FragmentStatePagerAdapter {...}
FragmentPagerAdapter.notifyDataSetChanged() didn't clear those already committed fragments.
FragmentPagerAdapter is best for use when there are a handful of
typically more static fragments to be paged.

How to create tabs & their associated Fragments dynamically?

I want to create n number of tabs dynamically, using Rest APIs, Since every tab is associated with there Fragments while swiping, can I create tabs + Fragments dynamically using the number of tabs given in JSON (Rest API) or I have to create all individually?
Yes, you can:
<android.support.design.widget.TabLayout
android:id="#+id/htab_tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
android:background="#color/colorDivider"
app:tabIndicatorColor="#android:color/white" />
<android.support.v4.view.ViewPager
android:id="#+id/htab_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
 
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.htab_viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.htab_tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
//jsonResArray get from web service
for (int i = 0; i < jsonResArray.size(); i++) {
adapter.addFrag(new NewFragment(jsonResArray.get(i)), jsonResArray.get(i).getName());
}
viewPager.setAdapter(adapter);
}
static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List < Fragment > mFragmentList = new ArrayList < >();
private final List < String > mFragmentTitleList = new ArrayList < >();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

Tabs with in the Tabs in android studio

I just want to implements Tabs with in the Tabs. If it is possible than please share the code
See image below for reference
This is the answer of my question. I set the tabs with in the Tabs. Basically set the tab in fragment:
My java code:
public class SD extends Fragment {
View view;
PagerAdapter adapter;
public SD() {
// Required empty public constructor
}
private void setupViewPager(ViewPager viewPager) {
///Here we have to pass ChildFragmentManager instead of FragmentManager.
adapter = new PagerAdapter(getChildFragmentManager());
adapter.addFragment(new Sim1Detail(), "Sim 1");
adapter.addFragment(new Sim2Detail(), "Sim 2");
viewPager.setAdapter(adapter);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.sd, container, false);
super.onCreate(savedInstanceState);
// ButterKnife.bind(getActivity(), view);
final ViewPager viewPager = ButterKnife.findById(view, R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = ButterKnife.findById(view, R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
#Override
public void onDestroyView() {
super.onDestroyView();
// ButterKnife.unbind(getActivity);
}
static class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public PagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
here it is ..

PagerSlidingTabStrip shows wrong ViewPager

I have used fragments for drawer item, each fragments consists of non-fixed tabs.I'm using PagerSlidingTabStrip library for showing tabs.When fragment is initialized it shows 1st ViewPager, which is correct but when I click on 2nd tab it shows up 3rd ViewPager.Likewise the last ViewPager is never shown.
Tab xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dp"
android:textColor="#000000"
android:textSize="#dimen/sp_fifteen"
app:pstsDividerColor="#color/red"
app:pstsIndicatorColor="#color/black"
app:pstsTextAllCaps="true"
app:pstsUnderlineColor="#color/gray" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Adapter class:
private PagerSlidingTabStrip tabStrip;
private Adapter adapter;
public static TowerOneTabs newInstance() {
TowerOneTabs fragment = new TowerOneTabs();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mView = getActivity().getLayoutInflater().inflate(R.layout.tower_tabs_layout, null);
super.onCreate(savedInstanceState);
ViewPager viewPager = (ViewPager) mView.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabStrip = (PagerSlidingTabStrip) mView.findViewById(R.id.tabs);
tabStrip.setViewPager(viewPager);
tabStrip.setOnPageChangeListener(this);
return mView;
}
#Override
public void onResume() {
super.onResume();
tabStrip.setOnPageChangeListener(this);
}
#Override
public void onDestroyView() {
super.onDestroyView();
}
private void setupViewPager(ViewPager viewPager) {
//Here we have to pass ChildFragmentManager instead of FragmentManager.
adapter = new Adapter(getChildFragmentManager());
adapter.addFragment(TowerOne.newInstance(), getResources().getString(R.string.tower1));
adapter.addFragment(T11.newInstance(), Constants.KEY_T1_1);
adapter.addFragment(T12.newInstance(), Constants.KEY_T1_2);
adapter.addFragment(T13.newInstance(), Constants.KEY_T1_3);
adapter.addFragment(T14.newInstance(), Constants.KEY_T1_4);
adapter.addFragment(T15.newInstance(), Constants.KEY_T1_5);
viewPager.setAdapter(adapter);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
switch (position) {
case 0:
// BaseActivity.getTitleView().setText(Constants.KEY_TOWER_ONE);
break;
case 1:
mCallback.showDialog();
break;
case 2:
mCallback.showDialog();
break;
case 3:
mCallback.showDialog();
break;
case 4:
mCallback.showDialog();
break;
case 5:
mCallback.showDialog();
break;
default:
break;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
I checked this code and its working. If you want to add extra parameter (like Constant), you can edit the SampleFragmentPagerAdapter's addFragment method.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
//viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager()));
setupViewPager(viewPager);
// Give the PagerSlidingTabStrip the ViewPager
PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
// Attach the view pager to the tab strip
tabsStrip.setViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
SampleFragmentPagerAdapter adapter = new SampleFragmentPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
viewPager.setAdapter(adapter);
}
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SampleFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return mFragmentList.size();
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

Categories

Resources