Get Horizontal Scroll on TabLayout - android

I want to interpolate between two colors while scrolling or "swiping" to the next tab in a TabLayout. To do this, I need some way to know when the user is swiping between tabs. I have tried using TabLayout.OnScrollChangeListener but the method is never called. Is it possible to know when the user is swiping to a new tab and, if so, how?
Edit:
libraryTabs.setOnScrollChangeListener(new TabLayout.OnScrollChangeListener() {
#Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
}
});
is never called.

i think to achieve what you want then you will need a ViewPage
something like this
ViewPager mViewPager;
TabLayout mSlidingTabs;
mSlidingTabs = (TabLayout) findViewById(R.id.tabs);
mSlidingTabs.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.w(TAG, "onPageScrolled");
// do what you want
}
#Override
public void onPageSelected(int position) {
Log.w(TAG, "onPageSelected");
// do what you want
}
#Override
public void onPageScrollStateChanged(int state) {
Log.w(TAG, "onPageScrollStateChanged");
// do what you want
}
});

Try this.
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
//Do whatever you want here.
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

Related

get data to viewPager fragment from its fragments

I am using viewPager for my custom Tablayout. I have two fragments, when viewPager activity was created , it should get the data from one of the fragment and have to display it on the tabs as unread count.
but I am getting data at scrolling. how can I over come this issue ?. please help me.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
PendingPlusPendingForUploadFiles frage = new PendingPlusPendingForUploadFiles();
System.out.println("====c value===" + frage.count);
}
#Override
public void onPageSelected(int position) {
// previousTabPosition = position;
viewPager.setCurrentItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
you can onResume(); method in the Fragment get it!

Android show simple Toast on each calling Fragment on ViewPager

you suppose i have two fragment on viewpager which i can switch between them by swipe, for example Fragment1 and Fragment2,
what i want to that when i swipe? i want to show toast between switch on them, for example when i swipe to switch on Fragment1, application show toast, or when i swipe to switch on Fragment2 application show toast, and again, show toast each showing and switching between fragments,
how can i do that? i can't find any documentation about this solution
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
// Show your Toast Here like
Toast.makeText(app.getBaseContext(),"Some string",
Toast.LENGTH_SHORT).show();
}
});
and if you want some method's inside your Fragments use this in every fragment whereever you want to show toast
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
}
try this
viewpagerEvent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//toast message here
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
set OnPageChangeListener on ViewPager to get callback when page changed/scrolled or selected.
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// Show fragment change/switch toast
Toast.makeText(this,"Fragment1",
Toast.LENGTH_LONG).show();
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
You have to make condition for Both Fragment while on page changed. try below code on your Activity Class.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position == 0)
// Toast For Fragment 1
if(position == 1)
// Toast For Fragment 2
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

How to know whether the page is selected from Swipe or Click of Tab of Viewpager

I have a ViewPager which Toolbar tabs.
I have to know how many times user clicked tabs and how many times user swiped and selected a page.
I am using ViewPager.OnPageChangeListener() for this purpose.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override public void onPageSelected(int position) {
// Here i am sending the GA event
}
#Override public void onPageScrollStateChanged(int state) {
}
});
OnPageSelected is called for both click and swipe of page.
How will I differentiate the page selected is from click of tabs or its from swipe of Viewpager ?
Here is my solution. I am basing on single variable.
public class MainActivity extends AppCompatActivity {
// remember last action
private Action lastAction = Action.RESET;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...
mViewPager = findViewById(R.id.viewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i1) {
// No-op
}
#Override
public void onPageSelected(int i) {
if (lastAction == Action.RESET) {
lastAction = Action.SWIPE;
Log.d(TAG, "onPageSelected: SWIPED");
} else {
lastAction = Action.RESET;
}
}
#Override
public void onPageScrollStateChanged(int i) {
// No-op
}
});
mTabLayout = findViewById(R.id.tabLayout);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (lastAction == Action.RESET) {
lastAction = Action.SELECT;
Log.d(TAG, "onPageSelected: SELECTED");
} else {
lastAction = Action.RESET;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
// No-op
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
// No-op
}
});
}
}
Limitation:
Content for viewPager must be loaded before adding listeners because this solutions is basing on ordered calls (onTabSelected, onPageSelected).
Preview:
In this example I will be checking if the user selected the page at index 1 by swiping or by tapping the tab:
Note: You can use tabLayout.getChildAt(0) to get the main sliding layout, and
((ViewGroup) tabLayout.getChildAt(0)).getChildAt(desiredPosition) //to get the tab at desired position
Using this, we add onClickListener to the desired tab and once clicked, its onClick() method will be called first followed by the onTabSelected() method of the TabSelectedListener of the TabLayout.
private Boolean tabClicked = false; //variable which determines after entering the onTabSelected() method, if onClick was called or not
((ViewGroup) tabLayout.getChildAt(0)).getChildAt(1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tabClicked = true;
}
});
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if(tab.getPosition()==1){
if(tabClicked){
//your tab was clicked, do work here
}
else{
//your tab was swiped, do some work here
}
tabClicked = false;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {}
#Override
public void onTabReselected(TabLayout.Tab tab) {}
});
The tabview you use, is a textview. So there will be onclicklistener for this textview. You could track the tab click on onClick() method of listener!
Try this
fun onTabClickedListener(callback: String.() -> Unit){
tabContainer?.let { tabContainer->
for (i in 0 until tabContainer.tabCount) {
val text = tabContainer.getTabAt(i)?.text.toString() ?: ""
(tabContainer.getChildAt(0) as ViewGroup).getChildAt(i).tag = text
(tabContainer.getChildAt(0) as ViewGroup).getChildAt(i).setOnClickListener {
callback(text)
}
}
}
}```

Hide/Show FloatingActionButton on tab Click

I have a SlidingTabLayout with this pageListener, when I scroll to another tab, the FAB hides and then shows again, this is correct, the problem is that it doesn't work when I click tab, it just hides and doesn't reappear until I scrollPage again. Any ideas? (I'm using support design library of course)
private ViewPager.OnPageChangeListener pageListener = new
ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
Log.i("SCROLL", "onPageSelected");
fab.show()
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
Log.i("SCROLL", "onPageScrolled");
if(fab.isShown()) {
fab.hide();
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
Log.i("SCROLL", "onStateChanged: "+arg0);
fab.show();
}
};

Change ActionbarTitle using ViewPagerAdapter

Currently i am trying to deveolop an app which implements swiping between different views.
The swiping works fine, but i want to set the title of the view to the actionbar, that the users knows where he is.
So i hope you can give me some ideas where i had to put the code for the title change.
Thanks
You'll need to set up an OnPageChangeListener for your ViewPager. When you swipe, the OnPageChangeListener will be notified, and you can change the ActionBar title from the callback.
Here's some pseudo code for you:
ViewPager vp;
vp.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
getActionBar().setTitle(titles[position]); // Set the correct title based on the position
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} // not used
#Override
public void onPageScrollStateChanged(int state) {} // not used
});
public void setTitle (CharSequence title)
Change the title associated with this activity. If this is a top-level activity, the title for its window will change. If it is an embedded activity, the parent can do whatever it wants with it.
http://developer.android.com/reference/android/app/Activity.html#setTitle(int)
mPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int pos) {
// Chage the title
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});

Categories

Resources