我想在选择项目时添加一个计数器而不是普通标题,例如,如果用户选择 2 个项目,则 cab 的标题应更改为 2。
我已经尝试设置actionMode.setTitle(String.valueOf(counter))
,whreselected_items_list
是添加所有选定项目的位置,但它只是设置为0
内部的标题onPrepareActionMode
。
当调用Update
内部TextViewHolder
方法时(在onClick
侦听器内部),计数器会增加。我使用的是 RecyclerView,下面是 ViewHolder 和 ActionMode.Callback。
CAB 仅包含两个元素/项目:删除和共享按钮。
编辑
移入actionMode.setTitle()
方法Update
( TextViewholder
) 。标题更改为所选元素的数量。现在找到当项目被取消选择时如何减少。
视图持有者
public class TextViewHolder extends ViewHolder {
// each data item is just a string in this case
private TextView titolo;
private TextView ora;
private RelativeLayout selezione_layout_testo;
public TextViewHolder(View v) {
super(v);
titolo = itemView.findViewById(R.id.testo);
selezione_layout_testo =
itemView.findViewById(R.id.layout_selezione_testo); //This element
is root view of the items
ora = itemView.findViewById(R.id.ora);
}
void selectItem(ModelloDati item) {
if (multiSelect) {
if (selected_items_list.contains(item)) {
selected_items_list.remove(item);
if(selected_items_list.size()==0)
{
if (actionMode != null ){
actionMode.finish();
}
}
selezione_layout_testo.setBackgroundColor(
context.getResources().getColor(R.color.transparent));
} else {
selected_items_list.add(item);
selezione_layout_testo.setBackgroundColor(
context.getResources().getColor(R.color.Colore_selezione));
}
}
}
void update(final ModelloDati value) {
if (selected_items_list.contains(value)) {
selezione_layout_testo.setBackgroundColor(
context.getResources().getColor(R.color.Colore_selezione));
} else {
selezione_layout_testo.setBackgroundColor(
context.getResources().getColor(R.color.transparent));
}
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
actionMode =
((AppCompatActivity)view.getContext()).startSupportActionMode(
actionModeCallback);
selectItem(value);
//-------------- EDITED!!!
String counter_string = String.valueOf(counter);
actionMode.setTitle(counter_string);
return true;
}
});
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter++;
selectItem(value);
}
});
}
}
ActionMode.Callback
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(final ActionMode actionMode, Menu menu)
{
multiSelect = true;
actionMode.getMenuInflater().inflate(R.menu.action_bar_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(
ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.delete:
for(ModelloDati oggetto : selected_items_list)
{
if(oggetto.getTYPE() == tlTEXT)
data_list.remove(data_list.indexOf(oggetto)-1);
data_list.remove(oggetto);
}
actionMode.finish();
return true;
case R.id.share:
//Do nothing
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
multiSelect = false;
selected_items_list.clear();
notifyDataSetChanged();
counter = 0;
}
};
变量
private ArrayList<ModelloDati> data_list;
private ArrayList<ModelloDati> selected_items_list = new ArrayList<>();
private Context context;
private static final int tlTEXT = 0;
private boolean multiSelect = false;
private ActionMode actionMode;
private int counter = 0;
我看过很多使用计数器的教程,但我无法在我的代码中实现它:(