1

Well, I'm trying to change my drawer navigator whenever I change the fragment, I managed to change the ActionBar color and the Background color, but the thing is that with the Background is not enough... I saw that I declare a BackgroundResource with other colors, and when I try to change the color it does not work.

My MainActivity.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- Displaying Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <!-- Displaying Drawer -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"        
        android:listSelector="@drawable/drawer_list_selection"
        android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>

My colors.xml (I've tried to change it manually but I don't know how to do, then I've created as colors as items are on my drawable navigator).

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="list_item_title">#fff</color>
    <color name="list_background">#458A79</color>
    <color name="list_background2">#ffc591</color>
    <color name="list_background3">#ab91ff</color>
    <color name="list_background4">#f784fe</color>
    <color name="list_background5">#91dfff</color>
    <color name="list_background_pressed">#6FA698</color>
    <color name="list_background_pressed2">#ffc591</color>
    <color name="list_background_pressed3">#ab91ff</color>
    <color name="list_background_pressed4">#f784fe</color>
    <color name="list_background_pressed5">#91dfff</color>
    <color name="list_divider">#fff</color>
    <color name="counter_text_bg">#626262</color>
    <color name="counter_text_color">#c5c4c4</color>
</resources>

And the thing that I've tried on my MainActivity.java is

private void displayView(int position) {
        // update the main content with called Fragment
        Fragment fragment = null;
        LlistaGenericaFragment frag = null;
        FragmentTransaction ft = null;

        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#256F5C")));

        switch (position) {
        case 0:
            fragment = new MetallsAlcalinsFragment();

            bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffc591")));
            mDrawerList.setBackground(new ColorDrawable(Color.parseColor("#ffc591"))); //set the background but not the ListView
            mDrawerList.setBackgroundResource(R.color.list_background2); //Don't see any change
            break;

I know I'm doing something wrong, but I don't get what... Could you explain me how can I change this each time I change the fragment?

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148

2 Answers2

3

What you would want to do then is create a drawable selector -> list_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/list_background2" android:state_pressed="true"/>
    <item android:drawable="@color/list_background" />
</selector>

then on your list add this as a selector:

mDrawerList.setSelector(R.drawable.list_selector);

If you want to do this every time something happens in your fragment, you would create an interface:

public iterface TalkToActivity(){
  public void sendChangeEvent(int changeType);
}

in Your fragment create a local variable:

TalkToActivity m_callBack;

Then in your onAttach method of the fragment:

    @Override
public void onAttach(Activity activity) {
    // Call to the Super Class
    super.onAttach(activity);

    // Attempt to Add the Interface
    try {

        m_callBack = (TalkToActivity) activity;

    } catch (ClassCastException e) {

        // Print to LogCat if the Parent Did not implement the interface
        Log.e(FRAGMENT_TAG, "Failed to implement interface in parent.", e);
    }
}

and then in the event that you want to capture and communicate the change:

public void buttonWasPressed(int changeType){
     if(m_callBack != null){
         m_callBack.sendChangeEvent(changeType);
     }
}

Finally, in your activity make your activity 'implents TalkToActivity' which will force you to override the method sendChangeEvent and in this method

@Override
public void sendChangeEvent(int changeType){
 switch(changeType){
    case 0: 
            // Update you UI like above
            // ...
            if(mDrawerList != null){
                mDrawerList.setSelector(R.drawable.list_selector);
            }
       break;
    default: break;
  }

}

NOTE: If you have list_item.xml and an adapter:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/linearLayoutItem"
   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   android:orientation="vertical">
   <TextView
       android:id="@+id/textViewNavItem"
       android:layout_height="wrap_content"
       android:layout_width="match_parent"/>
 </LinearLayout

Then get a reference to the parent item

 LinearLayout linearlayoutItem = (LinearLayout) findViewById(R.id.linearLayoutItem);

and dynamically change the background of this with the above approach...

linearLayoutItem.setBackground(R.drawable.list_selector);

but create 6 different list_selector_num1.xml etc... and change this using the switch statement. but you need the adapter to have a method like

 public void updateViewBackground(Drawable drawable) {}

which will be in your adapter to do this.

For a demo on this communication between Fragments & activities look at:

https://github.com/lt-tibs1984/InterfaceDemo

kandroidj
  • 13,784
  • 5
  • 64
  • 76
0

Fragments talk to the main activity via bundle and onPause()

Passing data between a fragment and its container activity

Community
  • 1
  • 1
Adam Pitt
  • 128
  • 10