在DialogFragment

时间:2015-11-06 12:31:05

标签: android android-recyclerview android-dialogfragment android-viewholder

a simple test project at GitHub中,我显示RecyclerViewandroid.R.layout.simple_list_item_multiple_choice

app screenshot

SwapTilesDialogFragment我实施了流行的#34;片段界面"模式,并希望通过其doPositiveClick()方法传递选定的行:

public class SwapTilesDialogFragment extends DialogFragment {

    public final static String TAG = "SwapTilesDialogFragment";

    private final static String ARG = "ARG";

    public interface MyListener {
        public void doPositiveClick();
        public void doNegativeClick();
    }

    private MyListener mListener;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if (context instanceof MyListener) {
            mListener = (MyListener) context;
        } else {
            throw new ClassCastException(context.toString() +
                    " must implement " + TAG + ".MyListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();

        mListener = null;
    }

    private class MyViewHolder
            extends RecyclerView.ViewHolder
            implements View.OnClickListener {

        public MyViewHolder(View v) {
            super(v);
            v.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(),
                    "You have clicked " + ((TextView) v).getText(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    private RecyclerView mRecyclerView;
    private char[] mLetters;

    public static SwapTilesDialogFragment newInstance(char[] letters) {
        SwapTilesDialogFragment f = new SwapTilesDialogFragment();

        Bundle args = new Bundle();
        args.putCharArray(ARG, letters);
        f.setArguments(args);

        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mLetters = getArguments().getCharArray(ARG);

        mRecyclerView = new RecyclerView(getContext());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(new RecyclerView.Adapter<MyViewHolder>() {

            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(
                        android.R.layout.simple_list_item_multiple_choice,
                        parent,
                        false);
                MyViewHolder vh = new MyViewHolder(v);
                return vh;
            }

            @Override
            public void onBindViewHolder(MyViewHolder vh, int position) {
                TextView tv = (TextView) vh.itemView;
                tv.setText(String.valueOf(mLetters[position]));
            }

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

        return new AlertDialog.Builder(getActivity())
                .setIcon(R.mipmap.ic_launcher)
                .setTitle(R.string.swap_tiles_title)
                .setView(mRecyclerView)
                .setPositiveButton(R.string.swap_tiles_ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                mListener.doPositiveClick();
                            }
                        }
                )
                .setNegativeButton(R.string.swap_tiles_cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                mListener.doNegativeClick();
                            }
                        }
                )
                .create();
    }
}

我的问题:

如何在用户点击RecyclerView中的某一行时显示复选标记,以及在我致电doPositiveClick()之前如何/在何处收集所有选定的行?

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:


有一个数组来跟踪被检查的字母:

    private List<String> checkedLetters = new ArrayList<>();

修改你的听众:

 public interface MyListener {
    public void doPositiveClick(List<String> checkedLetters);
    public void doNegativeClick();
}

您的ViewHolder:

 private class MyViewHolder
        extends RecyclerView.ViewHolder
        implements View.OnClickListener {

    private CheckedTextView mCheckedText;

    public MyViewHolder(View v) {
        super(v);
        v.setOnClickListener(this);
        mCheckedText = (CheckedTextView)itemView;
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(getContext(),
                "You have clicked " + ((TextView) v).getText(),
                Toast.LENGTH_SHORT).show();

        toggle();
    }

    private void toggle(){

        if(mCheckedText.isChecked())
            checkedLetters.remove(LETTERS[getAdapterPosition()]);
        else
            checkedLetters.add(LETTERS[getAdapterPosition()]);

        mCheckedText.setChecked(!mCheckedText.isChecked());
    }
}'

完成。您现在可以从Activity

中检索选中的字母