gpt4 book ai didi

android - 抽屉导航操作栏按钮不起作用

转载 作者:IT王子 更新时间:2023-10-28 23:57:32 26 4
gpt4 key购买 nike

我正在开发一个 android 项目,我正在尝试使用 http://developer.android.com/training/implementing-navigation/nav-drawer.html 中的示例集成新的抽屉导航。 .

它主要工作除了一件事,操作栏向上按钮不显示菜单,但如果我从边缘在主 Activity 屏幕上滑动手指,菜单就会出现,所以我知道没有任何问题实际的菜单,它只是操作栏按钮。

下面是代码

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;
}
}
}

感谢您提供的任何帮助。

最佳答案

你忘了实现 onOptionsItemSelected

这就是神奇发生的地方:

@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;
}
// Handle your other action bar items...

return super.onOptionsItemSelected(item);
}

关于android - 抽屉导航操作栏按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16745001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com