Viewpager TabLayout Tab change when swipe - android

I want to change tab when user 20% of view swipe but right now if i swipe around 60 present then tab change. other wise it back previous tab. how can i do that.
viewPager = (ViewPagerCustomDuration) findViewById(R.id.tabs_viewpager);
viewPager.setScrollDurationFactor(1);
viewPager.setOffscreenPageLimit(3);
viewPager.animate().translationX(0f).setDuration(500);
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
viewPager.setPageMargin(pageMargin);
// viewPager.setPagingEnabled(false);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabFragmentPagerAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this, viewPager);
tabFragmentPagerAdapter.addFrag(new HomeFragment(), "Home");
tabFragmentPagerAdapter.addFrag(new DiscoverFragment(), "Discover");
tabFragmentPagerAdapter.addFrag(new TopFragment(), "Top");
tabFragmentPagerAdapter.addFrag(new CategoryFragment(), "Category");
// setupViewPager(viewPager);
viewPager.setAdapter(tabFragmentPagerAdapter);

Related

How to call an activity instead of a fragment in Tablayout?

I have an android app in which I have 3 tabs in a tablayout. I simply want the first tab to open a new activity and not a fragment, because I'll have new tabs in this new activity. What's the best solution for that?
I have tried to desing with a linear layout a "fake tab" but the problem is it's never gonna have the same size as the other tabs.
Is there a way to do that like this?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.pager);
tabLayout = findViewById(R.id.tabLayout);
adapter = new TabAdapter(getSupportFragmentManager());
adapter.addFragment(new ProfileActivity(), "Profile");
adapter.addFragment(new SwipeFragment(), "Meet");
adapter.addFragment(new FriendsFragment(), "Friends");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
this addFragment method does not accept an Activity so that line is in error. Someone has any idea on how can I achieve this?
Anyway in which I can make a tab call an activity instead of a fragment

add badge on icon of a tab in a tablayout using badgeView

I have looked all round stackoverflow how i can add a badge in an icon of a tab in a tab layout, yet have no answer.
This is my code
//Get reference to your Tablayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setIcon(ICONS[0]);
tabLayout.getTabAt(1).setIcon(ICONS[1]);
tabLayout.getTabAt(2).setIcon(ICONS[2]);
BadgeView badge = new BadgeView(this, tabLayout.getTabAt(0).getCustomView());
badge.setText("1"); //Whatever value you should add
badge.show();
BadgeView mMotification = new BadgeView(this, tabLayout.getChildAt(1));
mMotification.setText("10");
mMotification.show();
I have also tried many other alternatives but it seem like BadgeView takes only views
You can create custom layout for the tabs and add them in TabLayout
This is what i did for it to work
TabLayout.Tab tab = tabLayout.getTabAt(0);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_notifications);
tab.setCustomView(imageView);
BadgeView badge = new BadgeView(this, imageView);
badge.setText("23");
badge.setBadgeMargin(25,0);
badge.show();

How to use public method removeTab(TabLayout.Tab tab)?

Checking [https://developer.android.com/reference/android/support/design/widget/TabLayout.html][1] I created my tabs
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1 Item").setIcon(android.R.drawable.ic_dialog_email).setTag("tt"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2 Item").setIcon(android.R.drawable.ic_btn_speak_now));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3 Item").setIcon(android.R.drawable.ic_lock_idle_low_battery));
tabLayout.addTab(tabLayout.newTab().setText("Tab 4 Item").setIcon(android.R.drawable.ic_dialog_alert));
but then when I try to remove the tab I am getting error. My simple question is how can I use the method removeTab(TabLayout.Tab tab) ?
for example:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
TabLayout.Tab tab1 = tabLayout.newTab().setText("Tab 1 Item").setIcon(android.R.drawable.ic_dialog_email).setTag("tt");
tabLayout.addTab(tab1);
TabLayout.Tab tab2 = tabLayout.newTab().setText("Tab 2 Item").setIcon(android.R.drawable.ic_btn_speak_now);
tabLayout.addTab(tab2);
tabLayout.removeTab(tab1); // remove first tab
Create your tab outside the function and then add and remove them as you wish.
Something like :
TabLayout t = new TabLayout();
t.setText("test");
...
add(t);
remove(t);

Android tablayout selecting the last tab instead of first

I need your help. I have a tablayout with tintable imageview and textview. I want to highlight the default selected tab which is the first one.
This is my sample code:
TabLayout.Tab tab;
mTabs = (TabLayout) findViewById(R.id.tabs);
mViewpager = (ViewPager) findViewById(R.id.viewpager);
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
mViewpager.setAdapter(pagerAdapter);
mViewpager.addOnPageChangeListener(new MyPageScrollListener(mTabs));
mTabs.setupWithViewPager(mViewpager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < mTabs.getTabCount(); i++) {
tab = mTabs.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
mTabs.setOnTabSelectedListener(new MyOnTabSelectedListener());
tab.select();
But it's selecting the last tab. Anyone here knows how to fix this? I'd appreciate any kind of help. Thanks!
Finally, I found a solution here: link
I added:
mTabs.getTabAt(1).select();
mTabs.getTabAt(0).select();
And it works!
I found a better solution. Instead if selecting the tab from the TabLayout you can do this with the ViewPager like so:
ViewPager viewPager = (ViewPager) findViewById(R.id.restaurant_menu_viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
// ViewPager mit TabLayout verknüpfen
TabLayout tabLayout = (TabLayout) findViewById(R.id.restaurant_menu_tabLayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1);
See the post here:
https://stackoverflow.com/a/38762670/7318519
You selected the last tab.
Take a look at your source code :)
TabLayout.Tab tab;
...
for (int i = 0; i < mTabs.getTabCount(); i++) {
tab = mTabs.getTabAt(i);
}
...
tab.select();
tab is the last tab you will get with getTabAt(int position).
So when you want to select the first tab this will help
maTabs.getTabAt(0).select();
Another (maybe better) solution is to turn the for loop into this
for (int i = mTabs.getTabCount() - 1; i >= 0; i--) {
tab = mTabs.getTabAt(i);
}
Then your "last" tab you are getting in this loop is the first tab in your TabLayout.
An working full example code looks like this gist.

View Pager page not refreshing?

I have used SlidingTabLayout and Viewpager to make sliding Tab in fragement. And i have implemented it successfully.
My code for adding the fragment under view pager
void setUpFragementPager(View view) {
List<Fragment> fragments = new Vector<Fragment>();
MyFragmentAdapter mPagerAdapter;
//for each fragment you want to add to the pager
Bundle page = new Bundle();
page.putString("url", "d");
fragments.add(Fragment.instantiate(getActivity(),
AppFragement.class.getName(), page));
fragments.add(Fragment.instantiate(getActivity(),
CatFragement.class.getName(), page));
fragments.add(Fragment.instantiate(getActivity()
,FavFragement.class.getName(),page));
//after adding all the fragments write the below lines
mViewPager=(ViewPager)view.findViewById(R.id.viewpager);
mPagerAdapter = new
MyFragmentAdapter(super.getFragmentManager(), fragments);
mViewPager.setAdapter(mPagerAdapter);
mSlidingTabLayout = (SlidingTabLayout)
view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
if (mParam1.toString().equals("1")){
mViewPager.setCurrentItem(1);
mPagerAdapter.notifyDataSetChanged();
}
else
mViewPager.setCurrentItem(0);
}
Now when i click on MyLibrary button (Image 2)
under drawer the Tab should change to CATEGORY but what happen is currently now is that , white strip is going under the category tab but the CatFragement is not loading under the same tab(category tab). You can see it in the snapshot (image 3)
I used
mViewPager.setCurrentItem(1);
mPagerAdapter.notifyDataSetChanged();
but it leading me to the my desired result . The Desired Result should be look like image4 which you can see in the snapshot too.
Please help me where i am doing wrong.

Categories

Resources