1

I'm trying to make a list, much like in reddit, where there is a upvote button on the left and some text to the right of that (a title, user who asked, number answers).

I'm trying to make a list containing viewgroups like so: list item row example

I haven't been able to add that Upvote button on the left side of the image. Whenever I add a button there, I can no longer click on the list item itself (previously if you clicked on the list item it would take you to another intent)

How can I create a list, where each item of the list is clickable to go to an intent based off that row item, but also have an upvote button for each row item of the list?

Here is what I am currently at:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingTop="2dip"
    android:paddingBottom="2dip"
    >
    <Button
        android:layout_width="16dip"
        android:layout_height="8dip"
        android:layout_marginLeft="12dip"
        android:layout_marginRight="12dip"
        android:layout_marginTop="4dip"
        android:background="@drawable/vote_up_gray"
        android:id="@+id/vote_up_button"/>

    <TextView
        android:layout_width="40dip"
        android:layout_height="32dip"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/vote_up_button"

        android:singleLine="true"
        android:ellipsize="marquee"
        android:gravity="center"
        android:textSize="14sp"
        android:textStyle="bold"
        android:text="0"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/Q_list_descriptions">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/title_text_view"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text=""
            android:id="@+id/askedBy_text_view" />

        <TextView
            android:layout_width="fill_parent"
            android:gravity="right"
            android:layout_height="wrap_content"

            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text=""
            android:id="@+id/answers_text_view" />

    </LinearLayout>

</LinearLayout>

</LinearLayout>

Pseduosance
  • 305
  • 1
  • 5
  • 16
  • add `android:descendantFocusability="blocksDescendants"` to the root element of your row and try again – Blackbelt Jul 04 '15 at 06:14
  • In set attribute android:focusable="false" – Raj Jul 04 '15 at 06:17
  • @Blackbelt just tried that, still when I click on the actual row item, nothing happens – Pseduosance Jul 05 '15 at 19:30
  • @Raj Ok, doing this made it so clicking on the actual row item takes me where I want to go, but now one can't click the upvote button. Anywhere I click is like I clicked on the row item – Pseduosance Jul 05 '15 at 19:31
  • how can't click? please post your code of ListView implementation. I guess you extended ListFragment for this purpose. All you have to do now is Define a custom adapter for your ListView. If your custom adapter extends ArrayAdapter then in overriden getView() method you need to find Button's reference by using findViewById() and setOnclickListener(...) I've tried it and it's working well. post your java code. Something seems wrong in that. – Raj Jul 05 '15 at 22:47
  • @Raj Oh, I had my click listener in the activity that contained the listview, but not in the adapter itself. Let me try that and see if it solves my issue – Pseduosance Jul 09 '15 at 00:30

1 Answers1

1

Use RecyclerView instead. Never had such problems with it. You can specify OnClickListener for each item in your list_item, and also for the list_item as a whole, inside a ViewHolder in your adapter.

https://www.codexpedia.com/android/defining-item-click-listener-for-recyclerview-in-android/

SuperHaker
  • 178
  • 1
  • 15