views:

59

answers:

2

I add a listview via Java into @+id/View01. For some reason the items in the listview don't accept clicks. Does anybody have ideas about possible causes?

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:layout_height="fill_parent"
  android:background="@color/light_blue"
  >
    <Button android:text="@+id/Button01" 
        android:id="@+id/Button01" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <AbsoluteLayout 
    android:id="@+id/Layout01" 
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_below="@+id/Button01"        
    android:layout_above="@+id/Button02"
    android:layout_height="fill_parent">

        <View android:id="@+id/View01" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
        />
        <ListView android:id="@+id/ListView02" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"/>
    </AbsoluteLayout>
    <Button android:text="@+id/Button02" 
        android:id="@+id/Button02" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>

If I change my code to the following the clicks work but the layout doesn't look the way I want.:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:layout_height="fill_parent"
  android:background="@color/light_blue"
  >
    <Button android:text="@+id/Button01" 
        android:id="@+id/Button01" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <View android:id="@+id/View01" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_below="@+id/Button01"
        android:gravity="center_horizontal"
        />
    <ListView android:id="@+id/ListView02" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_below="@+id/View01"
        android:gravity="center_horizontal"
        android:layout_above="@+id/Button02"/>
    <Button android:text="@+id/Button02" 
        android:id="@+id/Button02" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>

One of the functions involved:

protected void initiateExtraView(FillInput fillInput){
    View extraView = fillInput.getExtraView();
    ViewGroup parent = (ViewGroup) extraView.getParent();
    int index = parent.indexOfChild(extraView);
    parent.removeView(extraView);
    extraView = fillInput.getLayoutInflater().inflate(
             R.layout.extrabuttonlistview, 
             parent, 
             false);

    parent.addView(extraView, index);
    fillInput.setExtraView(extraView);

    ListView extraButtonView = (ListView) extraView;
    fillInput.setExtraButtonListView(extraButtonView);

    ListAdapter adapter = new ExtraButtonAdapter(fillInput, this);
    fillInput.getExtraButtonListView().setAdapter(adapter);     
}

The function in the ExtraButtonAdapter that create the particular view that doesn't accept clicks:

private View getNewForm(int position, View convertView,
        ViewGroup parent) {
    View view = mInflater.inflate(
            R.layout.new_form_button, 
            parent, 
            false);

    View.OnClickListener listener = new View.OnClickListener() {
        public void onClick(View v) {

            Log.i(TAG, "Click on NewForm");
        }
    };
    view.setOnClickListener(listener);

    setNewFormView(view);
    return view;
}

R.layout.extrabuttonlistview:

<?xml version="1.0" encoding="UTF-8"?>
    <ListView android:id="@+id/ListView01" 
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:clickable="true"
        android:gravity="center_horizontal"
        android:layout_above="@+id/Button02"/>
A: 

By default ListView does not pass touch events through to a child unless it can accept focus. You can change this behavior by calling setItemsCanFocus(true).

The default behavior of ListView treats items as single units and dispatches click events using an OnItemClickListener attached to the ListView itself.

adamp
Calling setItemsCanFocus(true) doesn't change anything. It would also not explain why the second code works when the first doesn't. When I add an OnItemClickListener to the ListView it also doesn't receive any clicks.
Christian
A: 

Have you actually tried to replace the <View android:id="@+id/View01" with a

<ViewStub android:id="@+id/listViewStub" android:inflateId="@+id/View01" android:layout="@layout/layout/new_form_button" />

and in code you could inflate it by

ViewStub stub = (ViewStub)findViewById(R.id.listViewStub);
stub.inflate();

Seems weird that you try to add a ListView into a View element.

Advantage of inflating with ViewStub is, that once inflated the "ViewStub" Element will be completely replaced by your ListView and then be equal to your ListView02 and will reduce your Hierarchy depth by one.

ViewStub Documentation

edit: Maybe ViewGroup is more suitable then.

Tseng
I want to be able to put different stuff into the View on runtime. As far as I can see a ViewStub can't change it's content after it has been inflated.
Christian
Maybe try `ViewGroup` instead. The view class kinda seems to be the wrong class to add other views to it.
Tseng