-1

I have checked the Navigation drawer: How do I set the selected item at startup? post. In that post the navigation drawer has a menuview, and my navigation drawer has a listview.

I have a navigation drawer, and a list init. I have setup my list whenever an item chosen, the item gets in a pressed state and highlights the chosen list item. This function works well and looks as following.

activityBinding.list.setOnItemClickListener((parent, view, position, id) -> {
                parent.getChildAt(position).setPressed(true);
});

I have an xml, which is set on the listviev textcolor attribute. The xml looks like that:

<item android:state_pressed="true"
    android:color="#ffffffff"/> 
<item android:state_focused="true"
    android:color="#ff0000ff"/> 
<item android:color="#00000000"/>  

However I would like to setup the first item of the list pressed as default, before opening the drawer. My question is: How to get to an item of a list an set it to pressed outside of the setOnItemListener?

Community
  • 1
  • 1
XXX
  • 131
  • 1
  • 2
  • 8
  • If would help if you can post the XML layout and Java code that you have worked till now. Otherwise this is very vague. – Anurag Feb 25 '17 at 14:18
  • Possible duplicate of [Navigation drawer: How do I set the selected item at startup?](http://stackoverflow.com/questions/31233279/navigation-drawer-how-do-i-set-the-selected-item-at-startup) – Divers Feb 25 '17 at 14:55
  • @Divers I have seen that post, I have a list in my navigation drawer and not a menu. – XXX Feb 25 '17 at 14:57

2 Answers2

0

activityBinding.list.getChildAt(0).setPressed(true)

But you should use NavigationView and not ListView.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • I have tried that and I get a nullpointer exception: Attempt to invoke virtual method 'void android.view.View.setPressed(boolean)' on a null object reference. I also followed: https://developer.android.com/training/implementing-navigation/nav-drawer.html, which has a listview in the navigationdrawer. – XXX Feb 25 '17 at 15:09
  • Then please fix it as explained there - http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it . That problem is not related to your question. – Divers Feb 25 '17 at 15:11
  • I found the solution which detailed above. Thanks for your help. – XXX Feb 25 '17 at 16:22
-1

I found a solution for my problem.

activityBinding.list.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                activityBinding.list.getChildAt(0).setPressed(true);
                activityBinding.list.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });
XXX
  • 131
  • 1
  • 2
  • 8