Can't click on ListView whose rows are made of WebViews
I have an Activity which extends ListView The ListView that I use is an extension of ArrayAdapter and each row of the ListView primarily consists of a WebView
The problem is that touching the WebView does not call OnListItemClick.
Any thoughts on how I can be able to touch the webview and call OnListItemClick? Or is there something else that I'm doing wrong? Thanks!
Here is a skeleton of the Activity class, along with the relevant XML files for the layout
PS, I've tried setting android:clickable="false" in my layout, but this doesn't let me click on the rows of my ListView. Interestingly, I can still scroll through the list, but I just can't click!
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_widt开发者_如何学Ch="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>
If you add a focusable element to a ListView, it causes the ListView items to no longer be selectable. Try setting android:clickable = false
on your WebView.
Just make your onTouch event to do whatever you want.
e.g: My list item consists of TextView, WebView and Linear layout:
<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>
and I want to perform on click action - on the whole listitem.
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do stuff
}
});
But when I run my app, only texview is clickable. What to do? Set up onTouch listener on webView:
holder.webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
holder.layout.callOnClick();
return true;
}
});
supports only API 15+ tough...
精彩评论