[android]setOnItemClickListener 在唯一指定列表元素

标签: Android textview
发布时间: 2016/9/26 4:19:03
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个列表由由自定义的不同 TextViews adapter

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>

在我 activity 用于 setOnItemClickListener 执行某项操作,当点击 ListElements。

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

它是可能启动 setOnItemClickListener,只有当单击R.id.out3 TextViews 和中点击R.id.out1R.id.out2的什么都不做吗?

解决方法 1:

让你像下面唯一一套 getView setOnClickListener()R.id.out3TextView

此外删除添加的代码下面。

 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;
    }
官方微信
官方QQ群
31647020