【问题标题】:android how to get elements from listview headerandroid如何从listview标题中获取元素
【发布时间】:2013-01-24 20:03:06
【问题描述】:

我有这个 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:layout_marginTop="20dip"
        android:gravity="center_horizontal"
        android:text="@string/app_name"
        android:textSize="25dip"
        android:textStyle="bold"
        android:typeface="serif" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="150dip"
        android:layout_marginBottom="10dip"
        android:contentDescription="@string/iv_restaurantImage"
        android:src="@drawable/ic_launcher" />


    <ListView
        android:id="@+id/lv_restaurant_description_information"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

列表视图是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dip"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:typeface="serif"
        android:textSize="15dip" />

    <TextView
        android:id="@+id/tv_value"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="@string/IV_LOGO_DESCRIPTION"
        android:textSize="20dip" />

</LinearLayout>

列表视图标题为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rl_simple_list_item_header"

    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/orderMeal"
        android:layout_marginLeft="20dip"
        android:id="@+id/tv_restaurant_description_orderMeal"
        android:drawableTop="@drawable/order_meal" />
    <TextView 
        android:id="@+id/iv_simple_list_item_header_favorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/favorite"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dip"
        android:drawableTop="@drawable/favorite"/>
</RelativeLayout>

在 java 活动中,我像这样将标题添加到列表视图中

lv_restaurantInformation = (ListView) findViewById(R.id.lv_restaurant_description_information);
        View header = LayoutInflater.from(this).inflate(
                R.layout.simple_list_item_header, null);
        tv_favorite = (TextView) header
                .findViewById(R.id.iv_simple_list_item_header_favorite);
        lv_restaurantInformation.addHeaderView(header);

一切都很好,应用程序正在运行,现在我想获取列表视图标题上的元素,如何?以及如何为它们设置 onclick 监听器?

【问题讨论】:

  • 我喜欢你的代码组织

标签: android android-layout android-listview android-activity


【解决方案1】:

看起来你已经在这样做了。

tv_favorite = (TextView) header
            .findViewById(R.id.iv_simple_list_item_header_favorite);

// now just add a click listener.
tv_favorite.setOnClickListener(new View.OnClickListener() .... );

【讨论】:

  • 这就是让我承认的原因,我完全按照你写的做,点击文本框时没有得到任何结果
  • 然后有东西拦截了点击。我不确定那会是什么。
  • 是的,它有效,我只是重新启动 eClipse :),将接受您的回答
猜你喜欢
  • 2021-09-04
  • 1970-01-01
  • 2012-10-27
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多