处理在Fragment中的Adapter中触发的setOnCheckedChangeListener

时间:2015-07-13 20:34:42

标签: android android-fragments android-checkbox

我有一个片段,它会使Recyclerview膨胀并使用适配器填充它。现在布局有一个Checkbox项目。我没有在onBindViewHolder的Adapter中处理Checkbox Item click事件,而是想在实现setOnCheckedChangeListener的Fragment类中处理它。

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

这是代码 -

填充Cardview的适配器类

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();

        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

app.controller('otherController', ['$scope', 'breadcrumb', function($scope, breadcrumb) {

    // update company
    breadcrumb.setCompany('StackExchange');

    // update the scope data in the view?   

}]);

在我的片段类中实现它 -

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

将其传递给适配器 -

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

在适配器内 -

CardTypesAdapter adapter = new CardTypesAdapter(arrCardID, arrCardType, arrCardSummary, this);
复选框的

和onCheckedChangeListener

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

    }