列表视图的onitemclick()在导航抽屉中不起作用

时间:2014-03-06 09:23:49

标签: android android-listview navigation-drawer

我正在构建一个导航抽屉,当单击一个项目时,它会在音乐播放器中打开一个新活动。问题是列表视图已创建,项目也会列在其中。但在itemclick上没有任何反应。 下面是我的代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.audioedit);
    mTitle = mDrawerTitle = getTitle();

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

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

    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(1, -1)));


    // Recycle the typed array
    navMenuIcons.recycle();
    Log.d("navicon", "recycle");

    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // display view for selected nav drawer item
            displayView(position);
            Log.d("onslidermenu", String.valueOf(position));

        }
    });
    Log.d("drawerclicklistener", "set");


    /* nadapter = new ArrayAdapter<String>(
                getApplicationContext(),
                R.layout.drawer_list_item ,
                navMenuTitles);
     */

    // 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) {

            mDrawerLayout.closeDrawer(mDrawerList);
            getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            mDrawerLayout.openDrawer(mDrawerList);
            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);
    }

        /**
 * Diplaying fragment view for selected nav drawer list item
 * */

private void displayView(int position) {
    // update the main content by replacing fragments
    Log.d("indisplayview", String.valueOf(position));

    //Fragment fragment = null;
    switch (position) {
    case 0:
        Toast.makeText(getApplicationContext(), "home", Toast.LENGTH_LONG).show();
        break;
    case 1:
        Intent i=new Intent(getApplicationContext(), equalizer.class);
        startActivityForResult(i, 103);
        break;

    default:
        break;
    }


        // update selected item and title, then close the drawer

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);


}

@SuppressLint("NewApi")
@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);
}

@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;



         }
        return super.onOptionsItemSelected(item);
        }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.audioedit, menu);
    getMenuInflater().inflate(R.menu.equalizer1, menu);
    getMenuInflater().inflate(R.menu.equalizer2, menu);
    getMenuInflater().inflate(R.menu.bpm, menu);
    getMenuInflater().inflate(R.menu.reverbandbassboost, menu);
    return true;
}


/* *
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);

    return super.onPrepareOptionsMenu(menu);
}

我正在将它用于音乐播放器。因此,从导航抽屉我需要打开其他活动以获得均衡器和其他效果。 在此先感谢您的帮助。

这是布局。:

    <android.support.v4.widget.DrawerLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/drawer_layout"
   android:background="#000000"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

    <FrameLayout
      android:id="@+id/frame_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

<!-- Listview to display slider menu -->
<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"        
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>

 <ListView
    android:id="@+id/list_slidermenu2"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"        
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>

     <RelativeLayout android:id="@+id/mrelative"
android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="150dp"
    android:layout_height="20dp"
    android:layout_above="@+id/btnPlay1"
    android:layout_alignTop="@+id/btnrec"
    android:src="@drawable/ic_launcher" />




<ImageButton
    android:id="@+id/btnPlay1"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignBottom="@+id/btnPlay2"
    android:layout_alignLeft="@+id/imageView1"
    android:background="@null"
    android:src="@drawable/btn_play" />

<Button
    android:id="@+id/btncue2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btncue1"
    android:layout_alignTop="@+id/btnPlay2"
    android:layout_toLeftOf="@+id/btnPlay2"
    android:text="cue" />

<ImageButton
    android:id="@+id/btnPlay2"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/imageView2"
    android:background="@null"
    android:src="@drawable/btn_play" />

<Button
    android:id="@+id/btncue1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/btnPlay1"
    android:layout_toRightOf="@+id/btnPlay1"
    android:text="cue" />

<Button
    android:id="@+id/btncall1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btncue1"
    android:layout_alignTop="@+id/btncue1"
    android:layout_toRightOf="@+id/btncue1"
    android:text="call" />

<Button
    android:id="@+id/btncall2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btncue2"
    android:layout_alignTop="@+id/btncue2"
    android:layout_toLeftOf="@+id/btncue2"
    android:text="call" />

<ImageButton
    android:id="@+id/btnPlaylist2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btnrec"
    android:layout_toLeftOf="@+id/imageView2"
    android:background="@null"
    android:src="@drawable/btn_playlist" />

<ImageButton
    android:id="@+id/btnPlaylist1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btnrec"
    android:layout_toRightOf="@+id/imageView1"
    android:background="@null"
    android:src="@drawable/btn_playlist" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="150dp"
    android:layout_height="20dp"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/btnrec"
    android:layout_marginRight="38dp"
    android:src="@drawable/ic_launcher" />

<SeekBar
    android:id="@+id/seekBar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imageView1"
    android:layout_alignLeft="@+id/imageView1" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/seekBar2"
    android:layout_alignLeft="@+id/seekBar2" />

<ImageButton
    android:id="@+id/btnrec"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_centerVertical="true"
    android:layout_marginRight="30dp"
    android:layout_toLeftOf="@+id/btnPlaylist2"
    android:background="@null"
    android:src="@drawable/btn_record" />

NavDrawerListAdapter类:

    public class NavDrawerListAdapter extends BaseAdapter {

private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;

public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
    this.context = context;
    this.navDrawerItems = navDrawerItems;
}

@Override
public int getCount() {
    return navDrawerItems.size();
}

@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);
    }

    ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
    TextView txtCount = (TextView) convertView.findViewById(R.id.counter);

    imgIcon.setImageResource(navDrawerItems.get(position).getIcon());        
    txtTitle.setText(navDrawerItems.get(position).getTitle());

    // displaying count
    // check whether it set visible or not
    if(navDrawerItems.get(position).getCounterVisibility()){
        txtCount.setText(navDrawerItems.get(position).getCount());
    }else{
        // hide the counter view
        txtCount.setVisibility(View.GONE);
    }

    return convertView;
}

}

navDrawerItem类:

    public class NavDrawerItem {

private String title;
private int icon;
private String count = "0";
// boolean to set visiblity of the counter
private boolean isCounterVisible = false;

public NavDrawerItem(){}

public NavDrawerItem(String title, int icon){
    this.title = title;
    this.icon = icon;
}

public NavDrawerItem(String title, int icon, boolean isCounterVisible, String count){
    this.title = title;
    this.icon = icon;
    this.isCounterVisible = isCounterVisible;
    this.count = count;
}

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

public int getIcon(){
    return this.icon;
}

public String getCount(){
    return this.count;
}

public boolean getCounterVisibility(){
    return this.isCounterVisible;
}

public void setTitle(String title){
    this.title = title;
}

public void setIcon(int icon){
    this.icon = icon;
}

public void setCount(String count){
    this.count = count;
}

public void setCounterVisibility(boolean isCounterVisible){
    this.isCounterVisible = isCounterVisible;
}

}

drawer_list_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="48dp" 
     android:background="@drawable/list_selector">

<ImageView
    android:id="@+id/icon"
    android:layout_width="25dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:contentDescription="@string/desc_list_item_icon"
    android:src="@drawable/ic_home"
    android:layout_centerVertical="true" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/icon"
    android:textColor="@color/list_item_title"
    android:gravity="center_vertical"
    android:paddingRight="40dp"/>

<TextView android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/counter_bg"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="8dp"
    android:textColor="@color/counter_text_color"/>

0 个答案:

没有答案