我已为我的问题准备了a simple test project at GitHub。
它基于the well-known Navigation Drawer Example by Google - 由右侧抽屉扩展,在ListView中与音乐相关的操作:
这些操作都在 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>
以下是从资源中读取它们的代码(这很有效):
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();
我的问题是:
从ListView的监听器中的onItemClick
方法 - 如何在Fragment中调用相应的方法?
我只看到丑陋的方式,就像这里使用硬编码switch
声明一样:
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);
}
});
这很难看,因为我必须跟踪当前活动的Fragment,然后使用包装器方法最终在我的自定义View上调用方法。
如何改善这种情况,我是否可以在自定义视图中使用BroadcastReceiver?
我希望,我可以在我的资源中定义 icon + title + action ,然后只使用它们。
更新:
目前我正在使用BroadcastReceiver
,但想知道它是否是一个合适的解决方案(或者可能太重了?)。
在我的片段中,我打电话:
@Override
public void onResume() {
super.onResume();
myView.register();
}
@Override
public void onPause() {
super.onPause();
myView.unregister();
}
在我的自定义视图中,我有:
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);
}
我希望有一些&#34;上下文菜单&#34;片段 - 定义标题+图标+动作 - 然后可以被右侧Drawer中的actions-ListView使用...
public void unregister() {
getContext().unregisterReceiver(mMessageReceiver);
}
答案 0 :(得分:0)
广播适用于我的基于DrawerLayout的应用,但为了更好的安全性和效率,我已切换到LocalBroadcastManager -
MainActivity.java(右侧动作抽屉菜单发送广播):
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(活动片段接收应用内部广播):
@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);
}