vikramkakkar / SublimeNavigationView

...is a complete rewrite of NavigationView (from Design Support library) that enables - usage of Checkboxes, Switches & Badges as menu items - XML definition - Parcelable menus - collapsible/expandable menu-groups - multiple menus - custom styling.
Apache License 2.0
309 stars 42 forks source link

addTextItem not really adding all #6

Closed defaultbr closed 8 years ago

defaultbr commented 8 years ago

if you do this:

   for(int i = 1; i<= 10;i++) {        
         navdrawer_menu.addTextItem(navdrawer.getMenu().getGroup(R.id.custom_category).getGroupId(), i+"", null, true).setIcon(R.drawable.testimage).setId(i).setVisible(true);

        }

and than do this:

 List<SublimeBaseMenuItem> items=navdrawer.getMenu().getVisibleItems();
        for(int i = 0; i < items.size(); i++) {
            if(items.get(i).getGroupId() == R.id.custom_category) {
                Log.d("Visible", items.get(i).getTitle() + "--->" + items.get(i).getItemType());

                           }
        }

you will see in the Visible that not all the items are added to the mItens and it return in a "aleatory" way

POSSBLE SOLUTION https://github.com/vikramkakkar/SublimeNavigationView/issues/6#issuecomment-179823612

defaultbr commented 8 years ago

i solved it making this method PUBLIC

protected List<SublimeBaseMenuItem> getItemsForGroup(int groupId) {
        ArrayList<SublimeBaseMenuItem> groupItems = new ArrayList<>();

        final int N = mItems.size();

        for (int i = 0; i < N; i++) {
            SublimeBaseMenuItem item = mItems.get(i);
            if (item.getGroupId() == groupId) {
                groupItems.add(item);
            }
        }

        return groupItems;
    }

so now i have all items from category that i want, why you made this PROTECTED ? wonderfull method =x

anyway, getVisibleItems are not accurate

vikramkakkar commented 8 years ago

I tested your workflow a few times, and I consistently got the following log output:

02-04 00:22:44.143 19967-19967/com.appeaser.playground D/Visible: 1--->TEXT
02-04 00:22:44.143 19967-19967/com.appeaser.playground D/Visible: 2--->TEXT
02-04 00:22:44.143 19967-19967/com.appeaser.playground D/Visible: 3--->TEXT
02-04 00:22:44.143 19967-19967/com.appeaser.playground D/Visible: 4--->TEXT
02-04 00:22:44.143 19967-19967/com.appeaser.playground D/Visible: 5--->TEXT
02-04 00:22:44.143 19967-19967/com.appeaser.playground D/Visible: 6--->TEXT
02-04 00:22:44.143 19967-19967/com.appeaser.playground D/Visible: 7--->TEXT
02-04 00:22:44.143 19967-19967/com.appeaser.playground D/Visible: 8--->TEXT
02-04 00:22:44.143 19967-19967/com.appeaser.playground D/Visible: 9--->TEXT
02-04 00:22:44.143 19967-19967/com.appeaser.playground D/Visible: 10--->TEXT

The only change I made was remove setId() from this statement:

navdrawer_menu.addTextItem(navdrawer.getMenu()
    .getGroup(R.id.custom_category).getGroupId(), i+"", null, true)
    .setIcon(R.drawable.testimage)
    // --------> .setId(i)
    .setVisible(true);

So, setId(int) is a method you defined, correct? Does it check if such an ID already exists in the menu?

I did not think that public access to getItemsForGroup(int) will be required. I will make it public in the next update. I will also add a few more traversal methods.

By the way, getVisibleItems(int) only checks for one condition - if (item.isVisible()). So, I don't see why its giving you wrong results.

defaultbr commented 8 years ago

POSSBLE SOLUTION https://github.com/vikramkakkar/SublimeNavigationView/issues/6#issuecomment-179823612

strange, look my result:

02-04 07:54:27.458 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.468 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.567 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.568 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.568 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.569 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.570 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.570 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.571 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.571 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.571 15784-15784/com.leifacil.vademecum D/Items: Adding item Title 1
02-04 07:54:27.571 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.572 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.572 15784-15784/com.leifacil.vademecum D/Items: Adding item Title 2
02-04 07:54:27.572 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.572 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.572 15784-15784/com.leifacil.vademecum D/Items: Adding item Title 3
02-04 07:54:27.572 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.573 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.573 15784-15784/com.leifacil.vademecum D/Items: Adding item Title 4
02-04 07:54:27.573 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.573 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.573 15784-15784/com.leifacil.vademecum D/Items: Adding item Title 5
02-04 07:54:27.573 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.574 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.574 15784-15784/com.leifacil.vademecum D/Items: Adding item Title 6
02-04 07:54:27.574 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.574 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.575 15784-15784/com.leifacil.vademecum D/Items: Adding item Title 7
02-04 07:54:27.575 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.575 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.575 15784-15784/com.leifacil.vademecum D/Items: Adding item Title 8
02-04 07:54:27.575 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.575 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.576 15784-15784/com.leifacil.vademecum D/Items: Adding item Title 9
02-04 07:54:27.576 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.576 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.576 15784-15784/com.leifacil.vademecum D/Items: Adding item Title 10
02-04 07:54:27.576 15784-15784/com.leifacil.vademecum D/Items: Showing item Title Edit Categories -> TEXT
02-04 07:54:27.576 15784-15784/com.leifacil.vademecum D/Items: Showing item Title 1 -> TEXT
02-04 07:54:27.576 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum D/Items: Showing item Title 3 -> TEXT
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum D/Items: Showing item Title 5 -> TEXT
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum D/Items: Showing item Title 7 -> TEXT
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum D/Items: Showing item Title 9 -> TEXT
02-04 07:54:27.578 15784-15784/com.leifacil.vademecum I/SublimeMenu: prepareMenuItems()

so i will have only just this items from getVisibleItems:

02-04 07:54:27.576 15784-15784/com.leifacil.vademecum D/Items: Showing item Title Edit Categories -> TEXT (default one, must exist)
02-04 07:54:27.576 15784-15784/com.leifacil.vademecum D/Items: Showing item Title 1 -> TEXT
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum D/Items: Showing item Title 3 -> TEXT
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum D/Items: Showing item Title 5 -> TEXT
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum D/Items: Showing item Title 7 -> TEXT
02-04 07:54:27.577 15784-15784/com.leifacil.vademecum D/Items: Showing item Title 9 -> TEXT

and the method is just this one:

  for(int i = 1; i<= 10;i++) {
            SublimeBaseMenuItem a = navdrawer_menu.addTextItem(navdrawer.getMenu().getGroup(R.id.group_minhascategorias).getGroupId(), i + "", null, true).setIcon(R.drawable.icon_clips_laranja).setVisible(true);
            Log.d("Items", "Adding item Title " + i);
        }
        List<SublimeBaseMenuItem> items=navdrawer.getMenu().getVisibleItems();
        for(int i = 0; i < items.size(); i++) {

            if(items.get(i).getGroupId() == R.id.group_minhascategorias) {

                if(items.get(i).getItemType().equals(SublimeBaseMenuItem.ItemType.TEXT)) {
                    Log.d("Items", "Showing item Title " + items.get(i).getTitle() + " -> " + items.get(i).getItemType());
                }
            }
        }

but if i use the getMenu().getItemsForGroup(groupID);

it works normally o0

defaultbr commented 8 years ago

the method for testing USING THE getVisibleItems():

 public void adicionarCategorias() {
        for(int i = 1; i<= 10;i++) {
            SublimeBaseMenuItem a = navdrawer.getMenu().addTextItem(navdrawer.getMenu().getGroup(R.id.group_minhascategorias).getGroupId(), i + "", null, true).setIcon(R.drawable.icon_clips_laranja).setVisible(true);
            Log.d("Items", "Adding item Title " + i);
        }
        List<SublimeBaseMenuItem> items=navdrawer.getMenu().getVisibleItems();
        for(int i = 0; i < items.size(); i++) {

            if(items.get(i).getGroupId() == R.id.group_minhascategorias) {

                if(items.get(i).getItemType().equals(SublimeBaseMenuItem.ItemType.TEXT)) {
                    Log.d("Items", "Showing item Title " + items.get(i).getTitle() + " -> " + items.get(i).getItemType());
                }
            }
        }
    }

execute onCreate

02-04 09:18:45.817 19335-19335/? D/Items: Adding item Title 1 02-04 09:18:45.817 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.817 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.817 19335-19335/? D/Items: Adding item Title 2 02-04 09:18:45.817 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.818 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.818 19335-19335/? D/Items: Adding item Title 3 02-04 09:18:45.818 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.818 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.818 19335-19335/? D/Items: Adding item Title 4 02-04 09:18:45.818 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.819 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.819 19335-19335/? D/Items: Adding item Title 5 02-04 09:18:45.819 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.819 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.819 19335-19335/? D/Items: Adding item Title 6 02-04 09:18:45.819 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.820 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.820 19335-19335/? D/Items: Adding item Title 7 02-04 09:18:45.820 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.820 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.821 19335-19335/? D/Items: Adding item Title 8 02-04 09:18:45.821 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.821 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.821 19335-19335/? D/Items: Adding item Title 9 02-04 09:18:45.821 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.822 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.822 19335-19335/? D/Items: Adding item Title 10 02-04 09:18:45.822 19335-19335/? D/Items: Showing item Title Editar Categorias -> TEXT 02-04 09:18:45.822 19335-19335/? D/Items: Showing item Title 1 -> TEXT 02-04 09:18:45.822 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.822 19335-19335/? D/Items: Showing item Title 3 -> TEXT 02-04 09:18:45.822 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.823 19335-19335/? D/Items: Showing item Title 5 -> TEXT 02-04 09:18:45.823 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.823 19335-19335/? D/Items: Showing item Title 7 -> TEXT 02-04 09:18:45.823 19335-19335/? I/SublimeMenu: prepareMenuItems() 02-04 09:18:45.823 19335-19335/? D/Items: Showing item Title 9 -> TEXT

device screen category added items:

categoriaspares

in the log just show the ODD numbers and it just add the EVEN


THE WEIRD THING

as I write this post and was testing, I returned to the the above method and oddly enough, the getVisibleItems is working now -_-

dont know why and what to say =(

defaultbr commented 8 years ago

got the problem again, and i guess i found where is the issue (?)

i must refresh the group list after some items add/delete/edit, so i use this method to delete all items and than add again(from database)

method to remove:

public void removeMyCustomGroup() {
 List<SublimeBaseMenuItem> items=navdrawer.getMenu().getVisibleItems();
        for(int i = 0; i < items.size(); i++) {
            if(items.get(i).getGroupId() == R.id.group_minhascategorias) {
                if(items.get(i).getItemType().equals(SublimeBaseMenuItem.ItemType.TEXT)) {
//                    navdrawer.getMenu().removeItem(items.get(i).getItemId());
                    Log.d("Items", "Showing item Title " + items.get(i).getTitle() + " -> " + items.get(i).getItemType());
                }
            }
        }
}

if i comment the navdrawer.getMenu().removeItem(items.get(i).getItemId()); the Log.d show perfectly CORRECT

but when i remove the comment from the navdrawer.getMenu().removeItem(items.get(i).getItemId());

it get the weird issue(just remove some items, not all), so i think that is there something wrong with the getMenu.removeItem();

BUT if i use the .getItemForGroup(groupID);instead of getVisibleItems();, the function .removeItem(itemID) works perfectly

@vikramkakkar

can you test this function:

    List<SublimeBaseMenuItem> items=navdrawer.getMenu().getGroupItems(R.id.group_minhascategorias);
        for(int i = 0; i < items.size(); i++) {
            if(items.get(i).getGroupId() == R.id.group_minhascategorias) {
                if(items.get(i).getItemType().equals(SublimeBaseMenuItem.ItemType.TEXT)) {
                    Log.d("Items", "Title1 " + items.get(i).getTitle() + " -> " + items.get(i).getItemId());
                                //        navdrawer.getMenu().removeItem(items.get(i).getItemId());

                }
            }
        }

        List<SublimeBaseMenuItem> items2=navdrawer.getMenu().getVisibleItems();
        for(int i = 0; i < items2.size(); i++) {
            if(items2.get(i).getGroupId() == R.id.group_minhascategorias) {
                if(items2.get(i).getItemType().equals(SublimeBaseMenuItem.ItemType.TEXT)) {
                    Log.d("Items", "Title2 " + items2.get(i).getTitle() + " -> " + items2.get(i).getItemId());
                              //          navdrawer.getMenu().removeItem(items2.get(i).getItemId());

                }
            }
        }
    }

you will see that the Log output are the SAME!

but if you remove the comment from navdrawer.getMenu().removeItem(items.get(i).getItemId()); see the result and comment this line again, and in the // navdrawer.getMenu().removeItem(items2.get(i).getItemId()); you will see that the behaviour is different, the getVisibleItems doesnt delete all, but the getItemsForGroup DELETE all

defaultbr commented 8 years ago

or just try this:

//GetVisibleItems
 List<SublimeBaseMenuItem> items=navdrawer.getMenu().getVisibleItems();
        for(int i = 0; i < items.size(); i++) {
            navdrawer.getMenu().removeItem(items.get(i).getItemId());

        }
    }

and some items will not be removed, i dont know why, i tested, the mItens are ok, the returned visibleItens are OK too, but for some reason the removeItem mess the thing (but only with getVisible(), using the getItemsForGroup() works)


SOLUTION

i found the problem, its on onItemsChanged(); its block the updates/removal of items

try this:

//GetVisibleItems
 List<SublimeBaseMenuItem> items=navdrawer.getMenu().getVisibleItems();
        for(int i = 0; i < items.size(); i++) {
            navdrawer.getMenu().removeItem(items.get(i).getItemId());

        }
    }

some items arent removed

now go to SublimeMenu.java

comment these lines

// if (updateChildrenOnMenuViews) {
//            onItemsChanged();
//        }

try again the for, and after it call manually onItemsChanged(need to change the method at SublimeMenu.java to public)

//GetVisibleItems
 List<SublimeBaseMenuItem> items=navdrawer.getMenu().getVisibleItems();
        for(int i = 0; i < items.size(); i++) {
            navdrawer.getMenu().removeItem(items.get(i).getItemId());

        }
    }
        navdrawer.getMenu().onItemsChanged(); //call manually After ALLL the changes, not every change
vikramkakkar commented 8 years ago

I have figured out what the problem is.

If you are able to edit code in the library, change the getVisibleItems() method to:

public ArrayList<SublimeBaseMenuItem> getVisibleItems() {
    // Refresh the visible items
    ArrayList<SublimeBaseMenuItem> visibleItems = new ArrayList<SublimeBaseMenuItem>();

    final int itemsSize = mItems.size();
    SublimeBaseMenuItem item;
    for (int i = 0; i < itemsSize; i++) {
        item = mItems.get(i);
        if (item.isVisible()) visibleItems.add(item);
    }

    return visibleItems;
}

This change should fix the following issue:

getVisibleItems() should return a copy of mVisibleItems. But, at present, it returns mVisibleItems itself. This is a problem because mVisibleItems changes whenever items are added, deleted, made visible, or hid. Let's go through your code:

List<SublimeBaseMenuItem> items = navdrawer.getMenu().getVisibleItems();

This gives us:

{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

For loop:

// 1st iteration
i ==> 0
current state of `items` ==> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
items.size() ==> 10
item removed ==> items.get(0) ==> "1"
current menu items ==> { 2, 3, 4, 5, 6, 7, 8, 9, 10 }

Because items references mVisibleItems, and not a copy of it, our items list changes as well ==> { 2, 3, 4, 5, 6, 7, 8, 9, 10 }

// 2nd iteration
i ==> 1
current state of `items` ==> { 2, 3, 4, 5, 6, 7, 8, 9, 10 }
items.size() ==> 9
item removed ==> items.get(1) ==> "3"        // "3" is removed instead of "2"
current menu items ==> { 2, 4, 5, 6, 7, 8, 9, 10 }

I hope the problem is clear now. I will be fixing this in the next update. By the way, if you need to remove a whole group, you can use navdrawer.getMenu().removeGroup(int). This will remove all group items and call onItemsChanged() at the end.

defaultbr commented 8 years ago

Yep that was the problem, the problem of removing a specific group is that i set the Group Header and the First Item at the .xml file, and onCreate i populate this group from database, if i remove group, i need to create the group, the header and the default item again, anyway solved =)

sorry my spam and bad english

waiting for the new release, if you can think about making some methods public and adding a TAG or a setId, i would appreciate

thank you so much =)

can close ;)