第一个事件后DrawerLayout上单击禁用 [英] DrawerLayout on click disabled after first event

查看:163
本文介绍了第一个事件后DrawerLayout上单击禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的应用程序抽屉布局。我遵循Android开发者网站上的教程和一切都很好。我只有一个小的问题:我吃午饭的应用程序,打开抽屉布局并单击列表视图中的一个元素上,并一切顺利的伟大,但是当我尝试打开另一个时间抽屉,点击他的元素我的一个注意到我已经第一次点击的元素仍是点击,我不能点击其他任何elements.The抽屉只需关闭没有任何吃午饭的onclick事件。
我的code是:
    公共类节庆活动延伸活动{
    私人DrawerLayout mDrawerLayout;
    私人的ListView mDrawerList;
    私人ActionBarDrawerToggle mDrawerToggle;

I'm trying to implement a drawer layout in my app. I follow the tutorial on android developer site and all goes fine. I have only a "little" problem: I lunch the app, open the drawer layout and click on an element of the list view and all goes great, but when I try to open another time the drawer and click on one of his element I noticed that the element that I've first clicked is still clicked and i'm not able to click on any of the other elements.The drawer simply close without lunching any onClick events. My code is: 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);
}

}

我把它从开发者网站,所以我不明白wher是错误。是否有任何人谁可以帮我?
在此先感谢;)

I took it from the Developer site so I don't understand wher is the error. Is there anyone who can help me? Thanks in advance ;)

推荐答案

我的code请看下图:

Take a look at my code:

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;

        }

我使用的switch-case。位置0是在我的抽屉里ListView中的第一个项目。

I am using a switch-case. position 0 is the very first item in my ListView inside the drawer.

这篇关于第一个事件后DrawerLayout上单击禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆