从Fragment on Adapter获取一个字段,从Fragment上的Adapter获取List

asr*_*asr 0 checkbox android adapter fragment

我做的名单checkbox上我与itens adapter和我fragment对列表的顶部,"全选",并在列表中的底部有一个复选框textView"X Itens选择"加一个button.

我的问题1是如何获得这个"x":检查了多少个复选框的计数,因为它们已打开AdapterTextView打开Fragment,我无法设置它Adapter.为了得到这个,我在setOnCheckedChangeListener复选框列表中使用了一个.我试图通过很多方法从适配器获取Fragment,但没有成功.发送上构造,从viewHolder获得,从得到getSupportFragmentManager(),getFragments(),getFragmentById,getFragmentByTag.我也试过了FragmentObserver,但它没有用.

问题2:所以我试图Adapter用所有选中的Itens 列出一个列表,但是我需要发送它Fragment来管理它.另外,我无法从Adapteron Fragment(on onCreateView)获得它.

有人可以帮忙吗?

我的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);
                }
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

Art*_*tel 7

你可以使用界面来做到这一点.创建接口类,在适配器类中初始化它并在片段中实现它.因此,当您需要将任何值发送到片段时,请使用您在接口中创建的方法将数据发送到片段.您将在片段中获得已选中的复选框值.请参阅此链接.希望这会帮助你.

  • 如果选中或未选中,您可以使用 SparseBooleanArray 来维护您的复选框项目。您只需要保存数组值的位置并设置 true 如果选中否则从 SparseBooleanArray 中删除该位置(在删除它之前检查该位置是否可用)。 (2认同)