When adding Listview Selector Imagebutton to Work

Asked

Viewed 122 times

0

The list view is a list of items available to the user to select, and when the user clicks on a Listview item this item changes color and is added in an arraylist (this is the function of clicking on a Listview item), the function of the button that is available in each Listview item is, if clicked goes to another screen to show the full description of the item, the problem is, without this Imagebutton in my Listview Layout, the items are correctly selected by changing color and adding in the arraylist, by placing the info button in the Listview Layout, only the button works correctly, that is, when you click on the Listview Item nothing happens but when you click on the button it changes the screen correctly. Follow a screenshot of the app screen, in this case the Floattingactionbutton is working correctly the problem is with the list view items.

inserir a descrição da imagem aqui

item_lista_supermercado.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selected_item"
android:orientation="horizontal"
android:weightSum="1">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        app:srcCompat="@mipmap/ic_launcher" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/imageItem"
        android:orientation="vertical">

        <TextView
            android:id="@+id/nome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="50px" />

        <TextView
            android:id="@+id/preco"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        app:srcCompat="@drawable/btn_info" />

</RelativeLayout>


</LinearLayout>

activity_supermarket.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.joao.mercado.SupermercadoActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/btnConcluido"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:clickable="true"
    app:backgroundTint="@color/vermelho"
    app:fabSize="mini"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:srcCompat="@mipmap/checked_black"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintRight_creator="1" />

<ListView
    android:id="@+id/lstProdutos"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:choiceMode="multipleChoice"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>

Supermercadoactivity.java

package com.example.joao.mercado;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import java.util.ArrayList;
import java.util.List;

public class SupermercadoActivity extends AppCompatActivity {

private ListView listaProdutos;
private ArrayAdapter<String> adpDados;
private FloatingActionButton btnConcluido;


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

    final ArrayList<Item> items = new ArrayList<Item>();

    for (int i = 0; i < 10; i++){
        Item item = new Item(i,  10.01f, "Item num; " + i);
        items.add(item);
    }
    final ArrayList<Item> itemsSelecionados = new ArrayList<Item>();
    listaProdutos = (ListView) findViewById(R.id.lstProdutos);
    listaProdutos.setAdapter(new SupermercadoItemAdapter(this, items));
    listaProdutos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapter, View view,
                                int posicao, long id) {
            // TODO Auto-generated method stub
            if(!itemsSelecionados.contains(items.get(posicao))) {
                itemsSelecionados.add(items.get(posicao));
            }else{
                itemsSelecionados.remove(items.get(posicao));
            }
            Toast.makeText(SupermercadoActivity.this, "clicado", Toast.LENGTH_SHORT).show();
        }
    });

    btnConcluido = (FloatingActionButton) findViewById(R.id.btnConcluido);

    btnConcluido.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent it = new Intent(SupermercadoActivity.this, Carrinho.class);
            it.putParcelableArrayListExtra("itemsSelecionados", itemsSelecionados);
            startActivity(it);
        }
    });

}
}

selected_item.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:state_pressed="true" android:drawable="@android:color/darker_gray"/>
<item android:state_selected="true" android:drawable="@android:color/darker_gray"/>
<item android:state_activated="true" android:drawable="@android:color/darker_gray"/>
<item android:drawable="@android:color/background_light"/>
</selector>

supermarket_item_adapter

package com.example.joao.mercado;

import android.app.Activity;
import android.content.Context;
import android.view.*;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.*;

/**
 * Created by Joao on 25/08/2017.
 */

public class SupermercadoItemAdapter extends BaseAdapter {

private Context context;
private ArrayList<Item> lista;


public SupermercadoItemAdapter(Context context, ArrayList<Item> lista) {
    this.context = context;
    this.lista = lista;
}

@Override
public int getCount(){
    return lista.size();
}
@Override
public Object getItem(int position) {
    return lista.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Item item = lista.get(position);
    LayoutInflater inflater = (LayoutInflater) 
    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.item_lista_supermercado, null);
    TextView preco = (TextView) layout.findViewById(R.id.preco);
    TextView nome = (TextView) layout.findViewById(R.id.nome);

    nome.setText(item.getNome());
    preco.setText("R$: "+item.getPreco());

    return layout;
}



}
  • You are clicking right on top of ImageButton? If yes, if you click on any other part of the layout, it works?

  • What do you mean it stops working ? Please be more specific... Give some kind of error ? If so, which ?

  • When I click on the imagebutton it calls the on click function, but if I click on the listview item nothing happens.

2 answers

2


I found the solution: When you added an Imagebutton to Listview it "steals" the focus of the Listview item for itself. To solve this just add the following line in the root layout of your Listview:

android:descendantFocusability="blocksDescendants"

My problem is like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selected_item"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
android:weightSum="1">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    app:srcCompat="@mipmap/ic_launcher" />

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/imageItem"
    android:orientation="vertical">

    <TextView
        android:id="@+id/nome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="50px" />

    <TextView
        android:id="@+id/preco"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    app:srcCompat="@drawable/btn_info" />

</RelativeLayout>


</LinearLayout>
  • The click on Imagebutton continues to work?

  • 1

    Up to where I tested in the functions of my application yes.

0

Ola, From what I understand, you are trying to start an Activity by clicking on a listview item. Instead of using:

btnConcluido = (FloatingActionButton) findViewById(R.id.btnConcluido);

    btnConcluido.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent it = new Intent(SupermercadoActivity.this, Carrinho.class);
            it.putParcelableArrayListExtra("itemsSelecionados", itemsSelecionados);
            startActivity(it);
        }
    });

Try to use in your ( listaProdutos.setOnItemClickListener )

 Intent it = new Intent(SupermercadoActivity.this, Carrinho.class);
        it.putParcelableArrayListExtra("itemsSelecionados", itemsSelecionados);
        startActivity(it);

Leaving so:

    listaProdutos = (ListView) findViewById(R.id.lstProdutos);
    listaProdutos.setAdapter(new SupermercadoItemAdapter(this, items));
    listaProdutos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapter, View view,
                                int posicao, long id) {
            // TODO Auto-generated method stub
            if(!itemsSelecionados.contains(items.get(posicao))) {
                itemsSelecionados.add(items.get(posicao));
            }else{
                itemsSelecionados.remove(items.get(posicao));
            }
 Intent it = new Intent(SupermercadoActivity.this, Carrinho.class);
            it.putParcelableArrayListExtra("itemsSelecionados", itemsSelecionados);
            startActivity(it);
            Toast.makeText(SupermercadoActivity.this, "clicado", Toast.LENGTH_SHORT).show();
        }
    });
  • This is not the problem however that when a listview item is clicked it neither calls the function so Setonclicklistener.

Browser other questions tagged

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