0

我有一个列表视图。在列表视图的每一行的布局中,我有一个按钮。xml 中的那个按钮有一个 "android:onClick="onClickDoSmth" " 。列表视图还有一个 itemClickListener。

在 Activity 类中,我有一个方法“public void onClickDoSmth(View v)”,该方法在按下按钮时被调用。

但我还为列表视图设置了一个 ItemClickListener。我希望当行(按钮外)被击中时去另一个活动。当按钮可见时,列表视图的 ItemClickListener 不起作用。当按钮为 View.GONE 时,listview 的侦听器工作。

有什么建议么?

4

2 回答 2

1

使用 ImageView 而不是 Button。按钮单击和 OnItemClickListener 不能一起工作。

这是一个工作示例:

MainActivity.java

public class MainActivity extends Activity {
    private ListView customLV;
    private CustomArrayAdapter adapter;
    String strArray[] = { "First", "Second", "Third", "Forth", "Fifth" };
    ArrayList<String> strAL = new ArrayList<String>(Arrays.asList(strArray));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        customLV = (ListView) findViewById(R.id.customLV);
        adapter = new CustomArrayAdapter(this, R.layout.list_item, strAL);
        customLV.setAdapter(adapter);
        customLV.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long arg3) {
                Toast.makeText(getApplicationContext(), "List Item", Toast.LENGTH_SHORT)
                        .show();

            }
        });
    }

}

CustomArrayAdapter.java

public class CustomArrayAdapter extends ArrayAdapter<String> {

    Context context;

    public CustomArrayAdapter(Context context, int textViewResourceId,
            ArrayList<String> strAL) {
        super(context, textViewResourceId, strAL);
        this.context = context;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.list_item, parent, false);

            viewHolder = new ViewHolder();

            viewHolder.titleTV = (TextView) convertView
                    .findViewById(R.id.titleTV);
            viewHolder.testBT = (ImageView) convertView.findViewById(R.id.testBT);
            viewHolder.testBT.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "Button Clicked",
                            Toast.LENGTH_SHORT).show();
                }
            });
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.titleTV.setText(getItem(position));

        return convertView;
    }

    class ViewHolder {
        TextView titleTV;
        ImageView testBT;

    }
}

list_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:padding="10dp" >

    <TextView
        android:id="@+id/titleTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" />

    <ImageView
        android:id="@+id/testBT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:onClick="onClick"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/customLV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
/>

</RelativeLayout>
于 2013-06-01T23:13:03.573 回答
0

添加android:descendantFocusability="blocksDescendants"为您的属性Button。然后你应该能够同时管理onClickDoSmth/onListItemClickitemClickListener

于 2013-06-01T20:10:26.610 回答