Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android webview causing click listener not to trigger

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>
like image 405
Dave Avatar asked Dec 08 '25 06:12

Dave


1 Answers

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.

like image 110
Sam Avatar answered Dec 10 '25 22:12

Sam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!