My listview is click-free

Asked

Viewed 438 times

0

I don’t know why , but the items don’t click.

My layout:

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:src="@drawable/manager"
        android:background="#9b1136">
    </ImageView>

    <ListView
        android:layout_marginTop="10dp"    
        android:id="@+id/lista"            
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>   

</LinearLayout>

Activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ActionBar bar = getActionBar();
    bar.hide();

    listView = (ListView) findViewById(R.id.lista);

    listView.setOnItemClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

    Toast.makeText(DiretorioView.this, "teste", 1000).show();   

}

Items

<?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="horizontal">

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="5sp">

        <Button
            android:id="@+id/imageview"
            android:layout_marginLeft="20dp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:drawableTop="@drawable/lixo"
            android:text="Button" />

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="20dp"
            android:layout_marginLeft="14sp"
            android:textStyle="bold"
            android:textColor="#FFFFFF"
            android:gravity="center_vertical"/>         

    </LinearLayout>
</LinearLayout>
  • Debugged to see if you pass the method onItemClick ?

  • not enough there, I played a log there and nothing

  • War edition was fine, that’s how I wanted to put?

  • It’s nice to meet you

1 answer

1


This problem occurs when you have an item on your list that has a focus, (your Button) what causes not to call the method OnItemClickListener (see this bug reported here)

This is not to say that you cannot have a button within a list, you can use (depending on your need):

1) Insert the parameter android:descendantFocusability="blocksDescendants" on the parent of your item in the list, and implement the button click normally on your Adapter, for example:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
            Button btn= (Button) convertView.findViewById(R.id.youtButton);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ...
                }
            });
...
}

2) Place inside your tag Button the parameter android:focusable="false" and android:focusableInTouchMode="false"

  • It worked by putting item 2 in the boot, but it ignores the boot, how to treat both? want q I post my Adapter?

  • Remove item 2, place android:descendantFocusability="blocksDescendants" in your item and set the OnClickListener normally on your Adapter and try again

  • when I put android:descendantFocusability="blocksDescendants" in the boot, do not click

  • It is not on the button, it is on the father of your item. In your case, your LinearLayout

  • ready, and now the treatment of the boot in the Adapter?

  • I do not understand much how I will know to differentiate , and seprar the clicks of the button and the list

  • I edited my answer, made it a little clearer and with example

  • My button will be to delete an item from the list, as I do to pick the selected item (string) from the list and put in the button as the event will know?

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.