Problem listview with button and item

0

I am creating a mini application with a listview and an adapter. The adapter has a built-in imageButton for each item and the problem is that I can not execute the click on the item. Just answer the click on the imagebutton. I searched and added the tags:

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

But the click on the item still does not work. Does anyone know where I may be making the mistake? Thank you! :)

Activity Clients:

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);
        }
    });

}
}

Client adapter:

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 adapter:

<?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>

    
asked by Ferran 08.01.2018 в 21:06
source

1 answer

1

Something similar happened with a Spinner, I was using a Spinner with elements which have a button and a textview I managed to operate the button without problem, the issue was that I could not select the elements within the Spinner itself, solve it adding the following line to the Father Layout:

android:descendantFocusability="blocksDescendants"

In your case it would be in the first LinearLayout

I hope it works Greetings!

    
answered by 08.01.2018 / 21:31
source