私はアンドロイド プロジェクトに取り組んでおり、http://developer.android.com/training/implementing-navigation/nav-drawer.htmlの例を使用して新しいナビゲーション ドロワーを統合しようとしています。
アクションバーのアップボタンにメニューが表示されないことを除いて、ほとんど機能していますが、メインのアクティビティ画面を端から指でスワイプするとメニューが表示されるので、実際のメニューに問題はないことがわかります、それは単なるアクションバーボタンです。
以下はコードです
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
private CharSequence mDrawerTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.left_drawer);
MenuItemAdapter menuAdapter = new MenuItemAdapter(this);
menuAdapter.add(new MenuItem("Hello"));
menuAdapter.add(new MenuItem("World"));
menuAdapter.add(new MenuItem("Parsnips"));
menuAdapter.add(new MenuItem("Turnips"));
mDrawerList.setAdapter(menuAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_closed)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
android.app.Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARGS_PLANET_NUMBER, position);
fragment.setArguments(args);
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title)
{
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public static class PlanetFragment extends android.app.Fragment
{
public static final String ARGS_PLANET_NUMBER = "planet_number";
public PlanetFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflator.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARGS_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
//((TextView)rootView.findViewById(R.id.fragment_text_view)).setText(planet);
getActivity().setTitle("Planet: " + planet);
return rootView;
}
}
private class MenuItem
{
public String menuName;
public MenuItem(String menuName)
{
this.menuName = menuName;
}
}
public class MenuItemAdapter extends ArrayAdapter<MenuItem>
{
public MenuItemAdapter (Context context)
{
super(context,0);
}
public View getView (int position, View convertView, ViewGroup parent)
{
convertView = null;
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_menu_item, null);
}
/*TextView title = (TextView)convertView.findViewById(R.id.row_title);
title.setText(getItem(position).menuName);
title.setVisibility(View.VISIBLE);*/
EditText title = (EditText)convertView.findViewById(R.id.row_edittext);
title.setHint(getItem(position).menuName);
title.setVisibility(View.VISIBLE);
return convertView;
}
}
}
ご協力いただきありがとうございます。