【发布时间】: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(),getFragmentById,getFragmentByTag 获取。
我也试过FragmentObserver,但没用。
问题 2:所以我尝试在 Adapter 上列出所有已检查的 Itens,但我需要将其发送到 Fragment 来管理它。另外,我无法从Fragment(onCreateView)上的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