3

I have a ListView that has a list of articles. Every X articles (10 currently), I have a WebView that shows an ad. I can click on any list item EXCEPT the one just above the one where the WebView is displayed. That one won't register my click.

Code that shows or hides the WebView:

if(position % 10 == 0) // || position == 0) && position > 0
{
    viewHolder.adView.loadUrl(tmpAdURL);
    viewHolder.adView.setVisibility(View.VISIBLE);
}
else
{
    viewHolder.adView.setVisibility(View.GONE);
    viewHolder.adView.invalidate();
}

main.xml

<ListView android:id="@+id/articles_list"
    style="@style/ListView"
    android:fadingEdge="vertical"
    android:fadingEdgeLength="16dp"
    android:divider="@color/extreme_light_gray"
    android:dividerHeight="1dp" />

Listener in MainActivty.java's onCreate():

articleEntryListView.setOnItemClickListener(new OnItemClickListener()
{
    public void onItemClick(AdapterView<?> parent, View v, int position, long id)
    {
        Log.d("MINE", "ARTICLE CLICKED");
    }
}

article_entry_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minHeight="60dp" >

    <!-- Title of the news entry -->

    <ImageView
        android:id="@+id/article_thumbnail"
        android:layout_width="90dp"
        android:layout_height="80dp"
        android:layout_alignParentRight="true"
        android:adjustViewBounds="true"
        android:contentDescription="@string/articleThumbnail"
        android:cropToPadding="true"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:paddingLeft="0.5dp"
        android:scaleType="centerCrop" />
        <!-- WAS: android:padding="0.5dp" -->



    <LinearLayout
        android:id="@+id/article_row"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@id/article_thumbnail"
        android:minHeight="80dp"
        android:orientation="vertical"
        android:padding="10dp" >

        <TextView
            android:id="@+id/article_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/articleTitle"
            android:textColor="@drawable/list_title_selector"
            android:textSize="14sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/article_subtitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/articleSubtitle"
            android:textColor="@drawable/list_subtitle_selector"
            android:textSize="10sp" />
    </LinearLayout>

    <WebView
        android:id="@+id/article_ad"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:background="@color/meddark_gray"
        android:layout_below="@+id/article_thumbnail"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:padding="10dp" />

</RelativeLayout>
Sam
  • 86,580
  • 20
  • 181
  • 179
Dave
  • 28,833
  • 23
  • 113
  • 183

2 Answers2

2

You are wasting resources using one row layout. Think of it: you create a WebView in all of the rows, but you immediately hide them (over) 90% of the time... Instead you should use a custom adapter with two row layouts by overriding these methods:

@Override
public int getItemViewType(int position) {
    return position % 11 == 0 ? 1 :  0;
}

@Override
public int getViewTypeCount() {
    return 2;
}

Next inside getView() or newView() check which type of layout you should load. Inflate the news item layout when getItemViewType() returns 0 and inflate a WebView (by itself) when it returns 1.

Aside from being faster and more organized, your WebView has it's own row now and should play nice.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • Totally agree, just wasn't sure how (I'm still Android-noob). Thanks - will try to implement this and check back asap. – Dave Dec 03 '12 at 19:33
0

This may be similar to the issue I had with a CheckBox in a list item. Try using the android:focusable="false" option for the WebView in your list item XML.

In my case the CheckBox was taking focus from the list items and it prevented the event from firing for some reason.

http://developer.android.com/reference/android/view/View.html#attr_android:focusable

<WebView
    android:id="@+id/article_ad"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:background="@color/meddark_gray"
    android:layout_below="@+id/article_thumbnail"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:padding="10dp"
    android:focusable="false" />

You're also able to do this in the code behind with

setFocusable(false);
Kirk
  • 16,182
  • 20
  • 80
  • 112