抽屉无法处理android中的点击事件

时间:2014-09-12 09:54:52

标签: android

我想在抽屉中点击一个项目时显示吐司,但不显示只是关闭抽屉......

除了处理点击事件外,一切正常。

我正在使用以下代码。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
                TitleNavigationAdapter adapter = new TitleNavigationAdapter(this, items);
                drawerList = (ListView) findViewById(R.id.list_slidermenu);
                drawerList.setAdapter(adapter);

                drawerList.setOnItemClickListener(this);
                drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                        GravityCompat.START);
                // getActionBar().setHomeButtonEnabled(true);
                getActionBar().setDisplayHomeAsUpEnabled(true);

                drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
                        R.drawable.ic_drawer, R.string.desc, R.string.refresh) {
                    @Override
                    public void onDrawerClosed(View drawerView) {
                        super.onDrawerClosed(drawerView);
                        drawerToggle.syncState();
                    }

                    @Override
                    public void onDrawerOpened(View drawerView) {
                        super.onDrawerOpened(drawerView);
                        getActionBar().setTitle("Opened");
                    }

                };
                drawerLayout.setDrawerListener(drawerToggle);
            }

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                Toast.makeText(this, "seleceted" + position, Toast.LENGTH_LONG).show();
            }

        }

2 个答案:

答案 0 :(得分:0)

你的班级声明中有implements AdapterView.OnItemClickListener吗? 另一件事是直接将onClickListener设置为新的侦听器

drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Log.d("MyApp", "seleceted" + position);
    Toast.makeText(this, "seleceted" + position, Toast.LENGTH_LONG).show();
  }
});

另外,我建议在开发过程中使用Log发送调试消息。

答案 1 :(得分:0)

插入drawerLayout.closeDrawer(drawerList);在onItemCLick(...)

里面

@覆盖

public void onItemClick(AdapterView parent,View view,int position,                     长id){

       drawerLayout.closeDrawer(drawerList);
            Toast.makeText(this, "seleceted" + position, Toast.LENGTH_LONG).show();
        }