当前位置: 首页 > 知识库问答 >
问题:

第一个事件后禁用单击时的DrawerLayout

苏君昊
2023-03-14

我正在尝试在我的应用程序中实现一个抽屉布局。我遵循android开发者网站上的教程,一切顺利。我只有一个“小”问题:我用午餐打开应用程序,打开抽屉布局,然后点击列表视图中的一个元素,一切都很顺利,但当我尝试打开另一个抽屉并点击他的一个元素时,我注意到我第一次点击的元素仍然被点击,而我无法点击其他任何元素。我的代码是:public class Eventi extends Activity{private DrawerLayout MDrawerLayout;private ListView MDrawerList;private 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);
}

}

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

共有1个答案

艾照
2023-03-14

看看我的代码:

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中的第一个项目。

 类似资料:
  • 当我们需要别人只能点选“原始英文版”或“简体中文版”中的其中一项时,我们通常会用到 !insertmacro StartRadioButtons 这类的宏来控制单选,但是在 NSIS 的例子中,它们的操控都是在父项的情况下实行的,当我们在子项的环境中实现时,问题出现了,当我们为用户设想时,如果用户点选了父项“界面语言”,会造成子项选择丢失或者反选,这就跟我们原来设想的单选意图完全背道而驰了,那么我

  • 我想在点击某个div(“.checker”)后触发点击输入事件。换句话说,点击'.checker'将模拟点击复选框上的点击事件,远程检查它。 我写的代码可以工作,但click只有在第二次单击后才触发,第一次单击失败时才触发。这种情况甚至不会发生在'.checker'上,也会发生在checkbox上。 下面的所有HTML都包装在jquery折叠菜单中。单击“H3”后,“.sub-tree-wrap”

  • 如何为一个组件同时绑定单击和双击事件

  • 我正在尝试做一些非常简单的事情,在点击按钮时更改文本。 我似乎无法使用它,有人能告诉我添加ActionListener的正确位置吗? 主要类别 atmGUI类 编辑: 这是产生的错误 类型new ActionListener(){}必须实现继承的抽象方法ActionListener.actionPerform(ActionEvent)

  • 我正在使用android Studio模板中的android导航抽屉导航菜单。如何根据id导航到不同的活动。 我知道我错过了什么,但我找不到解决办法。有人能帮我吗?

  • 我需要创建一个按钮,如下所示: HTML 如何在JS文档中指定这些函数?