处理在 Fragment 中的适配器中触发的 setOnCheckedChangeListener

Handle setOnCheckedChangeListener fired in Adapter in Fragment

我有一个 Fragment,它可以膨胀 Recyclerview 并使用适配器填充它。现在布局有一个复选框项。我不想在 onBindViewHolder 中的 Adapter 中处理 Checkbox Item click 事件,而是想在实现 setOnCheckedChangeListener 的 Fragment class 中处理它。

是否可以将选中的更改事件委托给 Fragment?

这是代码 -

填充 Cardview

的适配器 class
public class CardTypesAdapter extends RecyclerView.Adapter<CardTypesAdapter.ViewHolder> {

    private int[] arrCardID;
    private String[] arrCardType;
    private String[] arrCardSummary;

    private Listener listener;

    ArrayList<String> selectedStrings = new ArrayList<String>();

    public static interface Listener {
        public void onClick(int position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private CardView cv;

        public ViewHolder(CardView v) {
            super(v);
            cv = v;
        }
    }

    public CardTypesAdapter(int[] arrCardID, String[] arrCardType, String[] arrCardSummary) {
        this.arrCardID = arrCardID;
        this.arrCardType = arrCardType;
        this.arrCardSummary = arrCardSummary;

    }


    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @Override
    public CardTypesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CardView cv = (CardView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_types, parent, false);
        return new ViewHolder(cv);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        //Log.d("position in bindview", ""+ position);

        CardView cv = holder.cv;

        final CheckBox chbCardSelect = (CheckBox) cv.findViewById(R.id.chbCardSelect);
        chbCardSelect.setId(arrCardID[position]);

        chbCardSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                if (isChecked) {
                    selectedStrings.add("" + chbCardSelect.getId());
                } else {
                    selectedStrings.remove("" + chbCardSelect.getId());
                }
                Log.d("Selected Id" , ""+chbCardSelect.getId());

            }
        });


        chbCardSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                //onCheckedChanged(buttonView, isChecked);
                if (isChecked) {
                    selectedStrings.add("" + chbCardSelect.getId());
                } else {
                    selectedStrings.remove("" + chbCardSelect.getId());
                }
                Log.d("Selected Id" , ""+chbCardSelect.getId());

            }
        });

        TextView txtCardName = (TextView) cv.findViewById(R.id.txtCardName);
        txtCardName.setText(arrCardType[position]);

        TextView txtCardSummary = (TextView) cv.findViewById(R.id.txtCardSummary);
        txtCardSummary.setText(arrCardSummary[position]);

        cv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("in ClickListener", "i am clicked");
                if (listener != null) {

                    Log.d("in ClickListener", "i am clicked");
                    listener.onClick(position);

                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return arrCardType.length;
    }

}

DialogFragment Class ,我计划在其中接收 Checkbox Click 事件

    public class CardsTypeFragment extends DialogFragment implements View.OnClickListener{


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


        final View view = inflater.inflate(R.layout.fragment_card_types, container, false);

        //Recyclerview code
        final RecyclerView cardtypeRecycler = (RecyclerView) view.findViewById(R.id.cards_types_recycler);

        Button btnApplyFilter=(Button) view.findViewById(R.id.btnApplyFilter);
        btnApplyFilter.setOnClickListener(this);


        int[] arrCardID=new int[clsCardTypes.arrCardTypes.length];
        String[] arrCardType = new String[clsCardTypes.arrCardTypes.length];
        String[] arrCardSummary = new String[clsCardTypes.arrCardTypes.length];

        for (int i = 0; i < arrCardType.length; i++) {
            arrCardID[i] = clsCardTypes.arrCardTypes[i].getIntCardID();
            arrCardType[i] = clsCardTypes.arrCardTypes[i].getStrCardType();
            arrCardSummary[i] = clsCardTypes.arrCardTypes[i].getStrCardSummary();
        }

        CardTypesAdapter adapter = new CardTypesAdapter(arrCardID, arrCardType, arrCardSummary);
        cardtypeRecycler.setAdapter(adapter);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL,false);
        cardtypeRecycler.setLayoutManager(layoutManager);

        adapter.setListener(new CardTypesAdapter.Listener() {
            public void onClick(int position) {
                Log.d("in ClickListener", "i am clicked");

            }
        });

        return view;
    }

    @Override
    public void onClick(View view){


        if(view.getId()==R.id.btnApplyFilter){

            Intent i=new Intent()
                    .putExtra("name", "Amit Pandey")
                    .putExtra("age", "36");

            getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);//getActivity().getIntent()
            dismiss();

        }
    }
}

Fragment 和 Activity 之间有很多通信的例子。但是Adapters和Fragment之间很少。

我在适配器中声明了一个接口 -

public interface FragmentCommunicator{
        public void passDataToFragment(String someValue);
    }

在我的 Fragment 中实现了它 Class -

public class CardsTypeFragment extends DialogFragment implements View.OnClickListener, CardTypesAdapter.FragmentCommunicator

传递给适配器 -

CardTypesAdapter adapter = new CardTypesAdapter(arrCardID, arrCardType, arrCardSummary, this);

在适配器内部 -

public CardTypesAdapter(int[] arrCardID, String[] arrCardType, String[] arrCardSummary, FragmentCommunicator fragmentCommunicator) {
        this.arrCardID = arrCardID;
        this.arrCardType = arrCardType;
        this.arrCardSummary = arrCardSummary;
        this.fragmentCommunicator=fragmentCommunicator;

    }

和复选框的onCheckedChangeListener

chbCardSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                //Calling interface too-

                if(fragmentCommunicator != null)
                    fragmentCommunicator.passDataToFragment("Hi from FragmentActivity");
                else
                    Log.d("Msg", "not exists");

            }
        });