0

Estoy creando una mini aplicacion con un listview y un adaptador. El adaptador tiene incorporado un imageButton para cada item y el problema es que no consigo que se execute el click en el item. Solo responde el click en el imagebutton. He buscado y añadido las etiquetas:

android:focusable="false" android:clickable="false"

Pero sigue sin funcionar el click en el item. Alguien sabe donde puedo estar cometiendo el error? Gracias! :)

Activity Clientes:

public class ClientesActivity extends AppCompatActivity {

private RealmResults<Clientes> clientes;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clientes);

    //Db Realm
    Realm realm = Realm.getDefaultInstance();
    clientes = realm.where(Clientes.class).findAll();

    //Adaptador
    AdaptadorClientes adaptadorClientes = new AdaptadorClientes(this, clientes, R.layout.adaptador_clientes);
    ListView listclientes = (ListView) findViewById(R.id.list_clientes);

    //Aplicar llista a l'adaptador
    listclientes.setAdapter(adaptadorClientes);

    //Aplicar codi per seleccionar un Client
    listclientes.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> adapterView, View v, int position, long id){

            //Aconseguim id de la BD
            int idselec = clientes.get((int) id).getId();
            String strid = Integer.toString(idselec);

            // Starting new intent
            Intent i = new Intent(getApplicationContext(),VerClienteActivity.class);
            // sending pid to next activity
            Bundle extras = new Bundle();
            extras.putString("idSelected", strid);
            i.putExtras(extras);
            // starting new activity and expecting some response back
            startActivity(i);
        }
    });

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(ClientesActivity.this, NuevoCliente.class);
            startActivity(intent);
        }
    });

}
}

Adaptador clientes:

public class AdaptadorClientes extends BaseAdapter {

private Context context;
private List<Clientes> list;
private int layout;

public AdaptadorClientes(Context context, List<Clientes> clientes, int layout) {

    this.context = context;
    this.list = clientes;
    this.layout = layout;
}

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

@Override
public Clientes getItem(int position) { return list.get(position);
}

@Override
public long getItemId(int id) {
    return id;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {

    ViewHolder vh;
    if (convertView == null) {
    convertView = LayoutInflater.from(context).inflate(layout, null);
    vh = new ViewHolder();
        vh.cliente = (TextView) convertView.findViewById(R.id.text_cliente);
        vh.ciudad = (TextView) convertView.findViewById(R.id.text_ciudad);
        vh.telefono = (ImageButton) convertView.findViewById(R.id.imgbtn_tel);
        vh.telefono.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Telefon: " , Toast.LENGTH_SHORT).show();
            }
        });
        convertView.setTag(vh);
}
else {
        vh = (ViewHolder) convertView.getTag();
    }

    Clientes clientes = list.get(position);
    vh.cliente.setText(clientes.getCliente());
    vh.ciudad.setText(clientes.getCiudad());
    return convertView;
}

private class ViewHolder {
        TextView cliente;
        TextView ciudad;
        ImageButton telefono;
    }
}

XML adaptador:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/imgbtn_tel"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:focusable="false"
        android:clickable="false"
        android:src="@android:drawable/stat_sys_phone_call" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text_cliente"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Nombre Cliente"
            android:textSize="24sp"
            android:textStyle="normal|bold" />

        <TextView
            android:id="@+id/text_ciudad"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Ciudad"
            android:textSize="18sp" />

    </LinearLayout>

</LinearLayout>

1 respuesta 1

1

Me paso algo parecido con un Spinner, yo estaba usando un Spinner con elementos los cuales tienen un botón y un textview logre hacer funcionar el boton sin problema, la cuestión era que no podia seleccionar los elementos dentro del propio Spinner, lo solucione añadiendo la siguiente linea al Layout Padre:

android:descendantFocusability="blocksDescendants"

En tu caso sería en el primer LinearLayout

Espero que funcione Saludos!

1
  • Muchas gracias! Funciona perfecto!
    – Ferran
    el 8 ene. 2018 a las 20:39

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.