3

我有一个扩展ListView的 Activity 我使用的 ListView 是ArrayAdapter的扩展, ListView 的每一行主要由一个WebView组成

问题是触摸 WebView 不会调用OnListItemClick

关于我如何能够触摸 webview 并调用 OnListItemClick 的任何想法?还是我做错了什么? 谢谢!

这是 Activity 类的骨架,以及布局的相关 XML 文件

PS,我已经尝试在我的布局中设置 android:clickable="false" ,但这并不能让我点击我的 ListView 的行。有趣的是,我仍然可以滚动列表,但我无法点击!

public class ListOfItemsFromTopicActivity extends ListActivity {
    private Topic t;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.items_from_topic_skeleton);

        TopicFactory tf                 = new TopicFactory();
        t                               = tf.build_topic(3);
        ListView itemlist               = getListView();
        ListOfItemsAdapter adapter      = new ListOfItemsAdapter(this, R.layout.items_from_topic_row, t.getItems());
        itemlist.setAdapter(adapter);
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // do some stuff that never gets done
    }

    private class ListOfItemsAdapter extends ArrayAdapter<Item> {

        private ArrayList<Item> items;
        private int                     text_view_resource_id;
        private LayoutInflater          layout_inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        public ListOfItemsAdapter(Context context, int a_text_view_resource_id, ArrayList<Item> some_items) {
            super(context, a_text_view_resource_id, some_items);
            this.items                  = some_items;
            this.text_view_resource_id  = a_text_view_resource_id;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v              = layout_inflater.inflate(this.text_view_resource_id, null);
            Item o              = items.get(position);
            WebView page        = (WebView)v.findViewById(R.id.item);
            page.loadDataWithBaseURL("fake://a/bcde", o.getShort_title(), "text/html", "utf-8", "");
            return v;
        }
    }
}

items_from_topic_skeleton.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="fill_parent"
              android:layout_width="fill_parent"
              android:orientation="vertical"
        >
    <TextView
            android:background="@drawable/blackgrad"
            android:gravity="center"
            android:id="@+id/section_title"
            android:layout_height="50dip"
            android:layout_width="fill_parent"
            android:textColor="#ffffff"
            android:textSize="18sp"
            />
    <ListView
            android:background="#ffffff"
            android:cacheColorHint="#ffffffff"
            android:id="@android:id/list"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            />
</LinearLayout>

items_from_topic_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:padding="5dip"
        >
    <LinearLayout
            android:gravity="center"
            android:layout_height="60dip"
            android:layout_width="fill_parent"
            android:orientation="horizontal"
            >
        <LinearLayout
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:minHeight="55dip"
                android:orientation="horizontal"
                android:padding="5dip"
                >
            <WebView
                    android:id="@+id/item"
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent"
                    android:scrollbars="none"
                    />
        </LinearLayout>
        <ImageView
                android:id="@+id/disclosure"
                android:layout_height="29dip"
                android:layout_width="29dip"
                android:paddingRight="5dip"
                android:src="@drawable/disclosure"
                />
    </LinearLayout>
</LinearLayout>
4

2 回答 2

2

如果将可聚焦元素添加到 ListView,则会导致 ListView 项目不再可选择。尝试android:clickable = false在您的 WebView 上进行设置。

于 2010-10-20T20:40:00.033 回答
0

只需让您的 onTouch 事件做任何您想做的事情。

例如:我的列表项由 TextView、WebView 和线性布局组成:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="66dp"
    android:orientation="vertical"
    android:id="@+id/layout">

    <TextView
        android:layout_width="wrap_content"
        android:id="@+id/textView"
        android:layout_height="wrap_content" />

    <WebView
        android:id="@+id/webvie"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </WebView>
</LinearLayout>

我想在点击操作上执行 - 在整个列表项上。

 holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //do stuff
            }
        });

但是当我运行我的应用程序时,只有 texview 是可点击的。该怎么办?在 webView 上设置 onTouch 监听器:

holder.webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                holder.layout.callOnClick();
                return true;
            }
        });

仅支持 API 15+ 强硬...

于 2018-02-06T21:09:49.047 回答