listview中的android按钮都可以点击

时间:2013-06-01 19:36:53

标签: android button android-listview onitemclicklistener

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

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

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

有什么建议吗?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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