How to make FAB overwrite items in a Listview

Asked

Viewed 444 times

1

I have an Activity where I inflate custom items into a Listview. In setContentView() of this Activity I put a file that has only one Listview declared.

 <ListView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/list_exercicios"
     android:orientation="vertical"
     android:clickable="true"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>

In another XML file I create the layout of the items that go in Listview.

<LinearLayout
    android:id="@+id/exercicios_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/exercicios_background"
    android:orientation="vertical">

    <TextView
        android:id="@+id/pergunta"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/title_exercicios_background"
        android:gravity="center"
        android:text="Aqui vai a pergunta"
        android:textColor="@color/letras_exercicios"
        android:textSize="25sp" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radio_button_a"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Aqui vai a perunta"
            android:textColor="#FFFFFF"
            style="@android:style/Widget.CompoundButton.CheckBox"/>
        <RadioButton
            android:id="@+id/radio_button_b"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Aqui vai a perunta"
            android:textColor="#FFFFFF"
            style="@android:style/Widget.CompoundButton.CheckBox"/>
        <RadioButton
            android:id="@+id/radio_button_c"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Aqui vai a perunta"
            android:textColor="#FFFFFF"
            style="@android:style/Widget.CompoundButton.CheckBox"/>
        <RadioButton
            android:id="@+id/radio_button_d"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Aqui vai a perunta"
            android:textColor="#FFFFFF"
            style="@android:style/Widget.CompoundButton.CheckBox"/>
        <RadioButton
            android:id="@+id/radio_button_e"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Aqui vai a perunta"
            android:textColor="#FFFFFF"
            style="@android:style/Widget.CompoundButton.CheckBox"/>

    </RadioGroup>

    <TextView
        android:id="@+id/finished_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/terminei_background"
        android:text="@string/txt_responder"
        android:textSize="25sp"
        android:textColor="#FFFFFF"
        android:gravity="center"
        android:onClick="terminou"/>

</LinearLayout>

The problem is that if I put the FAB in this file it appears in all the items and it doesn’t work, and if I put it in the Listview file it gives error.

2 answers

1


You must use Constraintlayout or Framelayout Something like:

<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="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView 
             android:id="@+id/list_exercicios"
             android:orientation="vertical"
             android:clickable="true"
             android:layout_width="match_parent"
             android:layout_height="match_parent"/>

       <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="16dp"
            android:src="@drawable/ic_done" />

    </FrameLayout>

</LinearLayout>

0

Try it this way:

XML

<ListView
    android:id="@+id/list_exercicios"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true" />



<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:clickable="true"
    android:id="@+id/fab"
    android:src="@mipmap/ic_icon"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    />

Activity

public class SuaActivity extends AppCompatActivity {

   private FloatingActionButton fab;
    private ListView listaExercicio;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seu_xml);
         // Referencia da Lista
        listaExercicio = ListView.class.cast(findViewById(R.id.list_exercicios));
         // Referencia do Floating Action Button
        fab = FloatingActionButton.class.cast(findViewById(R.id.fab));
    }

}

Browser other questions tagged

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