Skip to content
This repository has been archived by the owner on Jul 10, 2020. It is now read-only.

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jahirfiquitiva committed Jan 24, 2018
1 parent 9893c2a commit cde8a62
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Expand All @@ -33,6 +36,8 @@

public class MainActivity extends AppCompatActivity {

private FABsMenu menu;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -54,7 +59,7 @@ public void onClick(View view) {
rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
rv.setAdapter(new SampleAdapter(this));

final FABsMenu menu = findViewById(R.id.fabs_menu);
menu = findViewById(R.id.fabs_menu);
menu.attachToRecyclerView(rv);
menu.setMenuListener(new FABsMenuListener() {
// You don't need to override all methods. Just the ones you want.
Expand Down Expand Up @@ -102,10 +107,14 @@ public void onClick(View view) {
}
});

Log.d("FABsMenu", "Current buttons count: " + menu.getButtonsCount());

// Removes a button
TitleFAB toRemove = findViewById(R.id.to_remove);
menu.removeButton(toRemove);

Log.d("FABsMenu", "Buttons count after removal: " + menu.getButtonsCount());

// Adds a button to the bottom
final TitleFAB toAdd = new TitleFAB(this);
toAdd.setTitle("A new added fab");
Expand All @@ -119,6 +128,37 @@ public void onClick(View view) {
}
});
menu.addButton(toAdd);

Log.d("FABsMenu", "Buttons count after addition: " + menu.getButtonsCount());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_options, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.add) {
final TitleFAB toAdd = new TitleFAB(this);
toAdd.setTitle("A new added fab");
toAdd.setBackgroundColor(Color.parseColor("#ff5722"));
toAdd.setTitleClickEnabled(true);
toAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showToast("You pressed the new button");
toAdd.hide();
}
});
menu.addButton(toAdd);
Log.d("FABsMenu", "Buttons count after addition: " + menu.getButtonsCount());
} else if (item.getItemId() == R.id.remove) {
menu.removeAllButtons();
Log.d("FABsMenu", "Buttons count after removing all: " + menu.getButtonsCount());
}
return super.onOptionsItemSelected(item);
}

private void showToast(String text) {
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/res/menu/menu_options.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2018. Jahir Fiquitiva
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/add"
android:title="Add"
android:orderInCategory="100"
app:showAsAction="ifRoom"
tools:ignore="HardcodedText"/>
<item
android:id="@+id/remove"
android:title="Remove All"
android:orderInCategory="200"
app:showAsAction="ifRoom"
tools:ignore="HardcodedText"/>
</menu>
47 changes: 29 additions & 18 deletions library/src/main/java/jahirfiquitiva/libs/fabsmenu/FABsMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,11 @@ public void addButton(TitleFAB button, int index) throws IllegalArgumentExceptio
throw new IllegalArgumentException("A floating action buttons menu should have no " +
"more than six options.");
addView(button, index);
buttonsCount++;
buttonsCount += 1;
createLabels();
if (buttonsCount > 1 && getVisibility() != View.VISIBLE) {
show(false);
}
if (buttonsCount < 3)
Log.w("FABsMenu", "A floating action buttons menu should have at least three options");
}
Expand All @@ -245,39 +248,47 @@ public void addButton(TitleFAB button) throws IllegalArgumentException {
addButton(button, buttonsCount - 1);
}

public void removeButton(int index) throws IndexOutOfBoundsException {
View button = getChildAt(index);
if (button != null) {
try {
String title = ((TitleFAB) button).getTitle();
if (button.equals(menuButton) || title == null || title.length() <= 0 ||
button.getTag(R.id.fab_label) != null)
return;

removeButton((TitleFAB) button);
} catch (Exception ignored) {
@SuppressWarnings("UnnecessaryReturnStatement")
private void removeButtonInternal(int index, boolean throwException)
throws NullPointerException, IllegalArgumentException {
View child = getChildAt(index);
if (child != null) {
if (child instanceof MenuFAB) {
return;
} else if (child instanceof TitleFAB) {
removeButton((TitleFAB) child);
} else {
if (throwException)
throw new IllegalArgumentException(
"The view you want to remove is not an instance of TitleFAB");
}
} else {
throw new IndexOutOfBoundsException(
"The index of the button you try to remove is invalid");
if (throwException)
throw new NullPointerException("The button you want to remove does not exists");
}
}

@SuppressLint("ResourceType")
public void removeButton(int index) throws IndexOutOfBoundsException {
removeButtonInternal(index, false);
}

public void removeButton(TitleFAB button) {
try {
button.hide();
button.setTag(R.id.fab_label, null);
removeView(button.getLabelView());
removeView(button);
buttonsCount--;
button.setTag(R.id.fab_label, null);
buttonsCount -= 1;
if (buttonsCount <= 1) hide();
} catch (Exception e) {
e.printStackTrace();
}
}

public void removeAllButtons() {
for (int i = 0; i < buttonsCount; i++) {
removeButton(i);
for (int i = 0; i <= getChildCount(); i++) {
removeButton(0);
}
}

Expand Down

0 comments on commit cde8a62

Please sign in to comment.