在选择项目时,我想添加一个计数器而不是普通标题,例如,如果用户选择2个项目,则出租车的标题应更改为2。
我尝试设置actionMode.setTitle(String.valueOf(counter))
,在其中selected_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);
}
});
}
}
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;
我看过很多使用计数器的教程,但是无法在我的代码中实现它:(