【问题标题】:Button inside ListView unable to Receive OnClick? [duplicate]ListView 内的按钮无法接收 OnClick? [复制]
【发布时间】:2014-08-16 04:57:47
【问题描述】:

目前,我的 listView 位于片段内。但是,每当我点击它们时,视图中的按钮都无法接收 on click 事件。

我不知道该怎么做。

这是我的代码:

public class ViewInvitationsCursorAdapter extends CursorAdapter {

private Activity activity;

private int layout;
private Cursor cr;
private final LayoutInflater inflater;
private Fragment f;
private boolean displayAcceptDeny;

public ViewInvitationsCursorAdapter(Activity activity, Fragment f,
        int layout, Cursor c, boolean displayAcceptDeny) {
    super(activity, c);
    this.layout = layout;
    this.activity = activity;
    this.inflater = LayoutInflater.from(activity);
    this.cr = c;
    this.f = f;
    this.displayAcceptDeny = displayAcceptDeny;
}

public static class ViewHolder {

    public TextView sender;
    public TextView sender_email;
    public TextView author_role;
    public Button deny;
    public Button accept;
    public Button cancel;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    ViewHolder holder = new ViewHolder();
    View view = inflater.inflate(layout, parent, false);

    holder.sender = (TextView) view.findViewById(R.id.connection_sender);
    holder.sender_email = (TextView) view
            .findViewById(R.id.connection_sender_email);

    holder.author_role = (TextView) view.findViewById(R.id.connection_role);

    holder.accept = (Button) view.findViewById(R.id.connection_allow);
    holder.deny = (Button) view.findViewById(R.id.connection_deny);
    holder.cancel = (Button) view.findViewById(R.id.connection_cancel);

    view.setTag(holder);
    return view;
}


@Override
public void bindView(View view, Context context, final Cursor cursor) {
    // .bindView(view, context, cursor);
    final ViewHolder holder = (ViewHolder) view.getTag();

    int sender_index = cursor
            .getColumnIndexOrThrow(FamilyDBHelper.COLUMN_SENDER_BY_NAME);
    int sender_email_index = cursor
            .getColumnIndexOrThrow(FamilyDBHelper.COLUMN_SENDER_BY_EMAIL);

    String senderString = cursor.getString(sender_email_index);

    int author_role_name = cursor
            .getColumnIndexOrThrow(FamilyDBHelper.COLUMN_RELATIONTYPE_NAME);
    int message_index = cursor
            .getColumnIndexOrThrow(FamilyDBHelper.COLUMN_MESSAGE);

    holder.sender.setText(cursor.getString(sender_index));

    holder.author_role.setText(cursor.getString(author_role_name));
    holder.sender_email.setText(cursor.getString(sender_email_index));



    holder.cancel.setVisibility(View.VISIBLE);
    // setUpCancel(view, cursor);
    holder.cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

              Log.i("cancel", "cancel clicked!");

        }
    });

}

protected void setUpAcceptDeny(View view, final Cursor cursor) {
    final ViewHolder holder = (ViewHolder) view.getTag();

    holder.accept.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.i("accept", "accept clicked!");
        }
    });
    holder.deny.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.i("deny", "deny clicked!");
        }
    });
}

}

这是每个项目的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/connection_item_row_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="8dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="8dp" >

    <LinearLayout
        android:id="@+id/connection_item_info_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/connection_sender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="16sp"
                android:textStyle="bold"
                tools:text="ANDROID: PREFERENCEFRAGMENTCOMPAT" >
            </TextView>

            <TextView
                android:id="@+id/connection_sender_email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/connection_sender"
                android:textColor="#000000"
                android:textSize="14sp"
                tools:text="ANDROID: PREFERENCEFRAGMENTCOMPAT" >
            </TextView>

            <TextView
                android:id="@+id/connection_role"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/connection_sender_email"
                android:textColor="#000000"
                android:textSize="14sp"
                 >
            </TextView>
        </RelativeLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/connection_item_info_layout"
        android:orientation="horizontal"
        android:paddingBottom="1.0dip"
        android:paddingLeft="4.0dip"
        android:paddingRight="4.0dip"
        android:paddingTop="5.0dip" >

        <Button
            android:id="@+id/connection_allow"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="Allow"
            android:background="@color/green"
            android:textColor="@color/white"
            android:visibility="gone" />

        <Button
            android:id="@+id/connection_deny"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
             android:background="@color/red"
             android:textColor="@color/white"
            android:text="Deny"
            android:visibility="gone" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/connection_item_info_layout_request"
        android:orientation="horizontal"
        android:paddingBottom="1.0dip"
        android:paddingLeft="4.0dip"
        android:paddingRight="4.0dip"
        android:paddingTop="5.0dip" >

        <Button

            android:id="@+id/connection_cancel"
            android:layout_width="0.0dip"
             android:background="@color/red"
             android:textColor="@color/white"

            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="Cancel Request"
            android:visibility="gone" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • @Faytll 我没有看到调用 setUpAcceptDeny
  • @Maddy:我减少了代码以便尝试找出问题所在。
  • @Krylez:这些解决方案都不适合我。如果我的列表视图在片段内,它会改变吗?

标签: android listview button focus


【解决方案1】:

将此行添加到您的列表项 xml 文件中。

android:descendantFocusability="blocksDescendants"

【讨论】:

  • 不起作用。不幸的是,当我将它添加到包装整个项目视图的 LinearLayout 时,测试失败了。
【解决方案2】:

如果您像这样在 newView 方法中设置 clicklistener,它应该可以工作。我通常使用 getView() 这个方法,从来没有问题..

@覆盖 public View getView(Context context, View View, ViewGroup parent) {

ViewHolder holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View = inflater.inflate(R.layout.your_layout_file_id, parent,
                false);

holder.sender = (TextView) view.findViewById(R.id.connection_sender);
holder.sender_email = (TextView) view
        .findViewById(R.id.connection_sender_email);

holder.author_role = (TextView) view.findViewById(R.id.connection_role);

holder.accept = (Button) view.findViewById(R.id.connection_allow);
holder.deny = (Button) view.findViewById(R.id.connection_deny);
holder.cancel = (Button) view.findViewById(R.id.connection_cancel);

view.setTag(holder);

holder.cancel.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

          Log.i("cancel", "cancel clicked!");

    }
});

return view;

}

【讨论】:

  • 不起作用。按钮不触发。
  • 请注意,我的代码用于 getView() 方法,这是为 ListView 实现自定义适配器的一种方式
猜你喜欢
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 2014-12-27
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多