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 to an arraylist (this is the function of clicking a ListView item ), the function of the button that has in each ListView item is, if clicked goes to another screen to show the complete description of the item, the problem is, without this ImageButton in my ListView Layout, the items are correctly selected, changing from When you click on the ListView Item nothing happens but when you click the button it changes the screen correctly.It follows an image of the screen of the ListView. app, in this case the FloattingActionButton is working correctly the problem is with the items in the list view.
item_exist_list.xml
<?xmlversion="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>
SupermarketActivity.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;
}
}