android如何从listview header中获取元素

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

我有这个 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>

ListView 是

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

ListView 标题是:

<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 Activity 中,我像这样将 header 添加到 ListView

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);

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

最佳答案

看起来你已经在做了。

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() .... );

关于android如何从listview header中获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14509737/

相关文章:

android - 'void android.view.inputmethod.InputConnection.closeConnection()' 上的 NullPointerException

Android:AlertDialog - 用户点击其他地方

android - 如何取消场景转换动画?

android - View 的 getWindowToken() 在 windowManager.addView() 之后为 null

android - 如何在 GridView 中的不同图标之间平滑移动焦点

java - 如何在 Android Wear 上调整下巴布局?

android - ListView 在 onCreate 中两次添加项目

Android wear Wearable ListView ImageView 选择器

java - 带有 OnScrollListener 的水平 ListView

android - 我可以用一个 View 创建一个没有包含布局的 xml 文件吗