简体   繁体   中英

How to get SlideMenu ListView Items clickable

I am new in android programming and I have set up a slide menu where all items are shown up correctly. The Problem is, I don't get the Items in my ListView clickable. Hope to get some help. Thanks a lot.

MainActivity Class:

package de.jan.syg;

import java.util.ArrayList;


import android.app.Activity;
import android.content.Context;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class SYGMain extends Activity implements ListView.OnItemClickListener{

    private DrawerLayout _mDrawerLayout;
    private ListView _mDrawerList;
    private ActionBarDrawerToggle _mDrawerToggle;

    ListAdapter _adapter;
    ArrayList<SYGSlideMenuItem> _dataList;


    // Constructor
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initializing
        _dataList = new ArrayList<SYGSlideMenuItem>();
        _mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        _mDrawerList = (ListView) findViewById(R.id.left_drawer);

        _mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        // Add Drawer Item to dataList
        _dataList.add(new SYGSlideMenuItem("Title")); // adding a header to the list
        _dataList.add(new SYGSlideMenuItem("item1", R.drawable.ic_action_camera));
        _dataList.add(new SYGSlideMenuItem("item2", R.drawable.ic_action_email));
        _dataList.add(new SYGSlideMenuItem("item3", R.drawable.ic_action_search));
        _dataList.add(new SYGSlideMenuItem("item4", R.drawable.ic_action_import_export));

        _dataList.add(new SYGSlideMenuItem("item5", R.drawable.ic_action_gamepad));
        _dataList.add(new SYGSlideMenuItem("item6", R.drawable.ic_action_group));
        _dataList.add(new SYGSlideMenuItem("item7", R.drawable.ic_action_good));
        _dataList.add(new SYGSlideMenuItem("item8", R.drawable.ic_action_settings));
        _dataList.add(new SYGSlideMenuItem("item9", R.drawable.ic_action_help));
        _dataList.add(new SYGSlideMenuItem("item10",
                    R.drawable.ic_action_about));


        _adapter = new SlideMenuItemAdapter(this, R.layout.slide_menu_item, _dataList);


        _mDrawerList.setAdapter(_adapter);

        _mDrawerList.setOnItemClickListener(this);


        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        _mDrawerToggle = new ActionBarDrawerToggle(this, _mDrawerLayout, 
                                                   R.drawable.ic_action_settings, R.string.drawer_open,
                                                   R.string.drawer_close) 
        {
                public void onDrawerClosed(View view) {
                      invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu()
                }

                public void onDrawerOpened(View drawerView) {
                      invalidateOptionsMenu(); // creates call to
                                                                // onPrepareOptionsMenu()
                }
          };

          _mDrawerLayout.setDrawerListener(_mDrawerToggle);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        _mDrawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (_mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return false;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        _mDrawerToggle.onConfigurationChanged(newConfig);
    }

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


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        Toast.makeText(getApplicationContext(), position+"Clicked", Toast.LENGTH_SHORT).show();


    }

    // Custom ListAdapter
    private class SlideMenuItemAdapter extends ArrayAdapter<SYGSlideMenuItem>{

        Context _context;
        int _layoutResourceId;
        ArrayList<SYGSlideMenuItem> _items = null;

        public SlideMenuItemAdapter(Context context, int textViewResourceId, ArrayList<SYGSlideMenuItem>  items) {
            super(context, textViewResourceId, items);
            this._context=context;
            this._layoutResourceId=textViewResourceId;
            this._items=items;
        }

        // get the ListLayout to DrawerLayout
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            if(convertView==null){
                // inflate the layout
                LayoutInflater inflater = ((Activity) _context).getLayoutInflater();
                convertView = inflater.inflate(_layoutResourceId, parent, false);
            }

            SYGSlideMenuItem item = _items.get(position);

            ImageView ivItem = (ImageView) convertView.findViewById(R.id.ivOptionPic);
            TextView tvItem = (TextView) convertView.findViewById(R.id.tvOptionText);

            tvItem.setText(item.getItemName());
            ivItem.setImageResource(item.getImgResID());
            convertView.setClickable(true);
            tvItem.setClickable(true);
            ivItem.setClickable(true);




            return convertView;
        }
    }

}

SlideMenuItemClass:

package de.jan.syg;

public class SYGSlideMenuItem {

    private String _itemName;
    private int _imgResID;


    // Constructors
    public SYGSlideMenuItem(String itemName, int imgResID) {
        super();
        _itemName = itemName;
        _imgResID = imgResID;
  }

    public SYGSlideMenuItem(String optionTitle) {
        this(optionTitle, 0);

  }

    // Getters
    public String getItemName() {
        return _itemName;
  }


    public int getImgResID() {
        return _imgResID;
  }


    // Setters
    public void setItemName(String itemName) {
      _itemName = itemName;
  }

    public void setImgResID(int imgResID) {
        this._imgResID = imgResID;
  }

}

activity_main.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="#36D92E">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->


    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#474747"

        />

     <RelativeLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

slide_menu_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="match_parent"
    android:orientation="vertical" 
    android:background="#474747"
    >

        <ImageView
            android:id="@+id/ivOptionPic"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:clickable="true"/>

        <TextView
            android:id="@+id/tvOptionText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/ivOptionPic"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="16dp"
            android:layout_toRightOf="@+id/ivOptionPic"
            android:gravity="left|center"
            android:text="Option"
            android:clickable="true"
            android:textColor="#FFFFFF"
            android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

What am I doing wrong? Hope to get some help. Thanks a lot.

In your item list layout file change android:clickable="true" to android:clickable="false" and add android:focusable="false" in ImageView and TextView

<?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="match_parent"
    android:orientation="vertical" 
    android:background="#474747"
    >

        <ImageView
            android:id="@+id/ivOptionPic"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:focusable="false"
            android:clickable="false"/>

        <TextView
            android:id="@+id/tvOptionText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/ivOptionPic"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="16dp"
            android:layout_toRightOf="@+id/ivOptionPic"
            android:gravity="left|center"
            android:text="Option"
            android:focusable="false"
            android:clickable="false"
            android:textColor="#FFFFFF"
            android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

and in ListView

<ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:focusable="true"
        android:clickable="true"
        android:background="#474747"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM