2014-09-04 153 views
0

我想在應用程序中使用滑動導航抽屜。但是,它似乎沒有處理OnItemClick方法。當我點擊一個項目什麼我happens.How使它發揮作用?謝謝滑動導航抽屜不處理點擊菜單項android

// MainActivity

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    drawer_layout = (DrawerLayout)findViewById(R.id.drawerLayout); 
    list_view = (ListView)findViewById(R.id.drawerList); 
    menu_items = getResources().getStringArray(R.array.nav_drawer_items); 
    list_view.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, menu_items)); 
    list_view.setOnItemClickListener(this); 

} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // TODO Auto-generated method stub 



     Toast.makeText(MainActivity.this, menu_items[position] + " was selected", Toast.LENGTH_LONG).show(); 

} 

// MainActivity.xml

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<FrameLayout 
    android:id="@+id/mainContent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<ListView android:id="@+id/drawerList" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="left" 
    android:background="@android:color/white" 
    android:divider="@android:color/black" 
    android:dividerHeight="1dp" 
    android:listSelector="@android:color/black" 
    android:choiceMode="singleChoice" 
    /> 

<LinearLayout 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/container" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
tools:ignore="MergeRootFrame" 
tools:context="com.example.MainActivity" 
> 



<TextView 
    android:id="@+id/tv1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/textview1" 
    android:layout_gravity="center" 
    android:textStyle="bold" 
    android:textSize="20sp" 
    android:gravity="center" 
    android:layout_marginTop="30dp"/> 


<Button 
    android:id="@+id/Button1" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_gravity="center" 
    android:background="@drawable/ok_button" 
    android:layout_marginTop="20dp"/> 

</LinearLayout> 

</android.support.v4.widget.DrawerLayout> 

回答

2

DrawerLayout的孩子View小號不正確的z順序。抽屜的View - 在這種情況下,ListView - 需要最後列出才能成爲其他任何東西的首選。

+1

正確,我不是usi在z_order中。現在它可以工作。謝謝一堆 – artist 2014-09-04 01:08:44

0

對於左側菜單中的抽屜完成簡單的,你可以試試下面的簡單....

MainActivity.class

private DrawerLayout mDrawerLayout; 
private ListView mDrawerList; 
private ActionBarDrawerToggle mDrawerToggle; 
private CharSequence mDrawerTitle; 
private CharSequence mTitle; 
private String[] navMenuTitles; 
private ArrayList<MainDrawerItem> navDrawerItems; 
private NavDrawerListAdapter adapter; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // for lefdrawer 
    mTitle = mDrawerTitle = getTitle(); 

    // load slide menu items 
    navMenuTitles = getResources().getStringArray(
      R.array.dashboard_leftMenu); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_dashboard_layout); 
    mDrawerList = (ListView) findViewById(R.id.main_dash_left_list_drawer); 
    navDrawerItems = new ArrayList<MainDrawerItem>(); 
    navDrawerItems.add(new MainDrawerItem(navMenuTitles[0], 0, 
      null, false)); 
     mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); 

    // setting the nav drawer list adapter 
    adapter = new NavDrawerListAdapter(getApplicationContext(), 
      navDrawerItems); 
    mDrawerList.setAdapter(adapter); 

    // enabling action bar app icon and behaving it as toggle button 
    getActionBar().setDisplayHomeAsUpEnabled(true); 
    getActionBar().setHomeButtonEnabled(true); 
     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.drawable.ic_drawer, // nav menu toggle icon 
      R.string.app_name, // nav drawer open - description for 
           // accessibility 
      R.string.app_name // nav drawer close - description for 
           // accessibility 
    ) { 
     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); 

    if (savedInstanceState == null) { 
     // on first time display view for first nav item 
     displayView(0); 
    } 
private void displayView(int position) { 
    // update the main content by replacing fragments 
    switch (position) { 
    case 0: 

    Toast.makeText(MainActivity.this, ..... + " was selected",Toast.LENGTH_LONG).show(); 
     break; 
    default: 

     break; 
} 
} 

private class SlideMenuClickListener implements 
     ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) { 
     // display view for selected nav drawer item 
     displayView(position); 
    } 
} 

    @Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // toggle nav drawer on selecting action bar app icon/title 
    if (mDrawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
    // Handle action bar actions click 
    switch (item.getItemId()) { 
    case R.id.action_settings: 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

MainDrawerItem.class

public class MainDrawerItem { 
private String title; 
public MainDrawerItem(){ 
} 

public MainDrawerItem(String title){ 
    this.title = title; 

} 

public String getTitle(){ 
    return this.title; 
} 
} 

對於適配器類別

public class NavDrawerListAdapter extends BaseAdapter { 

private Context context; 
private ArrayList<MainDrawerItem> navDrawerItems; 

public NavDrawerListAdapter(Context context, ArrayList<MainDrawerItem> navDrawerItems){ 
    this.context = context; 
    this.navDrawerItems = navDrawerItems; 
} 
@Override 
public Object getItem(int position) {  
    return navDrawerItems.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     LayoutInflater mInflater = (LayoutInflater) 
       context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     convertView = mInflater.inflate(R.layout.drawer_list_item, null); 
    } 

    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);  
    txtTitle.setText(navDrawerItems.get(position).getTitle()); 

    return convertView; 
} 

}