setOnItemClickListener仅指定列表元素

时间:2016-07-20 10:08:40

标签: android list textview onitemclicklistener

我有一个由自定义适配器管理的不同TextView组成的列表。

list.xml     

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FDD017" >

        <ListView android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

     </LinearLayout>

element.xml

<?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:orientation="vertical" >

    <View android:id="@+id/line1"
        android:layout_width="fill_parent"
        android:layout_height="5dp"
        android:background="#fd1726" />

    <TextView
        android:id="@+id/out1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="17sp"
        android:textStyle="bold"
        android:background="#17c4fd" />

    <View android:id="@+id/line2"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#236e01" />

    <TextView
        android:id="@+id/out2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="17sp"
        android:textStyle="bold"
        android:background="#17c4fd" />

    <View android:id="@+id/line3"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#236e01" />

    <TextView
        android:id="@+id/out3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="17sp"
        android:textStyle="bold"
        android:background="#c6c6c7" />

</LinearLayout>

在我的活动中,我使用setOnItemClickListener在单击ListElements时执行操作。

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter_view, View view, int position, long id ) { MyAction(); }
    });

是否可以仅在单击 R.id.out3 TextViews时启动setOnItemClickListener,并且在单击 R.id.out1 R时不执行任何操作。 id.out2

2 个答案:

答案 0 :(得分:1)

有两种方法可以设置全局OnItemClickListener,然后视图可用。 (如果需要,您甚至可以使用getView(position)从适配器返回视图,但不需要这样做)

然后,您可以检查该视图,验证它是您想要的视图,并完成操作,否则不执行任何操作。

另一种方式,我认为更简洁一点就是将OnClickListeners放在自定义适配器中的视图上。

希望您使用ViewHolder模式。在您定义视图时,您可以ViewHolder

进入out3.setOnClickListener(this);

然后像处理任何其他方式一样处理onClick,只有该视图才会注册点击事件。

答案 1 :(得分:1)

让你像下面的getView只将setOnClickListener()设置为R.id.out3 TextView

同时删除以下代码。

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter_view, View view, int position, long id ) { MyAction(); }
    });

GetView方法。

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View row = convertView;
        final ViewHolder holder;

        if (convertView == null) {

            /****** Inflate dahsboadrf_single_row.xml file for each row ( Defined below ) *******/
            row = inflater.inflate(R.layout.element, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();

            holder.out1 = (TextView) row.findViewById(R.id.out1);
            holder.out2 = (TextView) row.findViewById(R.id.out2);
            holder.out3 = (TextView) row.findViewById(R.id.out3);

            row.setTag(holder);
        } else
            holder = (ViewHolder) row.getTag();

        holder.out1.setText(""); // your logic
        holder.out2.setText(""); // your logic
        holder.out3.setText(""); // your logic

        holder.out3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // And you are done.
            }
        });
        return row;
    }

    class ViewHolder {

        TextView out1, out2, out3;
    }