2014-01-18 84 views
1

我想实现抽屉布局,但onclick列表中的项目之一,它不会带我到另一种方法。我不知道这是什么错误。 这里是我的代码onClickLayout onClick监听器不起作用

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" > 

请不要让我知道我错了

+0

您是否试图在您的点击监听器中显示日志以确保它未被调用? – TrtG

+0

我没有检查...它没有被调用。调试点也不会进入该方法。 –

回答

0

你的代码看起来罚款我。但我还没有看到你的适配器代码。

在列表适配器中有按钮吗?我看不到您的列表适配器代码,所以我猜测这可能是问题。

有一个线程讨论this按钮问题。