我的上下文菜单有问题,我必须控制" MenuButton"不使用"上下文操作栏菜单(CAB)"当你进行长时间点击并短按显示上下文菜单时。我需要使用"上下文操作栏菜单(CAB)"来显示上下文菜单。当你点击"菜单按钮" CAB菜单也可以短按,而不是长按。我需要通过短暂的单击显示上下文菜单。这有可能吗?
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_internal);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
title = (TextView) findViewById(R.id.txt_name_file);
past = (ImageView) findViewById(R.id.img_paste);
home = (ImageView) findViewById(R.id.img_home);
menu_item = (ImageView) findViewById(R.id.img_more);
list_in = (ListView) findViewById(R.id.list_in);
past.setVisibility(View.INVISIBLE);
fileManager = new FileManager();
arrayList = new ArrayList<>();
adapter = new InternalAdapter(getApplicationContext(), arrayList);
list_in.setAdapter(adapter);
list_in.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
if (selectCount > 0) {
if (actionMode == null) {
actionMode = InternalActivity.this.startActionMode(callback);
Toast.makeText(getApplicationContext(), "S>0", Toast.LENGTH_SHORT).show();
}
toggleselection(i);
actionMode.setTitle(selectCount + " " + "Selected");
if (selectCount == 0) {
actionMode.finish();
}
} else if (selectCount == 0) {
Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
String S = arrayList.get(i).getPathFile();
File file = new File(S);
if (arrayList.get(i).isFile() == false) {
pathItem = arrayList.get(i).getPathFile();
title.setText(pathItem.substring(pathItem.lastIndexOf("/") + 1));
readFile(pathItem);
} else if (file.canRead()) {
Intent intent = FileManager.createIntent(file);
if (getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
}
}
});
list_in.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (actionMode == null) {
actionMode = InternalActivity.this.startActionMode(callback);
Toast.makeText(getApplicationContext(), "L", Toast.LENGTH_SHORT).show();
}
toggleselection(position);
actionMode.setTitle(selectCount + " " + "Selected");
if (selectCount == 0) {
actionMode.finish();
}
return true;
}
});
readFile(FileManager.PATH);
loadFileItem();
}
ArrayList<FileItem> selectedFile = new ArrayList<>();
private void toggleselection(int position) {
arrayList.get(position).isSelected = !arrayList.get(position).isSelected;
if (arrayList.get(position).isSelected) {
selectedFile.add(arrayList.get(position));
selectCount++;
} else {
selectedFile.add(arrayList.get(position));
selectCount--;
}
adapter.notifyDataSetChanged();
}
private ActionMode.Callback callback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater menuInflater = mode.getMenuInflater();
menuInflater.inflate(R.menu.internal_context_menu, menu);
MenuItem mi = menu.getItem(0);
mi.setTitle(mi.getTitle().toString());
actionMode = mode;
selectCount = 0;
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.i_de:
mode.finish();
break;
}
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
if (selectCount > 0) {
deSelect();
}
actionMode = null;
}
};
private void deSelect() {
for (int i = 0; i < arrayList.size(); i++) {
arrayList.get(i).isSelected = false;
}
selectCount = 0;
adapter.notifyDataSetChanged();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.select_file, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.copy:
copyFolder(pos);
break;
case R.id.delete:
deleteFolder(pos);
break;
case R.id.rename:
renameFile(pos);
break;
}
return super.onContextItemSelected(item);
}
public class InternalAdapter extends BaseAdapter {
ArrayList<FileItem> arrayList;
Context context;
ViewHolder viewHolder;
public InternalAdapter(Context context, ArrayList<FileItem> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public FileItem getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.activity_internal_adapter, parent, false);
viewHolder.imgFolder = (ImageView) view.findViewById(R.id.img_folder);
viewHolder.txtNameFile = (TextView) view.findViewById(R.id.txt_name_folder);
viewHolder.txtDate = (TextView) view.findViewById(R.id.txt_date);
viewHolder.imgCreate = (ImageView) view.findViewById(R.id.img_create);
viewHolder.view = view.findViewById(R.id.view_internal);
viewHolder.linearLayout = (LinearLayout) view.findViewById(R.id.lv_image);
FileItem fileItem = getItem(position);
if (fileItem.isSelected) {
viewHolder.view.setAlpha(0.5f);
} else {
viewHolder.view.setAlpha(0.0f);
}
if (fileItem.isDirectory()) {
viewHolder.imgFolder.setImageResource(R.drawable.ic_folder_black_24dp);
} else if (fileItem.isFile()) {
viewHolder.imgFolder.setImageResource(R.drawable.ic_insert_drive_file_black_24dp);
}
viewHolder.txtNameFile.setText(fileItem.getName());
viewHolder.txtDate.setText(fileItem.getDate());
viewHolder.imgCreate.setLongClickable(false);
registerForContextMenu(viewHolder.imgCreate);
viewHolder.imgCreate.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
unregisterForContextMenu(v);
Toast.makeText(getApplicationContext(), "111", Toast.LENGTH_SHORT).show();
return false;
}
});
viewHolder.imgCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pos = position;
openContextMenu(v);
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
}
});
return view;
}
private class ViewHolder {
ImageView imgFolder;
TextView txtNameFile;
TextView txtDate;
ImageView imgCreate;
View view;
LinearLayout linearLayout;
}
}
}