Android startActionMode无法解析

时间:2016-04-01 01:00:16

标签: android android-recyclerview android-appcompat recycler-adapter contextual-action-bar

我已经按照教程(https://www.youtube.com/watch?v=8XR0cy3YgLo),稍微修改了它,然后合并了onLongClickListener(https://www.youtube.com/watch?v=jGW95imi9tw),其中所有代码都可以正常工作,除了我希望使用CAB(Contextual Action)栏)以便选择多个项目并能够删除所选项目。

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

Context c;
ArrayList<Player> players;
ActionMode mActionMode;

...

//View Holder Initialised
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    //View obj
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);

    //Holder
    MyHolder holder=new MyHolder(v);
    return holder;
}

//Data is bound to views
@Override
public void onBindViewHolder(MyHolder holder, int position) {
    holder.nameTxt.setText(players.get(position).getName());
    holder.posTxt.setText(players.get(position).getPosition());
    holder.img.setImageResource(R.drawable.marker);

    //When view is clicked
    holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onItemClick(View v, int pos) {
            Snackbar.make(v,players.get(pos).getName() + " 1 ", Snackbar.LENGTH_SHORT).show();
        }
    });

    holder.setItemLongClickListener(new ItemLongClickListener() {
        @Override
        public void onItemLongClick(View v, int pos) {
            mActionMode = startActionMode(new ActionMode.Callback() {
                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    mode.setTitle("Selected");

                    MenuInflater inflater = mode.getMenuInflater();
                    inflater.inflate(R.menu.contextual_menu, menu);
                    return true;
                }

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

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.action_delete:
                            //deleteClicked();
                            mode.finish();
                            return true;
                        default:
                            //doneClicked();
                            return false;
                    }
                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {
                }
            });
        }
    });
}

所以我遇到的问题是'mActionMode = startActionMode(new ActionMode.Callback(){'无效,因为'startActionMode'无法解析。

我知道startActionMode属于AppCompat,但我无法理解如何初始化CAB。我环顾四周,似乎无法找到明确的答案,所以非常感谢你解决这个问题。

修改

我现在设法通过长按处理程序显示CAB(上下文操作栏)(在完成本教程之后:http://www.startingandroid.com/how-to-use-sqlite-database-in-android/)但我无法弄清楚如何实际获取对给定项的引用。由于目前长按任何项目都会显示CAB但我没有参考被点击的项目。

@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_row, parent, false);
    UserViewHolder userViewHolder = new UserViewHolder(v);

    v.setClickable(true);

    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(v, "CLICK", Snackbar.LENGTH_SHORT).show();
        }
    });

    v.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Snackbar.make(v, "LONG CLICK", Snackbar.LENGTH_SHORT).show();
            if (mActionMode != null) {
                return false;
            }
            v.startActionMode(new ActionMode.Callback() {
                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    MenuInflater inflater = mode.getMenuInflater();
                    inflater.inflate(R.menu.context, menu);
                    return true;
                }

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

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    //get number of selected items

                    switch (item.getItemId()) {
                        case R.id.context_favourite:
                            //SQL - Favourite Item
                            mode.finish();
                        case R.id.context_delete:
                            //SQL - Delete Item
                            mode.finish();
                    }
                    return false;
                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {
                    mActionMode = null;
                }
            });
            return true;
        }
    });
    return userViewHolder;
}

2 个答案:

答案 0 :(得分:0)

startActionMode只有在您参与活动或您有活动参考才能进行此次通话时才可用。

从您的代码中,您实际从MyAdapter课程中调用它,而不是Activity

答案 1 :(得分:0)

使用view.startActionMode

public class ViewHolder extends RecyclerView.ViewHolder implements
        View.OnClickListener,
        View.OnLongClickListener {

    public ViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public boolean onLongClick(View view) {
        view.startActionMode(...);
        return true;
    }
}