0

我正在尝试在我的应用程序中实现抽屉布局。我按照 android 开发者网站上的教程进行操作,一切正常。我只有一个“小”问题:我午餐应用程序,打开抽屉布局并单击列表视图的一个元素,一切都很好,但是当我尝试再次打开抽屉并单击他的一个元素时,我注意到我第一次单击的元素仍然被单击,我无法单击任何其他元素。抽屉只是关闭而不会触发任何 onClick 事件。我的代码是: public class Eventi extends Activity { private DrawerLayout mDrawerLayout; 私有 ListView mDrawerList;私人 ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;

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

    mTitle = mDrawerTitle = getTitle();
    mPlanetTitles = getResources().getStringArray(R.array.menu);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer
    // opens
    // mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
    // GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.listview_item, mPlanetTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
    mDrawerLayout, /* DrawerLayout object */
    R.drawable.ic_launcher, /* nav drawer image to replace 'Up' caret */
    R.string.hello_world, /* "open drawer" description for accessibility */
    R.string.hello_world /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.eventi, menu);
    return super.onCreateOptionsMenu(menu);
}

/* Called whenever we call invalidateOptionsMenu() */

@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);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action buttons
    switch (item.getItemId()) {
    case R.id.action_settings:
        // create intent to perform web search for this planet
        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
        // catch event that there's no activity to handle intent
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Toast.makeText(this, R.string.hello_world, Toast.LENGTH_LONG)
                    .show();
        }
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        System.out.println("c2");

        selectItem(position);
    }
}

private void selectItem(int position) {
    System.out.println("c");
    final String[] classes = getResources().getStringArray(R.array.classi);
    FragmentTransaction tx = getFragmentManager().beginTransaction();
    tx.replace(R.id.drawer_layout,
            Fragment.instantiate(Eventi.this, classes[position]));
    tx.commit();
    /*
     * // update the main content by replacing fragments Fragment fragment =
     * new PlanetFragment(); Bundle args = new Bundle();
     * args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
     * fragment.setArguments(args);
     * 
     * FragmentManager fragmentManager = getFragmentManager();
     * fragmentManager.beginTransaction() .replace(R.id.content_frame,
     * fragment).commit();
     */
    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@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);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

}

我是从开发者网站上拿的,所以我不明白错误出在哪里。有谁能帮助我吗?提前致谢 ;)

4

2 回答 2

2

看看我的代码:

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) {

        switch (position) {

            case 0:

                fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, new MainActivityFragment())
                        .commit();
                break;

            case 1:

                fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, new DbListViewFragment())
                        .commit();
                break;

            case 2:

                fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, new StatisticsFragment())
                        .commit();
                break;

            case 3:

                fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, new CalculatorFragment(), "calculator")
                        .commit();
                break;

        }

我正在使用开关盒。位置 0 是抽屉内 ListView 中的第一项。

于 2013-06-14T12:02:58.620 回答
0

问题是您正在使用

                                   FragmentTransaction.replace(r.Id.drawer_layout,fragment).

不要将片段直接附加到抽屉布局元素,尝试添加框架布局作为主要内容容器,然后将片段附加到此框架布局。您的 xml 内容应该看起来像这样 sudo one

<drawerlayout>
    <framelayout Id:fragment_holder/> //attach fragment        to this view
        <ListView/>  // your drawer ListView.
<drawerlayout/>
于 2013-07-24T21:39:56.950 回答