onItemClickListener不起作用

时间:2015-05-28 17:31:57

标签: android listview android-adapter onitemclicklistener drawerlayout

我的抽屉布局中有一个列表视图,我想为每个项目设置setlistener,我为listView的每个元素读取了几个post和setFocusable,setclickiable,setfocusableInTouchMode为false但仍然无效。先谢谢你。这是我的代码:

targetItem.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="#ffffff"
android:focusable="true"
android:clickable="true"
>

 <!-- icon -->
 <ImageView
     android:id="@+id/item_icon"
     android:layout_width="20dp"
     android:layout_height="20dp"
     android:layout_alignParentRight="true"
     android:layout_marginLeft="8dp"
     android:layout_marginRight="18dp"
     android:layout_marginTop="25dp"
     android:clickable="false"
     android:focusable="false"
     android:focusableInTouchMode="false"
     />

<!-- title -->
<TextView
     android:id="@+id/item_title"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_toLeftOf="@+id/item_icon"
     android:textSize="12dp"
     android:layout_marginTop="28dp"
     android:layout_marginRight="15dp"
     android:textColor="#000000"
     android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"

      />

<TextView
     android:id="@+id/item_id"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_toLeftOf="@+id/item_icon"
     android:textSize="12dp"
     android:layout_marginTop="28dp"
     android:layout_marginRight="15dp"
     android:textColor="#000000"
     android:visibility="gone"
     android:clickable="false"
    android:focusableInTouchMode="false"
    android:focusable="false"/>


</RelativeLayout>

activity_main.xml中

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

<!-- The navigation drawer -->

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

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

         .
         .
         .
   </RelativeLayout>

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

MainActivity.java

public class MainActivity extends Activity {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    sidebar();
}
private void sidebar() {


    MyAdapter adapter = new MyAdapter(this, generateData());
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.listview);
    mDrawerList.setAdapter(adapter);

    mDrawerList
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                    Toast.makeText(getBaseContext(), "Hello",
                            Toast.LENGTH_LONG).show();
                }
            });


}

private ArrayList<Model> generateData() {
    ArrayList<Model> models = new ArrayList<Model>();
    models.add(new Model("TimiT "));

    models.add(new Model(R.drawable.home,home,1));
    models.add(new Model(R.drawable.basket, basket,2));
    models.add(new Model(R.drawable.list, list,3));

    return models;
}

MyAdapter.java

public class MyAdapter extends ArrayAdapter<Model> {

    private final Context context;
    private final ArrayList<Model> modelsArrayList;

    public MyAdapter(Context context, ArrayList<Model> modelsArrayList) {

        super(context, R.layout.target_item, modelsArrayList);

        this.context = context;
        this.modelsArrayList = modelsArrayList;
    }

    @Override
    public View getView( final int position, View convertView, ViewGroup parent) {

        // 1. Create inflater 
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // 2. Get rowView from inflater

        View rowView = null;
        if(!modelsArrayList.get(position).isGroupHeader()){
            rowView = inflater.inflate(R.layout.target_item, parent, false);
            rowView.setFocusable(true);
            rowView.setClickable(true);
            rowView.setFocusableInTouchMode(true);


            // 3. Get icon,title & counter views from the rowView
            ImageView imgView = (ImageView) rowView.findViewById(R.id.item_icon); 
            TextView titleView = (TextView) rowView.findViewById(R.id.item_title);
            TextView idView = (TextView) rowView.findViewById(R.id.item_id);

            imgView.setImageResource(modelsArrayList.get(position).getIcon());
            imgView.setFocusable(false);
            imgView.setClickable(false);
            imgView.setFocusableInTouchMode(false);

            titleView.setText(modelsArrayList.get(position).getTitle());
            titleView.setFocusable(false);
            titleView.setFocusableInTouchMode(false);
            titleView.setClickable(false);

            idView.setText(modelsArrayList.get(position).getID());
            idView.setFocusable(false);
            idView.setFocusableInTouchMode(false);
            idView.setClickable(false);

            rowView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "Hello", Toast.LENGTH_LONG).show();

                }
            });


        }
        else{
                rowView = inflater.inflate(R.layout.group_header_item, parent, false);
                TextView titleView = (TextView) rowView.findViewById(R.id.header);
                titleView.setText(modelsArrayList.get(position).getTitle());

        }

        // 5. retrn rowView
        return rowView;
    }
}

Model.java

public class Model {

private int icon;
private String title;
private int id;

private boolean isGroupHeader = false;

public Model(String title) {
    this(-1,title,0);
    isGroupHeader = true;
}
public Model(int icon, String title,int id) {
    super();
    this.icon = icon;
    this.title = title;
    this.id = id;
}
public int getIcon() {
    return icon;
}
public void setIcon(int icon) {
    this.icon = icon;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public int getID() {
    return icon;
}
public void setID(int id) {
    this.id = id;
}
public boolean isGroupHeader() {
    return isGroupHeader;
}
public void setGroupHeader(boolean isGroupHeader) {
    this.isGroupHeader = isGroupHeader;
}



}

1 个答案:

答案 0 :(得分:0)

问题在于这一行rowView.setOnClickListener。注册onClickListener会阻止onItemClik被解雇。摆脱它,它会工作。如果您想在列表项上设置额外的点击监听器,则必须在OnClickListener的子项上设置convertView,而不是在convertView本身设置android:descendantFocusability="blocksDescendants"并添加Marshal.from_bytes }到列表项xml

的根目录