Search code examples
androidandroid-fragmentsandroid-listviewandroid-resourcesandroid-broadcast

DrawerLayout with ListView representing actions - how to call action methods on a View?


I have prepared a simple test project at GitHub for my question.

It is based on the well-known Navigation Drawer Example by Google - extended by a right-side Drawer with music-related actions in a ListView:

screenshot

The actions are all defined in res/values/strings.xml:

<string-array name="music_actions">
    <item>Play</item>
    <item>Pause</item>
    <item>Stop</item>
    <item>Shuffle</item>
</string-array>

<integer-array name="music_icons">
    <item>@drawable/ic_play_arrow_black_24dp</item>
    <item>@drawable/ic_pause_black_24dp</item>
    <item>@drawable/ic_stop_black_24dp</item>
    <item>@drawable/ic_shuffle_black_24dp</item>
</integer-array>

And here is the code reading them from resources (this works well):

public class MainActivity extends AppCompatActivity {
    private String[] mActions;
    private int[] mIcons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mActions = getResources().getStringArray(R.array.music_actions);

        TypedArray ta = getResources().obtainTypedArray(R.array.music_icons);
        mIcons = new int[ta.length()];
        for (int i = 0; i < mIcons.length; i++)
                mIcons[i] = ta.getResourceId(i, R.drawable.ic_menu_black_24dp);
        ta.recycle();

My problem is:

From the onItemClick method in the listener for the ListView - how to call the corresponding method in the Fragment?

I only see ugly ways to do it, like here with a hardcoded switch statement:

    mRightDrawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mActions) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView view = (TextView) super.getView(position, convertView, parent);
            view.setCompoundDrawablesWithIntrinsicBounds(mIcons[position], 0, 0, 0);
            return view;
        }
    });
    mRightDrawer.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (mActiveFragment instanceof GameFragment) {
                GameFragment fragment = (GameFragment) mActiveFragment;
                switch (position) {
                case 0:
                    fragment.playMusic(); // and then this calls myView.playMusic();
                    break;
                case 1:
                    fragment.pauseMusic(); // calls myView.pauseMusic();
                    break;
                }
            }

            mDrawerLayout.closeDrawer(mRightDrawer);
        }
    });

It is ugly, because I have to track my currently active Fragment and then use wrapper methods to finally call the methods on my custom View.

How to improve this situation, should I maybe use BroadcastReceiver in my custom View?

I wish, I could define icon + title + action in my resources and then just use them.

UPDATE:

For now I am using BroadcastReceiver, but wonder if it's an apropriate solution (or maybe too heavy-weight?).

In my Fragment I call:

@Override
public void onResume() {
    super.onResume();

    myView.register();
}

@Override
public void onPause() {
    super.onPause();

    myView.unregister();
}

And in my custom View I have:

public class MyView extends View {

    public final static String ACTION_PLAY    = "de.afarber.myapp.play";
    public final static String ACTION_PAUSE   = "de.afarber.myapp.pause";
    public final static String ACTION_STOP    = "de.afarber.myapp.stop";
    public final static String ACTION_SHUFFLE = "de.afarber.myapp.shuffle";

   private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              String action = intent.getAction();
              if (ACTION_PLAY.equals(action))
                  playMusic();
              else if (ACTION_PAUSE.equals(action))
                  pauseMusic();
              else if (ACTION_STOP.equals(action))
                  stopMusic();
              else if (ACTION_SHUFFLE.equals(action))
                  shuffleMusic();
          }
    };

    public void register() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_PLAY);
        filter.addAction(ACTION_PAUSE);
        filter.addAction(ACTION_STOP);
        filter.addAction(ACTION_SHUFFLE);
        getContext().registerReceiver(mMessageReceiver, filter);
    }

I would prefer to have some kind of "context menu" for Fragments - defining title + icon + action - which then could be used by the actions-ListView in the right Drawer...

    public void unregister() {
        getContext().unregisterReceiver(mMessageReceiver);
    }

Solution

  • Broadcasts work well for my DrawerLayout based apps, but for better security and efficiency I have switched to LocalBroadcastManager -

    MainActivity.java (right action drawer menu sends broadcasts):

        mRightDrawer.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String action = mActions[position];
                Intent intent = new Intent(action);
                //intent.putExtra("message", "data");
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
                mDrawerLayout.closeDrawer(mRightDrawer);
            }
        });
    

    PlanetFragment.java (the active fragment receives app-internal broadcasts):

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ...
        mManager = LocalBroadcastManager.getInstance(getActivity());
    
        mFilter = new IntentFilter();
        mFilter.addAction(ACTION_PLAY);
        mFilter.addAction(ACTION_PAUSE);
        mFilter.addAction(ACTION_STOP);
        mFilter.addAction(ACTION_SHUFFLE);
        ...
    }
    
    @Override
    public void onResume() {
        super.onResume();
        mManager.registerReceiver(mMessageReceiver, mFilter);
    }
    
    @Override
    public void onPause() {
        super.onPause();
        mManager.unregisterReceiver(mMessageReceiver);
    }