Fixed FAB

This commit is contained in:
Christian Basler 2017-08-10 00:40:44 +02:00
parent ec3009a257
commit e2aa0e8b1d
6 changed files with 95 additions and 106 deletions

View File

@ -75,7 +75,7 @@ dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.5.0@aar' compile 'com.journeyapps:zxing-android-embedded:3.5.0@aar'
compile 'com.google.zxing:core:3.3.0' compile 'com.google.zxing:core:3.3.0'
compile 'com.github.kobakei:MaterialFabSpeedDial:1.1.4' compile 'com.github.kobakei:MaterialFabSpeedDial:1.1.5'
compile 'com.github.amlcurran.showcaseview:library:5.4.3' compile 'com.github.amlcurran.showcaseview:library:5.4.3'
compile('com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.10.4@aar') { compile('com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.10.4@aar') {
transitive = true transitive = true

View File

@ -91,6 +91,7 @@ public class AddressListFragment extends AbstractItemListFragment<Void, Bitmessa
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
initFab((MainActivity) getActivity());
updateList(); updateList();
} }
@ -117,13 +118,9 @@ public class AddressListFragment extends AbstractItemListFragment<Void, Bitmessa
}.execute(); }.execute();
} }
@Override private void initFab(MainActivity activity){
public void onAttach(Context ctx) {
super.onAttach(ctx);
if (ctx instanceof MainActivity) {
MainActivity activity = (MainActivity) ctx;
activity.updateTitle(getString(R.string.contacts_and_subscriptions)); activity.updateTitle(getString(R.string.contacts_and_subscriptions));
FabSpeedDialMenu menu = new FabSpeedDialMenu(ctx); FabSpeedDialMenu menu = new FabSpeedDialMenu(activity);
menu.add(R.string.scan_qr_code).setIcon(R.drawable.ic_action_qr_code); menu.add(R.string.scan_qr_code).setIcon(R.drawable.ic_action_qr_code);
menu.add(R.string.create_contact).setIcon(R.drawable.ic_action_create_contact); menu.add(R.string.create_contact).setIcon(R.drawable.ic_action_create_contact);
FabUtils.initFab(activity, R.drawable.ic_action_add_contact, menu) FabUtils.initFab(activity, R.drawable.ic_action_add_contact, menu)
@ -146,7 +143,6 @@ public class AddressListFragment extends AbstractItemListFragment<Void, Bitmessa
} }
}); });
} }
}
@Nullable @Nullable
@Override @Override

View File

@ -96,6 +96,7 @@ public class MessageListFragment extends Fragment implements ListHolder<Label> {
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
MainActivity activity = (MainActivity) getActivity(); MainActivity activity = (MainActivity) getActivity();
initFab(activity);
messageRepo = Singleton.getMessageRepository(activity); messageRepo = Singleton.getMessageRepository(activity);
if (backStack.isEmpty()) { if (backStack.isEmpty()) {
@ -239,13 +240,11 @@ public class MessageListFragment extends Fragment implements ListHolder<Label> {
return rootView; return rootView;
} }
@Override private void initFab(MainActivity context){
public void onAttach(Context context) {
if (context instanceof MainActivity) {
FabSpeedDialMenu menu = new FabSpeedDialMenu(context); FabSpeedDialMenu menu = new FabSpeedDialMenu(context);
menu.add(R.string.broadcast).setIcon(R.drawable.ic_action_broadcast); menu.add(R.string.broadcast).setIcon(R.drawable.ic_action_broadcast);
menu.add(R.string.personal_message).setIcon(R.drawable.ic_action_personal); menu.add(R.string.personal_message).setIcon(R.drawable.ic_action_personal);
FabUtils.initFab((MainActivity) context, R.drawable.ic_action_compose_message, menu) FabUtils.initFab(context, R.drawable.ic_action_compose_message, menu)
.addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() { .addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() {
@Override @Override
public void onMenuItemClick(FloatingActionButton floatingActionButton, @Nullable TextView textView, int itemId) { public void onMenuItemClick(FloatingActionButton floatingActionButton, @Nullable TextView textView, int itemId) {
@ -258,13 +257,13 @@ public class MessageListFragment extends Fragment implements ListHolder<Label> {
case 1: { case 1: {
Intent intent = new Intent(getActivity(), ComposeMessageActivity.class); Intent intent = new Intent(getActivity(), ComposeMessageActivity.class);
intent.putExtra(EXTRA_IDENTITY, identity); intent.putExtra(EXTRA_IDENTITY, identity);
intent.putExtra(EXTRA_BROADCAST, true);
startActivity(intent); startActivity(intent);
break; break;
} }
case 2: { case 2: {
Intent intent = new Intent(getActivity(), ComposeMessageActivity.class); Intent intent = new Intent(getActivity(), ComposeMessageActivity.class);
intent.putExtra(EXTRA_IDENTITY, identity); intent.putExtra(EXTRA_IDENTITY, identity);
intent.putExtra(EXTRA_BROADCAST, true);
startActivity(intent); startActivity(intent);
break; break;
} }
@ -275,8 +274,6 @@ public class MessageListFragment extends Fragment implements ListHolder<Label> {
} }
}); });
} }
super.onAttach(context);
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {

View File

@ -1,35 +0,0 @@
package ch.dissem.apps.abit.util;
import android.support.annotation.DrawableRes;
import android.support.design.widget.FloatingActionButton;
import ch.dissem.apps.abit.MainActivity;
import ch.dissem.apps.abit.R;
import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
import io.github.kobakei.materialfabspeeddial.FabSpeedDialMenu;
/**
* Utilities to work with the common floating action button in the main activity
*/
public class FabUtils {
public static FabSpeedDial initFab(MainActivity activity, final @DrawableRes int drawableRes, FabSpeedDialMenu menu) {
FabSpeedDial fab = activity.getFloatingActionButton();
fab.show();
fab.closeMenu();
final FloatingActionButton mainFab = fab.getMainFab();
mainFab.setImageResource(drawableRes);
fab.setMenu(menu);
fab.addOnStateChangeListener(new FabSpeedDial.OnStateChangeListener() {
@Override
public void onStateChange(boolean isOpened) {
if (isOpened) {
// It will be turned 45 degrees, which makes an x out of the +
mainFab.setImageResource(R.drawable.ic_action_add);
} else {
mainFab.setImageResource(drawableRes);
}
}
});
return fab;
}
}

View File

@ -0,0 +1,34 @@
package ch.dissem.apps.abit.util
import android.support.annotation.DrawableRes
import android.support.design.widget.FloatingActionButton
import ch.dissem.apps.abit.MainActivity
import ch.dissem.apps.abit.R
import io.github.kobakei.materialfabspeeddial.FabSpeedDial
import io.github.kobakei.materialfabspeeddial.FabSpeedDialMenu
/**
* Utilities to work with the common floating action button in the main activity
*/
object FabUtils {
@JvmStatic
fun initFab(activity: MainActivity, @DrawableRes drawableRes: Int, menu: FabSpeedDialMenu): FabSpeedDial {
val fab = activity.floatingActionButton
fab.removeAllOnMenuItemClickListeners()
fab.show()
fab.closeMenu()
val mainFab = fab.mainFab
mainFab.setImageResource(drawableRes)
fab.setMenu(menu)
fab.addOnStateChangeListener { isOpened ->
if (isOpened) {
// It will be turned 45 degrees, which makes an x out of the +
mainFab.setImageResource(R.drawable.ic_action_add)
} else {
mainFab.setImageResource(drawableRes)
}
}
return fab
}
}

View File

@ -60,13 +60,10 @@
<io.github.kobakei.materialfabspeeddial.FabSpeedDial <io.github.kobakei.materialfabspeeddial.FabSpeedDial
android:id="@+id/fab" android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_gravity="bottom|end" app:fab_useTouchGuard="true"
android:layout_margin="16dp" app:fab_miniFabTextBackground="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent" app:fab_miniFabTextColor="@color/colorPrimaryDarkText" />
app:layout_constraintEnd_toEndOf="parent"
app:elevation="8dp"
app:fab_useTouchGuard="true"/>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>