Skip to content

Commit

Permalink
Fab customizing
Browse files Browse the repository at this point in the history
  • Loading branch information
CullyCross committed Nov 5, 2015
1 parent f9c0861 commit e2d2855
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
Expand All @@ -15,6 +14,7 @@
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnPageChange;
import me.cullycross.test4tabs.R;
import me.cullycross.test4tabs.fragments.ContactsFragment;
import me.cullycross.test4tabs.fragments.DatabaseFragment;
Expand All @@ -25,11 +25,17 @@
public class FourTabsActivity extends AppCompatActivity
implements EmailDialogFragment.OnFragmentInteractionListener {

private SectionsPagerAdapter mSectionsPagerAdapter;
public static final int CONTACTS_POS = 0;
public static final int DATABASE_POS = 1;
public static final int PICTURES_POS = 2;
public static final int SINGLE_PICTURE_POS = 3;

@Bind(R.id.toolbar) Toolbar mToolbar;
@Bind(R.id.container) ViewPager mViewPager;
@Bind(R.id.tabs) TabLayout mTabLayout;
@Bind(R.id.fab) FloatingActionButton mFab;

private SectionsPagerAdapter mSectionsPagerAdapter;

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -43,14 +49,8 @@ public class FourTabsActivity extends AppCompatActivity
mViewPager.setAdapter(mSectionsPagerAdapter);
mTabLayout.setupWithViewPager(mViewPager);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.show();
}
});
mFab.setTag(false);
mFab.hide();
}

@Override public void onSendEmail(String recipient, String subject, String body) {
Expand All @@ -66,6 +66,27 @@ public class FourTabsActivity extends AppCompatActivity
}
}

@OnPageChange(R.id.container) public void onPageSelected(int position) {
switch (position) {
case DATABASE_POS:
mFab.setTag(true);
mFab.setVisibility(View.INVISIBLE);
mFab.setImageResource(R.drawable.ic_add);
mFab.show();
break;
case PICTURES_POS:
mFab.setTag(true);
mFab.setVisibility(View.INVISIBLE);
mFab.setImageResource(R.drawable.ic_update);
mFab.show();
break;
default:
mFab.setTag(false);
mFab.hide();
break;
}
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public static final String CONTACTS = "Contacts";
Expand All @@ -80,13 +101,13 @@ public SectionsPagerAdapter(FragmentManager fm) {
@Override public Fragment getItem(int position) {

switch (position) {
case 0:
case CONTACTS_POS:
return ContactsFragment.newInstance();
case 1:
case DATABASE_POS:
return DatabaseFragment.newInstance();
case 2:
case PICTURES_POS:
return PicturesFragment.newInstance();
case 3:
case SINGLE_PICTURE_POS:
return SinglePictureFragment.newInstance();
default:
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* See
* <a href="https://guides.codepath.com/android/Floating-Action-Buttons#using-coordinatorlayout">
* this article</>
* this article</>
*/
public class HidingFabBehavior extends FloatingActionButton.Behavior {

Expand All @@ -38,7 +38,8 @@ public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionBu

if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
child.hide();
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE
&& ((Boolean) child.getTag())) {
child.show();
}
}
Expand Down
Binary file added app/src/main/res/drawable-hdpi/ic_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-hdpi/ic_update.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_update.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_update.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_update.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_four_tabs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"
app:layout_behavior="me.cullycross.test4tabs.utils.HidingFabBehavior"
/>

Expand Down

0 comments on commit e2d2855

Please sign in to comment.