1

I just stuck here while programming an android app.

I created a list of items based on adapter (Extends BaseAdapter), which shows items in TableLayout due to required positioning of items. Then, I add the onItemClickListener, but nothing happens...

ActivityNews.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

>

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:id="@+id/news_toolbar"
    android:elevation="4dp"
    android:background="@color/colorPrimary"
    >
</android.support.v7.widget.Toolbar>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/news_toolbar"
    android:id="@+id/newslist"
    android:background="@color/colorBackground"


    ></ListView>

</RelativeLayout>

Then, this is news_list_view.xml

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="6dp"
    android:background="@color/colorBackground"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:descendantFocusability="blocksDescendants"
    >
        <ImageView android:id="@+id/news_image"
            android:layout_alignParentStart="true"
            android:layout_marginEnd="6dp"
            android:layout_height="60dp"
            android:layout_width="60dp"
            android:clickable="false"
            android:focusable="false"
            android:layout_gravity="center"
            android:focusableInTouchMode="false"
            />
    <TableLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:clickable="false"
        android:focusable="false"

        >
        <TableRow
            android:clickable="false"
            android:focusable="false">
    <TextView android:id="@+id/news_date"
        android:layout_marginTop="4dp"
        android:layout_marginRight="4dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorDate"
        android:layout_toRightOf="@id/news_image"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textIsSelectable="false"
        />

    <TextView android:id="@+id/news_type"
        android:layout_marginTop="4dp"
        android:layout_width="match_parent"
        android:layout_toRightOf="@id/news_date"
        android:layout_height="wrap_content"
        android:textColor="@color/colorSubHeadline"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textIsSelectable="false"
        />
     </TableRow>

     <TableRow
         android:clickable="false"
         android:focusable="false">
    <TextView android:id="@+id/news_headline"
        android:layout_marginTop="4dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="wrap_content"
        android:layout_below="@id/news_type"
        android:layout_height="wrap_content"
        android:textColor="@color/colorHeadline"
        android:layout_span="2"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
         android:textIsSelectable="false"/>
     </TableRow>
    </TableLayout>
</LinearLayout>

</android.support.v4.widget.NestedScrollView>

and the snippet from NewsActivity

 lv = (ListView) findViewById(R.id.newslist);
    adapter = new NewsAdapter(this);
    lv.setAdapter(adapter);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(NewsActivity.this, "You Clicked ", Toast.LENGTH_SHORT).show();
        }
    });

Any ideas?

While researching about it, I found tips like setting clickable=false or descendantFocusability for first layout below ListView, but none worked...

Thanks!

Daniel

1
  • You have scrollview inside every item in your listview? Commented Feb 25, 2017 at 21:21

1 Answer 1

0

You're using CHOICE_MODE_SINGLE which tells the ListView you are expecting clicks to change items' selection not to just register a click event.

If that is what you want then your list items need to implement the Checkable interface and have the display change by using a state selector.

Otherwise you can remove the lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); line completely

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.