根据选择的

时间:2015-07-23 01:17:37

标签: android android-actionbar android-recyclerview contextual-action-bar multipleselection

如何更改ActionBar菜单中包含的内容,具体取决于选择RecyclerView中的项目数量?例如,当在“选择模式”中选择一张卡时,我想要一个可见的编辑图标(铅笔)。一旦选择了多个项目,我希望编辑图标从选项中消失。

下面的代码:我尝试创建一个条件,mDeletionMode创建一个菜单,如果所选项目的数量为<= 1,则会显示一个编辑图标,并创建如果数字超过一个,则不带铅笔的菜单。我的方法是愚蠢的,因为我意识到菜单只在一个项目遇到longClick之后创建,当然在这一点上只选择了一个项目。我离开dumb-dumb code我曾经试过这只是为了展示我的方法,而我实际上正在寻找的是:

public class SubjectManagerFragment extends BaseFragment implements ActionMode.Callback {

public static ArrayList<SubjectInfo> subjectList = new ArrayList<SubjectInfo>();
public static FloatingActionButton fabCreateSubject;
private AlertDialog.Builder build;
private MultiSelector mMultiSelector = new MultiSelector();

public ActionMode actionMode;
public RecyclerView recList;
public CardView cardView;
public ItemClickSupport itemClick;

// currently an adaptation from:
// https://github.com/bignerdranch/recyclerview-multiselect#modal-multiselection-with-long-click

public ActionMode.Callback mDeleteMode = new ModalMultiSelectorCallback(mMultiSelector) {

    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        super.onCreateActionMode(actionMode, menu);

        if (mMultiSelector.getSelectedPositions().size() <= 1) {
            getActivity().getMenuInflater().inflate(R.menu.menu_subject_manager, menu);
        } else {
            getActivity().getMenuInflater().inflate(R.menu.menu_subject_manager_multiple, menu);
        }
        return true;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.action_select_all:
                // Delete crimes from model

                //actually do nothing.
                mMultiSelector.clearSelections();
                return true;
            default:
                break;
        }
        return false;
    }
};
public static final String ARG_PARAM1 = "param1";
public static final String ARG_PARAM2 = "param2";

private String mParam1;
private String mParam2;

public static SubjectManagerFragment newInstance(String param1, String param2) {
    SubjectManagerFragment fragment = new SubjectManagerFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

public SubjectManagerFragment() {
    // Required empty public constructor
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    return false;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    return false;
}

@Override
public void onDestroyActionMode(ActionMode mode) {

}


//non graphical initialization
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(false);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
    setRetainInstance(true);

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View smFragmentView = inflater.inflate(R.layout.fragment_subject_manager, container, false);
    recList = (RecyclerView) smFragmentView.findViewById(R.id.subject_card_list);
    cardView = (CardView) smFragmentView.findViewById(R.id.subject_card);
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    subjectList = getSubjectInfoArrayList();
    recList.setLayoutManager(llm);
    recList.setAdapter(new CrimeAdapter());

    fabCreateSubject = (FloatingActionButton) smFragmentView.findViewById(R.id.fab_create_subject);
    fabCreateSubject.setOnClickListener (new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            build = new AlertDialog.Builder(getActivity());
            LayoutInflater inflater1 = getActivity().getLayoutInflater();
            View alertview = inflater1.inflate(R.layout.create_subject_dialog, null);

            // Pass null as the parent view because its going in the dialog layout
            build.setView(alertview);
            final EditText inputSubjectName = (EditText) alertview.findViewById(R.id.dialog_edit_subject_card_name);
            final EditText inputSubjectGrade = (EditText) alertview.findViewById(R.id.dialog_edit_subject_card_grade);

            build.setTitle("Add Subject");
            build.setPositiveButton("Save", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    String enteredSubjectName = inputSubjectName.getText().toString();
                    int enteredSubjectGrade = Integer.parseInt("0" + inputSubjectGrade.getText().toString()); //was getting stupid error from null value going to int?
                    boolean enteredSubjectIsArchived = false;

                    if (subjectCanBeEntered(inputSubjectName, inputSubjectGrade, subjectList)) {
                        SubjectInfo si = new SubjectInfo(enteredSubjectName, "Assignments", enteredSubjectGrade, enteredSubjectIsArchived, true);
                        si.save();
                        subjectList.add(si);
                        getActivity().recreate();
                        sa.notifyDataSetChanged();
                        recList.smoothScrollToPosition(subjectList.size()-1);
                    } //will need to check if subject already exists, but YOLO for now.
                    dialog.cancel();
                }
            });
            build.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = build.create();
            alert.show();
        }
    });
    // Inflate the layout for this fragment
    return smFragmentView;
}

public ArrayList<SubjectInfo> getSubjectInfoArrayList(){
    ArrayList<SubjectInfo> sial= new ArrayList<SubjectInfo>();
    List<SubjectInfo> sil = SubjectInfo.listAll(SubjectInfo.class);
    sial.addAll(sil);
    for (int go = 0; go <sial.size(); go++) {
        if (sial.get(go).itemHeaderTitle.equals("Archived")) {
            sial.remove(go);
        }
    }
    return sial;
}

public boolean subjectCanBeEntered (EditText inputName, EditText inputGrade, ArrayList<SubjectInfo> aList) {
    boolean enterable = true;
    if ((inputName.getText().toString().equals(""))) {
        enterable = false;
        Toast.makeText(
                getActivity().getApplicationContext(), "Enter a class name.", Toast.LENGTH_SHORT).show();
    }
    if ((inputGrade.getText().toString().equals(""))) { // I don't think hint is picked up
        enterable = false;
        Toast.makeText(
                getActivity().getApplicationContext(), "Enter a grade.", Toast.LENGTH_SHORT).show();
    }
    for (int go = 0; go < aList.size(); go++) {
        if (inputName.getText().toString().equals(aList.get(go).subjectName)) {
            enterable = false;
            Toast.makeText(
                    getActivity().getApplicationContext(), "You've already saved a class with that name.", Toast.LENGTH_LONG).show();
        }
    }
    return enterable;
}

private class CrimeHolder extends SwappingHolder
        implements View.OnClickListener, View.OnLongClickListener {
    protected TextView vSubjectName;
    protected EditText vSubjectGrade;
    protected RelativeLayout vSubjectLayout;
    private SubjectInfo sInfo;

    public CrimeHolder(View itemView) {
        super(itemView, mMultiSelector);

        vSubjectName = (TextView) itemView.findViewById(R.id.subject_card_name_textView);
        vSubjectGrade = (EditText) itemView.findViewById(R.id.subject_card_grade_editText);
        vSubjectLayout = (RelativeLayout) itemView.findViewById(R.id.subject_card_relative_layout);
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
        itemView.setLongClickable(true);
    }

    public void bindCrime(SubjectInfo si) {
        sInfo = si;
        vSubjectName.setText(sInfo.subjectName);
        vSubjectGrade.setText(Integer.toString(si.subjectGrade));
        vSubjectLayout.setBackgroundColor(Color.parseColor(SubjectAdapter.giveSubjectHexValue((double) si.subjectGrade)));
    }

    @Override
    public void onClick(View v) {
        if (sInfo == null) {
            return;
        }
        if (!mMultiSelector.tapSelection(this)) {
            // This condition is the same as, if not in ActionMode, handle the click normally:
            //getActionBar().startActionMode(mDeleteMode);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        //ActionBarActivity activity = (ActionBarActivity)getActivity();
        getActionBar().startActionMode(mDeleteMode);
        mMultiSelector.setSelected(this, true);
        return true;
    }
}

private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
    @Override
    public CrimeHolder onCreateViewHolder(ViewGroup parent, int pos) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.subject_card_layout, parent, false);
        return new CrimeHolder(view);
    }

    @Override
    public void onBindViewHolder(CrimeHolder holder, int pos) {
        SubjectInfo sInfo = subjectList.get(pos);
        holder.bindCrime(sInfo);
    }

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

我的问题是:如何以编程方式 删除/添加编辑图标切换到其他菜单(少一个选项) )取决于如何选择项目。

1 个答案:

答案 0 :(得分:1)

使用ActionMode实例操作ActionBar菜单。

@Override
public boolean onLongClick(View v) {
    this.actionMode = getActionBar().startActionMode(mDeleteMode);
    mMultiSelector.setSelected(this, true);
    return true;
}

以下是menuItem初始化的位置。请注意,onCreateActionMode的所有内容都会new ModalMultiSelectorCallback。{/ p>

    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        super.onCreateActionMode(actionMode, menu);getActivity().getMenuInflater().inflate(R.menu.menu_subject_manager, menu);
        mEditItem = actionMode.getMenu().findItem(R.id.action_edit);
        mActionMenu = actionMode.getMenu();
        return true;
    }

现在,mEditItem可以轻松删除或替换,因为mEditItem是一个实例变量。我将在onClick()

中提供我的实施方案
@Override
    public void onClick(View v) {

        if (sInfo == null) {
            return;
        }
        if (!mMultiSelector.tapSelection(this)) {
            // if not in selection mode, this is handled
        }
        if (mMultiSelector.tapSelection(this)) {
            mMultiSelector.tapSelection(this);

            switch (mMultiSelector.getSelectedPositions().size()) {
                case 0:
                    actionMode.finish();
                    break;
                case 1:
                    mEditItem.setVisible(true);
                    break;
                case 2:
                    mEditItem.setVisible(false);
                    break;
                default:
                    break;
            }
        }
    }

我有它。对我来说,根据在任何特定时刻选择了多少项目,我只需要一个项目消失/重新出现。 .setVisible()方法为我处理了这个问题。我希望这可以帮助有类似问题的人!