gpt4 book ai didi

android - 切换按钮不适用于抽屉导航

转载 作者:太空狗 更新时间:2023-10-29 15:29:24 27 4
gpt4 key购买 nike

我正在为我的 android 应用程序实现一个抽屉导航。我通过从侧面拖动来使抽屉工作,但是操作栏上的主页/切换按钮不会更改为我想要的图标并且不会响应点击,你调用了其中定义的 onDrawerClosed 和 onDrawerOpened 方法.我的代码如下:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">


<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/loadingFragment"
android:name="com.whostr.clients.android.LoadingFragment" />

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/eventFragment"
android:name="com.whostr.clients.android.EventDisplayFragment"/>

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/emptyFragment"
android:name="com.whostr.clients.android.EmptyView"/>



<!-- Listview to display slider menu -->
<ListView android:id="@+id/left_drawer"
android:layout_width = "240dp"
android:layout_height = "match_parent"
android:layout_gravity = "start"
android:choiceMode = "singleChoice"
android:dividerHeight = "1dp"
android:divider = "@android:color/transparent"
android:background="#111"
/>

在 Activity 中我有以下代码:

    @Override
public void onCreate(Bundle savedDataInstace)
{
super.onCreate(savedDataInstace);
setContentView(R.layout.eventdisplay);

fm = getSupportFragmentManager();
//get fragments by id
fragments[EVENT_VIEW]= fm.findFragmentById(R.id.eventFragment);
fragments[LOADING] = fm.findFragmentById(R.id.loadingFragment);
fragments[EMPTY_VIEW] = fm.findFragmentById(R.id.emptyFragment);


menuList = getResources().getStringArray(R.array.menuArray);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawwer_open, R.string.drawer_closed)
{
/*
* Called when a drawer has settled in a completely closed state
*/
public void onDrawerClosed(View view)
{
Log.d("drawerToggle", "Drawer closed");
super.onDrawerClosed(view);
getActionBar().setTitle(R.string.app_name);
invalidateOptionsMenu(); //Creates call to onPrepareOptionsMenu()
}

/*
* Called when a drawer has settled in a completely open state
*/
public void onDrawerOpened(View drawerView)
{
Log.d("drawerToggle", "Drawer opened");
super.onDrawerOpened(drawerView);
getActionBar().setTitle("NavigationDrawer");
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

//Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, menuList));
mDrawerList.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
Log.d("navigationDrawer", "Clicked on an item");
}

});


FragmentTransaction transaction = fm.beginTransaction();

//Hide the event view
transaction.hide(fragments[EVENT_VIEW]);
transaction.hide(fragments[EMPTY_VIEW]);
transaction.commit();

AsyncTask<String, Void, EventResponseCollection> task = new GetEventsNearByAsyncTask().execute();
try {
if(task != null)
events = task.get().getItems();

} catch (InterruptedException | ExecutionException e) {
Log.d("viewEvent", "Error when retrieving task details " + e.getMessage());
}

if(events == null)
{
//show create your own event fragment
changeFragment(EMPTY_VIEW);
}
else
{
Log.d("viewEvent", "I have " + events.size());
//show proper fragment with the first event displayed
currentEvent = events.get(eventIndex);
((EventDisplayFragment) fragments[EVENT_VIEW]).updateFragment(currentEvent);
changeFragment(EVENT_VIEW);
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
Log.d("onOptionsItemSelected", "In the on item select");
//Pass the event to ActionBarDrawerToggle, if it returns
//true, then it has handled the app icon touch event
if(mDrawerToggle.onOptionsItemSelected(item))
{
Log.d("onOptionsItemSelected", "Clicked on action bar item");
return true;
}

//Handle your other action bar items...

return super.onOptionsItemSelected(item);
}

有人对我能做什么有什么建议吗?我已经尝试了我在网上找到的所有解决方案来完成这项工作,但显然我运气不好,似乎没有任何效果:(

最佳答案

你必须在你的 Activity 中添加这样的东西

@Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
return drawerToggle.onOptionsItemSelected(item);
}

关于android - 切换按钮不适用于抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23273275/

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