从片段类中的对话框添加项目到recyclerview。

时间:2018-12-21 10:35:47

标签: android android-fragments android-recyclerview

我有一个片段,其中必须包含两个回收者视图。该视图如下所示。我需要从对话框向底部的recyclerview添加一个项目。我将为适配器类和xml发布我的代码。请指导我实现这一目标,欢迎提供示例或代码。

My Screen with two recyclerviews

我尝试将数据从对话框传递到第二个recyclerview,但是它不显示添加的项目。告诉我我在这里做错了什么。我没有尝试解决任何问题以更新我的第二个recyclerview ...:/

我的片段类

public class ReceiptFragment extends Fragment { //implements ContReceiptsAdapter.ItemOnClickListener

RecyclerView rv_rceipts, rv_addpayments;
RecyclerView.Adapter mContReceiptsAdapter,mAddPaymentsAdapter;
RecyclerView.LayoutManager mLayoutManager1,mLayoutManager2;
View receipts_view;
private Context mContext;
ContReceiptsAdapter contReceiptsAdapter;
AddPaymentsAdapter addPaymentsAdapter;
//-- items in detailed dialog here
private ArrayList<Contract> contracts;
private ArrayList<AddPayments> payments;
private View linearLayout;
public View mRootView;

public ReceiptFragment() {
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    receipts_view = inflater.inflate(R.layout.fragment_receipt, container, false);

    BuildReceiptsRV();
    BuildAddedPaymentsRV();
    // for setting the first item as default in the list
 //        onItemClicked(0);
    return receipts_view;

}

private void BuildReceiptsRV() {
    rv_rceipts = receipts_view.findViewById(R.id.rv_tobe_added);
    rv_rceipts.setHasFixedSize(true);
    rv_rceipts.setItemAnimator(new DefaultItemAnimator());

    mLayoutManager1 = new LinearLayoutManager(getActivity());
    rv_rceipts.setLayoutManager(mLayoutManager1);

    mContReceiptsAdapter = new ContReceiptsAdapter(getActivity(), ApplicationClass.contractList);
    rv_rceipts.setAdapter(mContReceiptsAdapter);
}

private void BuildAddedPaymentsRV() {
    rv_addpayments = receipts_view.findViewById(R.id.rv_added);
    rv_addpayments.setHasFixedSize(true);
    rv_addpayments.setItemAnimator(new DefaultItemAnimator());

    mLayoutManager2 = new LinearLayoutManager(getActivity());
    rv_addpayments.setLayoutManager(mLayoutManager2);

    mAddPaymentsAdapter = new AddPaymentsAdapter(getActivity(),ApplicationClass.paymentList);
    rv_addpayments.setAdapter(mAddPaymentsAdapter);
}

//change when click on change on item added to second rv
private void changeLayoutRV() {
    linearLayout = mRootView.findViewById(R.id.rv_tobe_added);

    if (linearLayout.getVisibility() == View.GONE) {
        linearLayout.setVisibility(View.VISIBLE);
    } else {
        linearLayout.setVisibility(View.GONE);
    }
}
}

“我的适配器”用于第一个Recyclerview

public class ContReceiptsAdapter extends RecyclerView.Adapter<ContReceiptsAdapter.ReceiptsViewHolder> {

//    private static Dialog AddItemDialog;
private ArrayList<Contract> contracts;
private ArrayList<AddPayments> payments = new ArrayList<>();
Context mContext;
String inPending;
//for the details dialog
EditText et_number, contract_id_val, product_type_val, mobile_number_ADialog, et_amount_add;
//    ImageView btn_add_item;
//--to set to add item dialog
public TextView contract_number, vehicle_number, contract_number_ADialog, vehicle_number_ADialog;
CheckBox checkbox_pending;

public ContReceiptsAdapter(Context context, ArrayList<Contract> contractList) {
    contracts = contractList;
    mContext = context;
}

public class ReceiptsViewHolder extends RecyclerView.ViewHolder {

    TextView tv_contract, tvVehicleN, tvContractType;
    ImageView btn_add_item;
    LinearLayout detailsLayout;

    public ReceiptsViewHolder(@NonNull final View itemView) {
        super(itemView);

        detailsLayout = itemView.findViewById(R.id.add_details_item_id);
        tv_contract = itemView.findViewById(R.id.tv_contract);
        tvVehicleN = itemView.findViewById(R.id.tvVehicleN);
        tvContractType = itemView.findViewById(R.id.tvContractType);
        btn_add_item = itemView.findViewById(R.id.btn_add_item);
    }
}

@NonNull
@Override
public ContReceiptsAdapter.ReceiptsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.receipt_tobe_added_rv_item, viewGroup, false);

    final ReceiptsViewHolder receiptsViewHolder = new ReceiptsViewHolder(view);

    //dialog ini
 //        ShowDetails();
    final Dialog detailedDialog = new Dialog(this.mContext);
    detailedDialog.setContentView(R.layout.fragment_details);

    receiptsViewHolder.detailsLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //click view dto show the detaileds view of the list item
            contract_number = detailedDialog.findViewById(R.id.contract_id_val);
            vehicle_number = detailedDialog.findViewById(R.id.vehicle_num_val);
            contract_number.setText(contracts.get(receiptsViewHolder.getAdapterPosition()).getContractNumber());
            vehicle_number.setText(contracts.get(receiptsViewHolder.getAdapterPosition()).getVehicleNumber());
            detailedDialog.show();
            detailedDialog.setCancelable(true);
//                    mListener.onItemClicked(contracts.indexOf(v.getTag()));
        }
    });

    return receiptsViewHolder;
}

@Override
public void onBindViewHolder(@NonNull final ContReceiptsAdapter.ReceiptsViewHolder viewHolder, int i) {

    viewHolder.itemView.setTag(contracts.get(i));
//        Contract currentItem = contracts.get(i);

    viewHolder.tv_contract.setText(contracts.get(i).getContractNumber());
    viewHolder.tvVehicleN.setText(contracts.get(i).getVehicleNumber());
    viewHolder.tvContractType.setText(contracts.get(i).getContractType());

    final Dialog AddItemDialog = new Dialog(mContext);
    AddItemDialog.setContentView(R.layout.fragment_add_receipt);

    viewHolder.btn_add_item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
 //                ShowDetails();7
            contract_number_ADialog = AddItemDialog.findViewById(R.id.contract_id_val_add);
            contract_number_ADialog.setText(contracts.get(viewHolder.getAdapterPosition()).getContractNumber());
            vehicle_number_ADialog = AddItemDialog.findViewById(R.id.product_type_val_add);
            vehicle_number_ADialog.setText(contracts.get(viewHolder.getAdapterPosition()).getVehicleNumber());
            mobile_number_ADialog = AddItemDialog.findViewById(R.id.et_number_add);
            mobile_number_ADialog.setText(contracts.get(viewHolder.getAdapterPosition()).getMobileNumber());
            checkbox_pending = AddItemDialog.findViewById(R.id.checkbox_pending_add);
            checkbox_pending.setChecked(Boolean.parseBoolean(contracts.get(viewHolder.getAdapterPosition()).getPendingState()));
            Button btn_cancel_receipt = AddItemDialog.findViewById(R.id.btn_cancel_receipt);
            Button btn_add_receipt = AddItemDialog.findViewById(R.id.btn_add_receipt);

            et_amount_add = AddItemDialog.findViewById(R.id.et_amount_add);

            AddItemDialog.show();
            AddItemDialog.setCancelable(true);

            btn_cancel_receipt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AddItemDialog.dismiss();
                }
            });
            btn_add_receipt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AddData();

                    //add item to rv2
                    AddItemDialog.dismiss();

                }
            });

        }
    });
}

@Override
public int getItemCount() {
    return contracts.size();
}

private void addAnItem(AddPayments payment) {
    payments = ApplicationClass.paymentList;
    payments.add(payment);
    this.notifyDataSetChanged();
}

private void AddData() {

    String c_code = contract_number_ADialog.getText().toString();
    String c_mobile = mobile_number_ADialog.getText().toString();
    String c_amount = et_amount_add.getText().toString();

    AddPayments added_payment = new AddPayments(c_amount, c_code, c_mobile, "true");

    payments.add(added_payment);
    notifyDataSetChanged();

}
}

我的用于第二个Recyclerview的适配器类

public class AddPaymentsAdapter extends RecyclerView.Adapter<AddPaymentsAdapter.AddPayViewHolder> {

private ArrayList<AddPayments> payments;
Context mContext;

public AddPaymentsAdapter(Context context, ArrayList<AddPayments> addPayments) {
    mContext = context ;
    payments = addPayments;
}

public class AddPayViewHolder extends RecyclerView.ViewHolder{

    TextView tv_contract_d, tv_amount_d, tv_isPending_d, tv_mobile_num_d;
    ImageView btn_delete_item;
    ConstraintLayout addedPaymentLayout;

    public AddPayViewHolder(@NonNull View itemView) {
        super(itemView);

        addedPaymentLayout = itemView.findViewById(R.id.added_payment_item_id);
        tv_contract_d = itemView.findViewById(R.id.tv_contract_d);
        tv_amount_d = itemView.findViewById(R.id.tv_amount_d);
        tv_isPending_d = itemView.findViewById(R.id.tv_isPending_d);
        tv_mobile_num_d = itemView.findViewById(R.id.tv_mobile_num_d);
        btn_delete_item = itemView.findViewById(R.id.btn_delete_item);
    }
}

@NonNull
@Override
public AddPaymentsAdapter.AddPayViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.receipt_added_rv_item, viewGroup, false);

    final AddPayViewHolder addPayViewHolder= new AddPayViewHolder(view);

    return addPayViewHolder;
}

@Override
public void onBindViewHolder(@NonNull AddPaymentsAdapter.AddPayViewHolder addPayViewHolder, final int i) {

    AddPayments currentItem = payments.get(i);

    addPayViewHolder.itemView.setTag(payments.get(i));

    addPayViewHolder.tv_contract_d.setText(payments.get(i).getAddContractCode());
    addPayViewHolder.tv_isPending_d.setText(payments.get(i).getAddIsPending());
    addPayViewHolder.tv_amount_d.setText(payments.get(i).getAddAmount());
    addPayViewHolder.tv_mobile_num_d.setText(payments.get(i).getAddMobile());

    addPayViewHolder.btn_delete_item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //delete function
 //                Toast.makeText(mContext, "clicked delete icon inHoldernAdapter", Toast.LENGTH_SHORT).show();
            removeItem(i);
        }
    });
}

@Override
public int getItemCount() {
    return payments.size();
}

private void removeItem(int i) {
    payments.remove(i);
    this.notifyItemRemoved(i);
}
}

1 个答案:

答案 0 :(得分:1)

  • 从适配器的第一个回收者视图代码中,您正在使用此方法AddData();添加数据。但是此方法将数据添加到payments,这是保存要在第二个回收器视图中显示但与它没有任何关系的数据的列表(它只是适配器类的一个字段)。

  • this.notifyDataSetChanged()通知当前适配器更改数据(第一个回收者视图)。您将需要将此数据添加到第二个回收者视图适配器。您现在没有这样做。

因此,一种方法是在片段内部创建一个方法,该方法以AddPayments列表作为参数,并将此数据添加到第二个回收者视图adapater。会是这样的:

片段方法:

public void addSecondRVData(AddPayments itemToAdd) {
    mAddPaymentsAdapter.addNewPayment(itemToAdd);
}

第二种回收者查看方法:

public void addData(AddPayments itemToAdd) {
    payments.add(itemToAdd);
    this.notifyDataSetChanged():
}

别忘了在第二个回收站视图中初始化payment列表。

然后在第一个适配器中执行以下操作:

private ReceiptFragment receiptFragment;

//Constructor
public ContReceiptsAdapter(Context context, ArrayList<Contract> contractList, ReceiptFragment receiptFragment) {
   contracts = contractList;
   mContext = context;
   this.receiptFragment = receiptFragment;
}

//Use this to add elements to second rv
receiptFragment.addSecondRVData(newItem);

希望这会有所帮助,尽管这不是最干净的解决方案。请检查我在此处编写的示例,因为目前无法对其进行测试。