gpt4 book ai didi

android - 抽屉导航 : DrawerToggle not working, DrawerList 不听

转载 作者:行者123 更新时间:2023-11-29 01:47:58 25 4
gpt4 key购买 nike

我正在尝试使用抽屉导航,我已经按照官方开发者网站上的每一步操作,但仍然有两个问题:

  • DrawerToggle 按下时,什么都不做
  • ListView在抽屉里的item不接收点击事件

其他一切正常(甚至是打开/关闭监听器)。你能帮我找出错误吗?

import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;

public class ActTimerList extends FragmentActivity {


private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_timer_list);

---
configureActionBarAndNavigationDrawer();

retrieveStuff();
}

private void configureActionBarAndNavigationDrawer(){

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
//Some code that works
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {

Log.d("Drawer", "Stuff clicked: " + position);
mDrawerList.setItemChecked(position, true);
getActionBar().setTitle(stuff[position].name);
mDrawerLayout.closeDrawer(mDrawerList);


}
});


getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

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



private void retrieveStuff() {

query.findInBackground(new FindCallback<ParseObject>() { //Parse API to retrieve stuff, async
public void done(List<ParseObject> stuffList, ParseException e) { //Callback for when the query completes
if (e == null) {
//This works, stuff is being loaded and the adapter is set correctly
mDrawerList.setAdapter(new StuffAdapter(context,
R.layout.item_stuff, stuff));
---
}
}
});
}

最佳答案

解释错误:什么不正确?

这是我的一些 fragment :

    mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {

public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}

public void onDrawerOpened(View view) {
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu();
}
};

mDrawerLayout.setDrawerListener(mDrawerToggle);

关于 ListView 监听器:

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

和听众:

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}

和 selectItem():

  private void selectItem(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();


fragmentTrnsaction = fragmentManager.beginTransaction();

switch (position) {

case 0:
<some code>
break;
case 1:
<some code>
break;

case 2:
<some code>
break;

case 3:
<some code>
break;

default:
break;
}


mDrawerList.setItemChecked(position, true);

mDrawerLayout.closeDrawer(mDrawerList);
}

关于android - 抽屉导航 : DrawerToggle not working, DrawerList 不听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20252629/

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