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();
}
};
Related
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) {
}
});
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) {
}
});
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)
}
}
}
}```
i have two custom button Add and Remove, And when i click on Add tab Work good, after when i remove tab its go ok, but when i remove last tab then i get following error.
java.lang.NullPointerException
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3699)
at android.view.ViewGroup.removeViewAt(ViewGroup.java:3663)
at app.burhanimumineenbrowser.HomeActivity$3.onClick(HomeActivity.java:184)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4963)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
When i remove first tab its del , second one also delete but after third tab i cant remove it, error say me null pointer.
Here is Add Button Code.
btNewtab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
COUNT_TAB+=1;
// viewPager.setAdapter(mAdapter);
actionBar.addTab(actionBar.newTab().setText("New Tabs")
.setTabListener(HomeActivity.this));
mAdapter.notifyDataSetChanged();
}
});
Remove button Code.
btCloseTab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(COUNT_TAB>0) {
if(TAB_CURRENT>=0 && PAGE_CURRENT>=0) {
System.out.println("CLOSE PAGE AND TAB : "+TAB_CURRENT+" AND "+PAGE_CURRENT);
COUNT_TAB -=1;
// int ichektab=TAB_CURRENT-1;
int ip=viewPager.getCurrentItem();
viewPager.removeViewAt(ip);
actionBar.removeTabAt(TAB_CURRENT);
//actionBar.removeTab(iTab);
mAdapter.notifyDataSetChanged();
// viewPager.destroyDrawingCache();
/* if(ichektab>0){
// viewPager.setCurrentItem(ichektab);
viewPager.setCurrentItem(ichektab);
//actionBar.removeTabAt(TAB_CURRENT);
}*/
}
}
}
});
My Adapter
public class TabsPageAdapter extends FragmentStatePagerAdapter {
private long baseId = 0;
public TabsPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return new MyWebBrowser();
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public int getCount() {
return COUNT_TAB;
}
}
} please kindly suggest me about this error. Thanks
Use this code in remove button:
before removing tab u making COUNT_TAB to reduce so it troughs NullPointerException
also in addtab button increment the count tab in last
btCloseTab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(COUNT_TAB>0) {
if(TAB_CURRENT>=0 && PAGE_CURRENT>=0) {
System.out.println("CLOSE PAGE AND TAB : "+TAB_CURRENT+" AND "+PAGE_CURRENT);
// int ichektab=TAB_CURRENT-1;
int ip=viewPager.getCurrentItem();
viewPager.removeViewAt(ip);
actionBar.removeTabAt(TAB_CURRENT);
//actionBar.removeTab(iTab);
mAdapter.notifyDataSetChanged();
COUNT_TAB -=1;
// viewPager.destroyDrawingCache();
/* if(ichektab>0){
// viewPager.setCurrentItem(ichektab);
viewPager.setCurrentItem(ichektab);
//actionBar.removeTabAt(TAB_CURRENT);
}*/
}
}
}
});
View Pager page change listener
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
#Override
public void onPageScrolled(int i, float v, int i2) {
NUM_TABS=i+1;
btTabCount.setText(""+NUM_TABS);
}
#Override
public void onPageSelected(int i) {
getActionBar().setSelectedNavigationItem(i);
PAGE_CURRENT=i;
System.out.println("CURRENT PAGE DISCRIPTION : "+PAGE_CURRENT);
mAdapter.notifyDataSetChanged();
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
Tab listener methods
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mAdapter.notifyDataSetChanged();
viewPager.setCurrentItem(tab.getPosition());
TAB_CURRENT=tab.getPosition();
iTab=tab;
System.out.println("CURRENT TAB DISCRIPTION : "+TAB_CURRENT);
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//viewPager.setCurrentItem(tab.getPosition());
}
Finally i solve error.
Th reason is viewpager pager screen limit. By default there viewpager have Two page limit, but i write first time i used this code
viewPager.setOffscreenPageLimit(COUNT_TAB);
And i solved it
viewPager.setOffscreenPageLimit(500);
and if use COUNT_TAB then add button code is
btNewtab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// COUNT_TAB+=1;
// viewPager.setAdapter(mAdapter);
actionBar.addTab(actionBar.newTab().setText("New Tabs")
.setTabListener(HomeActivity.this));
COUNT_TAB+=1;
mAdapter.notifyDataSetChanged();
viewPager.setOffscreenPageLimit(COUNT_TAB);
}
});
Put pager limit after notifydatasetchanged.
Hope you guys understand.
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
}
});