Reputation: 31
I am trying to load the sharedFab(FloatingActionButton) with ViewPager. But it is not working when I load for first time (Before Page Change).
viewPagerMainAct.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@SuppressLint("SetTextI18n")
@Override
public void onPageSelected(int position) {
switch (position){
case 0 :
sharedFab.setImageDrawable(getDrawable(R.drawable.ic_medical));
sharedFab.show();
sharedFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "New Chat", Toast.LENGTH_SHORT).show();
}
});
break;
case 1:
sharedFab.setImageDrawable(getDrawable(R.drawable.ic_sticky_notes));
sharedFab.show();
sharedFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "New Post", Toast.LENGTH_SHORT).show();
}
});
break;
case 2:
sharedFab.hide();
break;
default:
sharedFab.hide();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Upvotes: 0
Views: 219
Reputation: 31
I totally agree that ur answers are correct but I am using a single FAB with two viewpager fragments and different work. And if it not loading at initialization then what we do?
Upvotes: 0
Reputation: 521
You use addOnPageChangeListener it wait for your request before do something. So, most easy way, it's just add this sharedFab.setImageDrawable(getDrawable(R.drawable.ic_medical)) before viewPagerMainAct.
Second way it add picture in xml, like default picture. And then change it in changeListener.
Upvotes: 1
Reputation: 1292
Insert this line of code into the onCreate.
sharedFab.setImageDrawable(getDrawable(R.drawable.ic_medical));
sharedFab.show();
The first time you run the app, you don't put photos inside the button. But in the first click, this photo is placed
Upvotes: 0