ListView + ImageButton + descendantFocusability

user1959076 picture user1959076 · Jul 6, 2013 · Viewed 8.7k times · Source

I have a listview to which I add rows with 1 imagebutton .. I tried to set the imagebutton the setfocusable false but still not working ..

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="160dp"
          android:descendantFocusability="blocksDescendants"
          android:id="@+id/RL_item">

    <ImageButton
            android:layout_width="200dp"
            android:layout_height="fill_parent"
            android:id="@+id/imageButton"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true" />

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/LL_Installed"
            android:layout_alignRight="@+id/textView2"
            android:layout_alignBottom="@+id/textView"
            android:layout_alignParentBottom="true"
            android:paddingBottom="@dimen/item_list_left_right"
            android:paddingLeft="@dimen/item_list_left_right"
            android:focusable="false"
            android:focusableInTouchMode="false">

        <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:id="@+id/imageView"
                android:background="@drawable/tick"
                android:focusable="false"
                android:focusableInTouchMode="false"/>

        <TextView

                android:layout_height="fill_parent"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/list_item_installed"
                android:id="@+id/textView3"
                android:paddingLeft="6dp"
                android:textColor="#ffffff"
                style="@style/TextShadow"
                android:gravity="center_vertical|fill_vertical"
                android:layout_weight="1"
                android:layout_width="0dip"
                android:focusable="false"
                android:focusableInTouchMode="false"/>

    </LinearLayout>

    <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:visibility="invisible"
            android:focusable="false"
            android:focusableInTouchMode="false"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text=""
            android:id="@+id/textView2"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            style="@style/TextShadow"
            android:paddingTop="@dimen/item_list_top"
            android:paddingRight="@dimen/item_list_left_right"
            android:focusable="false"
            android:focusableInTouchMode="false"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text=""
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            style="@style/TextShadow"
            android:layout_toLeftOf="@+id/textView2"
            android:paddingLeft="@dimen/item_list_left_right"
            android:paddingTop="@dimen/item_list_top"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
</RelativeLayout>

Item_ListAdapter.java

public class Item_ListAdapter extends BaseAdapter {


private final Activity activity;
private final ArrayList<Item_List> items;
private View vi;
private ImageButton button;

public Item_ListAdapter(Activity activity, ArrayList<Item_List> items) {
    this.activity = activity;
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int position) {
    return items.get(position).getId();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    vi=convertView;

    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi = inflater.inflate(R.layout.item_list, null);


    }

    Item_List item = items.get(position);


    // Colocar el titulo
    // Colocar la fecha
    // Colocar el background
    // Hacer visible o invisible el layout de instalado


    TextView txtTitulo = (TextView) vi.findViewById(R.id.textView);
    TextView txtFecha = (TextView) vi.findViewById(R.id.textView2);
    LinearLayout LL_Installed = (LinearLayout) vi.findViewById(R.id.LL_Installed);

    txtTitulo.setText(item.getTitle());
    txtFecha.setText(item.getFecha());

    if (item.getInstalled()) {
        LL_Installed.setVisibility(View.VISIBLE);


        Resources res = vi.getResources();
        Bitmap bitmap = BitmapFactory.decodeFile(item.getRutaImagen());
        final BitmapDrawable bd = new BitmapDrawable(res, bitmap);

        // ----------------------------------


        button = (ImageButton) vi.findViewById(R.id.imageButton);
        //button.setBackgroundDrawable(bd);
        button.setBackground(bd);
        button.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.getBackground().setColorFilter(Color.parseColor("#B7B2B0"), PorterDuff.Mode.MULTIPLY);
                    Log.d("aaa","DOWN");
                    return true;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    v.getBackground().clearColorFilter();
                    v.invalidate();
                    Log.d("aaa","UP");
                    return true;
                }
                return false;
            }
        });


    } else {
        LL_Installed.setVisibility(View.INVISIBLE);
    }

    return vi;
}

This is my 'main' code.. i want detect the press here

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


    Item_ListAdapter adapter = new Item_ListAdapter(ListActivity.this, items);
    lv.setAdapter(adapter);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);


    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parnet, android.view.View view, int position, long id) {


            // Que item ha sido pulsado
            Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
            Log.d("aaa", String.valueOf(position) );

        }
    });

Answer

SBerg413 picture SBerg413 · Jul 6, 2013

I believe in order to have BOTH the listView row and the ImageButton clickable, you'll need to set both of these on the ImageButton object:

  android:focusable="false"
  android:focusableInTouchMode="false"

Give that a shot. I'd also try using an onClickListener on your ImageButton instead of the touchListener.