mardi 7 août 2018

Get a field from Fragment on Adapter and get List from Adapter on Fragment

I'm making a list of checkbox with itens on my adapter and my fragment has one checkBox on the top of the list with "Select All" and on the bottom of the list a textView "x Itens Selected" plus a button.

My problem 1 is how to get this "x": the count of how many checkboxes are checked, because they are on Adapter and the TextView is on Fragment, I can't set it on Adapter. To get this, I'm using a setOnCheckedChangeListener from the list of checkboxes. I've tried to get Fragment from Adapter with a lot of ways but without success. Sending on constructor, getting from viewHolder, getting from getSupportFragmentManager(), getFragments(), getFragmentById, getFragmentByTag. I've tried FragmentObserver also, but it didnt work.

Problem 2: So I've tried to make a list on Adapter with all the checked Itens, but I need to send it to Fragment to manage it. Also, I can't get it from Adapter on Fragment (on onCreateView).

Can someone help please?

My Adapter.java onBindViewHolder:

@Override
public void onBindViewHolder(final PedidoItemRefugadoViewHolder holder, final int position) {
    //pedidoItem is each object from the list
    final PedidoItem pedidoItem = itensRefugados.get(position);

    //checkCadaItemRefugado is each checkbox from Adapter
    holder.checkCadaItemRefugado.setText(pedidoItem.getItemPedidoObservacao(context));

    holder.checkCadaItemRefugado.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int count = 0;
            int size = itensRefugados.size();
            for (int i=0; i<size; i++){
                if (holder.checkCadaItemRefugado.isSelected()) {
                        count++;
                        //itensRefugadosChecked is the new list with all the checked items
                        itensRefugadosChecked.add(pedidoItem);
                }
            }
        }
    });
}

My XML Adapter:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="6dp"
    app:cardElevation="3dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="false"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="6dp"
    android:foreground="?attr/selectableItemBackground">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/frame_layout_notificacao"
        android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp">

                <CheckBox
                    android:id="@+id/check_cada_item_refugado"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="Item: Pedido: \nMotivo:"/>

            </LinearLayout>
    </LinearLayout>

</android.support.v7.widget.CardView>

My XML Fragment:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="br.com.zapgrafica.appzap.fragments.ItensRefugadosFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:orientation="horizontal"
        android:layout_marginTop="8dp"
        android:layout_marginStart="7dp">

        <CheckBox
            android:id="@+id/check_itens_refugados"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Selecionar todos"/>
    </RelativeLayout>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/itens_refugados_layout_swipe_to_refresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="42dp"
        android:layout_marginBottom="66dp">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="@android:color/transparent"
            android:clipToPadding="false"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:listSelector="@android:color/transparent"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbars="vertical" />
    </android.support.v4.widget.SwipeRefreshLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_itens_refugados_total"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:layout_above="@+id/btn_itens_refugados_total"
            android:text=""/>
        <Button
            android:id="@+id/btn_itens_refugados_total"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="@string/itens_refugados_confirmar"
            android:theme="@style/ThemeButtonAjusteOnlineRefugo"/>
    </RelativeLayout>

</FrameLayout>




Aucun commentaire:

Enregistrer un commentaire