Android startActionMode 无法解析
Android startActionMode cannot be resolved
我遵循了一个教程 (https://www.youtube.com/watch?v=8XR0cy3YgLo), modified it slightly and then incorporated the onLongClickListener (https://www.youtube.com/watch?v=jGW95imi9tw),其中所有代码都没有错误,除了我希望使用 CAB(上下文操作栏)以便 select 多个项目并且是能够删除 selected。
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;
}
startActionMode
是仅当您在 Activity 中或您有 activity 的参考才能进行该调用时才可用的方法。
从你的代码来看,你实际上是从你的 MyAdapter
class 调用它的,这不是 Activity
.
使用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;
}
}
我遵循了一个教程 (https://www.youtube.com/watch?v=8XR0cy3YgLo), modified it slightly and then incorporated the onLongClickListener (https://www.youtube.com/watch?v=jGW95imi9tw),其中所有代码都没有错误,除了我希望使用 CAB(上下文操作栏)以便 select 多个项目并且是能够删除 selected。
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;
}
startActionMode
是仅当您在 Activity 中或您有 activity 的参考才能进行该调用时才可用的方法。
从你的代码来看,你实际上是从你的 MyAdapter
class 调用它的,这不是 Activity
.
使用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;
}
}