0

我有一个带有 relativelayout 标题的列表视图,当我单击 relativelayout 中的任何空间时,它会变成白色,我不想要那个白色,如果用户单击该空间,则不会发生任何事情,但是如果他单击图像进入那个relativelayout 有我可以在 onclick 中处理它的操作。

这是相对布局

<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:focusable="true"
    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>

编辑

01-25 21:00:42.718: E/AndroidRuntime(24121): FATAL EXCEPTION: main
01-25 21:00:42.718: E/AndroidRuntime(24121): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Syriatel.EatTel/com.Syriatel.EatTel.Restaurant_Description}: java.lang.NullPointerException
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.os.Looper.loop(Looper.java:130)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.main(ActivityThread.java:3701)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at java.lang.reflect.Method.invoke(Method.java:507)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at dalvik.system.NativeStart.main(Native Method)
01-25 21:00:42.718: E/AndroidRuntime(24121): Caused by: java.lang.NullPointerException
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.Syriatel.EatTel.Restaurant_Description.initialize(Restaurant_Description.java:46)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.Syriatel.EatTel.Restaurant_Description.onCreate(Restaurant_Description.java:35)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
01-25 21:00:42.718: E/AndroidRuntime(24121):    ... 11 more
4

3 回答 3

2

设置布局可点击,不带任何 onClickListener

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/info1"
            android:clickable="true">
于 2015-09-26T14:24:31.753 回答
1

如果您将 onclicklistener 分配给实际项目,而不是在活动中实现它,这应该不是问题。

tv_favorite = (TextView) header.findViewById(R.id.iv_simple_list_item_header_favorite);
tv_favorite.setOnClickListener( new OnClickListener(){
    @Override
public void onClick(View v) {
        tv_favorite.setText("asdfasdfasdf");
}

});

如果您想要 ListView 中的单个项目(而不是标题),您应该使用为整个 ListView 设置的 OnItemClickListener。

于 2013-01-24T18:38:23.330 回答
1

尝试让你的RelativeLayout焦点集中:

<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"
    android:focusable="true"
    android:focusableInTouchMode="true" >

更新

如果你想TextView在你的标题内点击,你可以这样做:

View header = LayoutInflater.from(this).inflate(R.layout.simple_list_item_header, null);
View favorite = header.findViewById(R.id.iv_simple_list_item_header_favorite);

lv_restaurantInformation = (ListView) findViewById(R.id.lv_restaurant_description_information);
lv_restaurantInformation.addHeaderView(header);

favorite.setOnClickListener(this);

然后在onClick方法中处理它Activity

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.iv_simple_list_item_header_favorite:
        Toast.makeText(this, "onClick", Toast.LENGTH_SHORT).show();
        break;

    default:
        break;
    }
}
于 2013-01-24T18:21:12.763 回答