DrawerLayout上的onClick Listener不起作用

时间:2014-01-18 12:06:51

标签: android drawerlayout

我正在尝试实现抽屉布局,但是点击列表中的一个项目,它不会将我带到另一个方法。我不知道这有什么不对。 这是我的代码

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items to array
    // Home
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
    // Find People
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(0, -1)));
    // Photos
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
    // Communities, Will add a counter here
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(0, -1)));
    // Pages
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(0, -1)));
    // What's hot, We  will add a counter here
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5],   navMenuIcons.getResourceId(0, -1)));

    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setDisplayShowHomeEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, 
            R.string.app_name, 
            R.string.app_name 
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            // TODO Auto-generated method stub
             displayView(position);
        }


    });

          private void displayView(int position) {
    // update the main content by replacing fragments
    Intent i = null;
    Bundle extras1 = new Bundle();
    switch (position) {
    case 0:

        break;
    case 1:

         i = new Intent(TimeSheetEntry.this,
                    Expense.class);

            extras1.putString("Name", objClsTracnsactions.getStrUserName());
            extras1.putString("BranchCode",
                    objClsTracnsactions.getStrBranchCode());
            extras1.putString("EmpId", objClsTracnsactions.getStrEmpId());
            extras1.putString("EmpType",
                    objClsTracnsactions.getStrEmpType());
            i.putExtras(extras1);
        break;
    case 2:
        //fragment = new PhotosFragment();
        break;
    case 3:
        //fragment = new CommunityFragment();
        break;
    case 4:
        //fragment = new PagesFragment();
        break;
    case 5:
        //fragment = new WhatsHotFragment();
        break;

    default:
        break;
    }

    if (i != null) {
        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        startActivity(i);
        finish();
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }

}

这是我的xml文件..

 <android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
  android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="5dp">

<View
    android:layout_width="wrap_content"
    android:layout_height="1dip"
    android:layout_marginTop="1dp"
    android:background="@drawable/divider_line" />
 <ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
     android:layout_gravity="left"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"        
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:background="@color/white">


            <TableLayout
                android:id="@+id/tableLayout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:shrinkColumns="1"
                android:stretchColumns="1" >

  

请告诉我出错的地方

1 个答案:

答案 0 :(得分:0)

您的代码对我来说很好。但我还没有看到你的适配器代码。

列表适配器中是否有按钮?我看不到你的列表适配器代码所以我猜这可能是问题。

有一个讨论this按钮问题的帖子。