在 Adapter 上从 Fragment 获取字段,在 Fragment 上从 Adapter 获取 List

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

我正在制作一份 checkbox 的列表,在我的 adapter 上有一个项目,我的 fragment 在列表的顶部有一个复选框,在 "Select All" 上还有列表底部一个textView "x Itens Selected"加上一个button.

我的问题 1 是如何得到这个 "x":选中复选框的数量,因为它们在 AdapterTextViewFragment,我无法在 Adapter 上设置它。 为此,我使用了复选框列表中的 setOnCheckedChangeListener。 我试过很多方法从 Adapter 获取 Fragment 但没有成功。在构造函数上发送,从 viewHolder 获取,从 getSupportFragmentManager()getFragments()getFragmentByIdgetFragmentByTag 获取。 我也试过 FragmentObserver,但没用。

问题 2:所以我尝试在 Adapter 上列出所有选中的 Itens,但我需要将其发送到 Fragment 进行管理。另外,我无法从 Fragment 上的 Adapter(在 onCreateView 上)获得它。

有人可以帮忙吗?

我的Adapter.javaonBindViewHolder:

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

我的XMLAdapter:

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

我的XMLFragment:

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

您可以使用界面来完成。 创建您的接口 class,在适配器 class 中初始化它并在您的片段中实现它。因此,当您需要将任何值发送到片段时,请使用您在接口中创建的方法将数据发送到片段。您将在片段中获得选中的复选框值。 Refer this link。希望对您有所帮助。