【问题标题】:Get a field from Fragment on Adapter and get List from Adapter on Fragment从 Adapter 上的 Fragment 获取一个字段并从 Fragment 上的 Adapter 获取 List
【发布时间】:2019-01-14 18:36:28
【问题描述】:

我正在制作一个checkbox 列表,我的adapter 和我的fragment 在列表顶部有一个带有“全选”的复选框,在列表底部有一个textView “x Itens Selected”加上button

我的问题 1 是如何获得这个“x”:检查了多少个复选框的计数,因为它们位于 Adapter 上,而 TextView 位于 Fragment 上,我无法在 @ 上设置它987654329@。 为此,我使用复选框列表中的setOnCheckedChangeListener。 我尝试通过多种方式从 Adapter 获取 Fragment,但没有成功。发送构造函数,从 viewHolder 获取,从 getSupportFragmentManager()getFragments()getFragmentByIdgetFragmentByTag 获取。 我也试过FragmentObserver,但没用。

问题 2:所以我尝试在 Adapter 上列出所有已检查的 Itens,但我需要将其发送到 Fragment 来管理它。另外,我无法从FragmentonCreateView)上的Adapter 获得它。

有人可以帮忙吗?

我的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);
                }
            }
        }
    });
}

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

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

【问题讨论】:

    标签: android checkbox fragment adapter


    【解决方案1】:

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

    【讨论】:

    • 帮助很大!太感谢了!你能帮我处理一下 holder.checkCadaItemRefugado.setOnCheckedChangeListener 吗?该活动运作良好。但我想运行所有列表并检查列表中有多少项被检查。以及其中哪一个(添加到我的新列表中)。我的 for 循环中的代码不起作用。
    • 如果选中或未选中,您可以使用 SparseBooleanArray 来维护您的复选框项目。您只需要保存数组值的位置,如果选中则设置为 true,否则从 SparseBooleanArray 中删除该位置(在删除它之前检查该位置是否可用)。
    • 我希望您的解决方案更具体,@Artipatel。在我的适配器中,我使用以下内容对其进行了初始化: public interface DataChangeListener { void onDataSetChanged();并在我的 FragmentA(显示复选框的位置)中实现了以下内容: public class GroupFragment extends Fragment implements GroupListAdapter.DataChangeListener and Override public void onDataSetChanged() { // 数据已经改变,做一些事情,比如告诉适配器并做任何你认为合适的东西。 }。我做错什么了吗?
    • 正如您所说,您在适配器类中初始化了该接口。您是否在复选框的 changelistener 上调用了 onDataSetChanged 方法?
    猜你喜欢
    • 2019-01-07
    • 2019-05-26
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    相关资源
    最近更新 更多