1

我有一个由每行中的文本和按钮组成的列表视图。我必须为驻留在行中的每个按钮实现单击侦听器,但我无法这样做。我也检查了与 SO 不同的地方,但无法得到想要的结果。为此,我将自定义适配器实现为:

public class CategoryAdapter extends BaseAdapter {

    Context mContext;
    LayoutInflater mInflater;
    String[] strings = { "jjacj", "kpomkn", "jjbjbj nk", "njbhvh",
            "mknkn", "mnknlhn", "lknkn", "mknkn"};
    Holder holder = null;

    public CategoryAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return strings.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressWarnings("static-access")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            mInflater = (LayoutInflater) mContext
                    .getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
            holder = new Holder();
            convertView = mInflater.inflate(R.layout.layout_child_category,
                    null);
            holder.mTxtTitle = (TextView) convertView
                    .findViewById(R.id.txt_notification);
            holder.mButtonTag = (Button) convertView
                    .findViewById(R.id.btnNotify);
            holder.mLayoutMain = (RelativeLayout) convertView
                    .findViewById(R.id.layout_main);
            // convertView.setOnClickListener(callback);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        holder.mTxtTitle.setText(strings[position]);
        holder.mButtonTag.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                btnSelection();
            }
        });

        if (position == 0) {
            holder.mLayoutMain.setBackgroundResource(R.drawable.bg_table_top);
        } else if (position == getCount() - 1) {
            holder.mLayoutMain
                    .setBackgroundResource(R.drawable.bg_table_bottom);
        } else {
            holder.mLayoutMain
                    .setBackgroundResource(R.drawable.bg_table_middle);
        }

        return convertView;
    }

    OnClickListener callback = new OnClickListener() {

        @Override
        public void onClick(View v) {
            btnSelection();
        }
    };

    private void btnSelection() {
        if (holder.mButtonTag.getTag().equals("no")) {
            holder.mButtonTag.setTag("yes");
            holder.mButtonTag.setText("NEI");
            holder.mButtonTag.setPadding(20, 0, 0, 0);
            holder.mButtonTag.setBackgroundResource(R.drawable.ic_btn_nei);
            holder.mButtonTag.setTextColor(Color.BLACK);
        } else {
            holder.mButtonTag.setTag("no");
            holder.mButtonTag.setText("JA");
            holder.mButtonTag.setPadding(0, 0, 20, 0);
            holder.mButtonTag.setBackgroundResource(R.drawable.ic_btn_ja);
            holder.mButtonTag.setTextColor(Color.WHITE);
        }
    }

    static class Holder {
        Button mButtonTag;
        TextView mTxtTitle;
        RelativeLayout mLayoutMain;
    }
}

列出子布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_main"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:background="@drawable/bg_table_middle" >

    <TextView
        android:id="@+id/txt_notification"
        style="@style/textview_layout"
        android:textColor="@android:color/black" />

    <Button
        android:id="@+id/btnNotify"
        style="@style/button_layout" />

</RelativeLayout> 

这是我的看法:

在此处输入图像描述

任何人都可以让我知道我缺少的地方,或者应该做任何更改以在列表中的每个按钮上至少祝酒。任何形式的帮助将不胜感激。

谢谢。

4

3 回答 3

1

像这样在函数中将视图作为参数传递

private void btnSelection(View button) {
    if (button.getTag().equals("no")) {
        button.setTag("yes");
        ((Button)button).setText("NEI");
        button.setPadding(20, 0, 0, 0);
        button.setBackgroundResource(R.drawable.ic_btn_nei);
        ((Button)button).setTextColor(Color.BLACK);
    } else {
        button.setTag("no");
        ((Button)button).setText("JA");
        button.setPadding(0, 0, 20, 0);
        button.setBackgroundResource(R.drawable.ic_btn_ja);
        ((Button)button).setTextColor(Color.WHITE);
    }
}

并在getView方法中更改OnClickListener


在点击监听器之前添加getView

if (holder.mButtonTag.getTag() == null)
  holder.mButtonTag.setTag("");

holder.mButtonTag.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            btnSelection(v);
        }
    });

希望这可以帮助...

于 2013-04-23T05:47:22.103 回答
0

更改您的 xml 文件,如下所示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_main"
        android:layout_width="fill_parent"
        android:layout_height="40dp" 
        android:descendantFocusability="blocksDescendants"  
        android:background="@drawable/bg_table_middle" > 

     <TextView
           android:id="@+id/txt_notification"
           style="@style/textview_layout"
           android:focusable="false"
           android:textColor="@android:color/black" />

      <Button
            android:id="@+id/btnNotify"
            style="@style/button_layout" 
            android:focusable="false" />

并将android:descendantFocusability="blocksDescendants" 您的列表视图 xml 文件放入主布局中。同时放入android:focusable="false"列表视图。

holder.mButtonTag.setId(position);
holder.mButtonTag.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                btnSelection();
            }
        });
于 2013-04-23T05:06:24.180 回答
-1

将按钮代码更改为

<Button
android:id="@+id/btnNotify"
style="@style/button_layout" 
  android:focusable="false"
  android:focusableInTouchMode="false"/>
于 2013-04-23T05:22:34.890 回答