【问题标题】:Android Listview with upvote button on leftAndroid Listview,左侧有upvote按钮
【发布时间】:2015-09-21 22:14:59
【问题描述】:

我正在尝试制作一个列表,就像在 reddit 中一样,左侧有一个upvote 按钮,右侧有一些文本(标题、提问的用户、数字答案)。

我正在尝试创建一个包含如下视图组的列表:

我无法在图片左侧添加支持按钮。 每当我在那里添加一个按钮时,我就不能再单击列表项本身(以前如果您单击列表项,它会带您进入另一个意图)

我如何创建一个列表,其中列表的每个项目都可以点击以根据该行项目转到一个意图,而且列表的每个行项目也有一个赞成按钮?

这是我目前所在的位置:

<?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>

【问题讨论】:

  • android:descendantFocusability="blocksDescendants" 添加到行的根元素并重试
  • 设置属性android:focusable="false"
  • @Blackbelt 刚刚尝试过,当我点击实际的行项目时,仍然没有任何反应
  • @Raj 好的,这样做可以让我点击实际的行项目将我带到我想去的地方,但现在无法点击 upvote 按钮。我点击的任何地方就像我点击了行项目
  • 怎么不能点击?请发布您的 ListView 实现代码。我猜您为此目的扩展了 ListFragment。您现在要做的就是为您的 ListView 定义一个自定义适配器。如果您的自定义适配器扩展了 ArrayAdapter,那么在覆盖的 getView() 方法中,您需要使用 findViewById() 和 setOnclickListener(...) 找到 Button 的引用,我已经尝试过了,它运行良好。发布您的Java代码。这似乎有些不对劲。

标签: android android-layout listview android-listview vote-up-buttons


【解决方案1】:

请改用RecyclerView。从来没有遇到过这样的问题。您可以为list_item 中的每个项目指定OnClickListener,也可以在适配器的ViewHolder 中为整个list_item 指定OnClickListener

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多