我如何创建像whatsapp这样的操作栏,在片段更改时更改按钮。
示例:
当我进入“Conver ...”标签时,我有一个气球图标,当我滑动到“Chamadas”标签时,我将图标更改为拨号图标
我怎样才能在android(java)上以编程方式编写这个?
我的抽屉活动
@EActivity(R.layout.drawer)
@OptionsMenu(R.menu.main)
public class DrawerActivity extends Activity {
private Fragment fragment;
@StringArrayRes(R.array.wigo_items)
String[] mWigoItems;
@ViewById(R.id.drawer_layout)
DrawerLayout mDrawerLayout;
@ViewById(R.id.left_drawer)
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle = "";
@Bean
MoodAdapter adapterKarma;
@AfterViews
public void afterViews() {
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(true);
mTitle = mDrawerTitle = getTitle();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer,
R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mWigoItems));
// adapterKarma.moods.addAll(setListKarma());
// mDrawerList.setAdapter(adapterKarma);
// mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerListItemClicked(0);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content
// view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// if((fragment.getClass() != TweetList_.class &&
// TweetList_.class.isAssignableFrom(fragment.getClass()))){
// Handle your other action bar items...
switch (item.getItemId()) {
case R.id.action_search:
case R.id.action_bookmark:
case R.id.action_calendar:
case R.id.action_sobre:
Toast.makeText(this, "Not implemented", Toast.LENGTH_LONG).show();
}
// }
return super.onOptionsItemSelected(item);
}
@ItemClick(R.id.left_drawer)
void mDrawerListItemClicked(int position) {
fragment = null;
switch (position) {
case 0:
fragment = new TweetFragment_();
break;
case 1:
fragment = new ConfigFragment_();
break;
case 2:
fragment = new AboutFragment_();
break;
default:
break;
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
//setTitle(mWigoItems[position]);
setTitle("");
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
public ArrayList<Mood> setListKarma() {
// Now i have taken static values by loop.
// For further inhancement we can take data by webservice / json / xml;
ArrayList<Mood> CustomListViewValuesArr = new ArrayList<Mood>();
CustomListViewValuesArr
.add(new Mood(1, "Apaixonado", BitmapFactory.decodeResource(getResources(), R.drawable.apaixonado)));
return CustomListViewValuesArr;
}
}
布局
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:1)
第1步:让你的片段在setHasOptionsMenu(true)
中呼叫onCreate()
。
第2步:在片段上实施onCreateOptionsMenu()
和onOptionsItemSelected()
。
步骤3:将特定于片段的所有操作项移出活动并进入片段。
片段中定义的操作栏项目只有在这些片段可见时才会出现在操作栏中。因此,当用户切换选项卡时,操作栏的内容将会改变。