按钮不可点击listView

时间:2017-05-15 09:06:46

标签: android listview custom-adapter

我有一个listView,每行都有一个按钮。问题是我可以点击我的listView项目,但有些事情发生了,但是我的Button不起作用,没有任何反应。我已经阅读了有关解决方案的一些内容,但没有人帮助过我。

这是 list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:descendantFocusability="blocksDescendants"
>
<TextView
    android:id="@+id/text_branche_cours"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="English"
    />

<TextView
    android:id="@+id/text_trait"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" - "
    />
<TextView
    android:id="@+id/text_designation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reading"
    android:layout_marginRight="150dp"
    />
<Button
    android:id="@+id/bAjouterJalon"
    android:layout_width="58dp"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_action_add_jalon"

/&GT;

这是我的习惯用语类:

public class CustomAdapter extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;

public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.layout = layout;
    this.mContext = context;
    this.inflater = LayoutInflater.from(context);
    this.cr = c;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return inflater.inflate(layout, null);
}

@Override
public void bindView(View view, final Context context, final Cursor cursor) {
    super.bindView(view, context, cursor);


   final Button ajouter = (Button)view.findViewById(R.id.bAjouterJalon);

    ajouter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent retour = new Intent(mContext,OngletJalonsNotes.class);
            mContext.startActivity(retour);

        }
    });
}
}

感谢您的帮助。

@EDIT - 我的列表视图现在无法点击!!

2 个答案:

答案 0 :(得分:0)

你正在使按钮可调焦和放大专注于触摸假,因此它不会检测按钮上的触摸标签,从xml中的按钮标记中删除这两行:

       android:focusable="false"
       android:focusableInTouchMode="false"

示例:

     <Button
       android:id="@+id/bAjouterJalon"
       android:layout_width="58dp"
       android:layout_height="wrap_content"
       android:background="@drawable/ic_action_add_jalon"
       />

*****************新的变化*******************

CustomAdapter.class:

public class CustomAdapter extends SimpleCursorAdapter {
                private Context mContext;
                private Context appContext;
                private int layout;
                private Cursor cr;
                private final LayoutInflater inflater;

                public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
                    super(context, layout, c, from, to);
                    this.layout = layout;
                    this.mContext = context;
                    this.inflater = LayoutInflater.from(context);
                    this.cr = c;
                }

                @Override
                public View newView(Context context, Cursor cursor, ViewGroup parent) {
                    return inflater.inflate(layout, null);
                }

                @Override
                public void bindView(View view, final Context context, final Cursor cursor) {
                    super.bindView(view, context, cursor);


                    final Button ajouter = (Button)view.findViewById(R.id.bAjouterJalon);

                    final LinearLayout root_view = (LinearLayout)view.findViewById(R.id.item_root);

                    root_view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                          // List item Click detected

                        }
                    });


                    ajouter.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent retour = new Intent(mContext,OngletJalonsNotes.class);
                            mContext.startActivity(retour);

                        }
                    });
                }
            }

列出项目xml:

   <?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/item_root"
        android:descendantFocusability="blocksDescendants"
        android:orientation="horizontal"
        android:padding="16dp">

        <TextView
            android:id="@+id/text_branche_cours"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="English" />

        <TextView
            android:id="@+id/text_trait"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text=" - " />

        <TextView
            android:id="@+id/text_designation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="150dp"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="Reading" />

        <Button
            android:id="@+id/bAjouterJalon"
            android:layout_width="58dp"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_action_add_jalon" />
    </LinearLayout>

答案 1 :(得分:0)

在你的list_item xml中放android:descendantFocusability="blocksDescendants“  在LinearLayout中。