I made custom gallery using fragment and recyclerview. I want to make multiple selection within it, to appear Actionbar with menu when I select pictures. So I made ActionMode in my adapter and tried to start action mode when the image were long clicked. But I can't bring the activity of my gallery fragment... with the error "Non-static method 'getActivity()' cannot be referenced from a static context".
Furthermore, I selected only one image but other images are selected together. How can I fix it?
public class exAdap extends RecyclerView.Adapter<exAdap.ViewHolder>
ActionMode.Callback mActionCallback = new ActionMode.Callback(){
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu){
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode){
mActionMode = null;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu){
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.pic_menu, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId()){
case R.id.menu_email:
mode.finish();
break;
}
return false;
}
};
public exAdap(ArrayList<String> datas, Context context) {
this.datas = datas;
this.context = context;
}
@NonNull
@Override
public exAdap.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
int viewType) {
View view;
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.activity_gallery, parent,
false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Glide.with(context).load(datas.get(position)).into(holder.getImage());
String currentImage = datas.get(position);
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(multiSelect){
selected.add(currentImage);
holder.check.setVisibility(View.VISIBLE);
holder.check.setChecked(true);
}
}
});
holder.image.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (!multiSelect) {
multiSelect=true;
mActionMode = ((AppCompatActivity)
fragment_gallery.getActivity()).startSupportActionMode(mActionCallback);
if(selected.contains(currentImage)){
selected.remove(currentImage);
}
else{
selected.add(currentImage);
holder.check.setVisibility(View.VISIBLE);
holder.check.setChecked(true);
}
}
return true;
}
});
}
@Override
public int getItemCount() {
return datas.size();
}
public void addAll(ArrayList<String> datas) {
clearAll(false);
this.datas = datas;
notifyDataSetChanged();
}
public void clearAll(boolean isNotify) {
datas.clear();
selected.clear();
if (isNotify) notifyDataSetChanged();
}
public void clearSelected() {
selected.clear();
notifyDataSetChanged();
}
public void selectAll() {
selected.clear();
selected.addAll(datas);
notifyDataSetChanged();
}
public ArrayList<String> getSelected() {
return selected;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
CheckBox check;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById((R.id.recyclerview_row_image));
check = itemView.findViewById(R.id.checkImage);
}
public ImageView getImage() {
return this.image;
}
}
'''
public class fragment_gallery extends Fragment
private ListView m_list;
private ArrayList<String> listt;
private RecyclerView recyclerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
//listt = getPic();
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
recyclerView.setAdapter(new exAdap(getPic(), getContext()));
return view;
}
public ArrayList<String> getPic() {
ArrayList<String> fileList = new ArrayList<>();
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, MediaStore.MediaColumns.DATE_ADDED + " desc");
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
int columnDisplayname = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
int lastIndex;
while (cursor.moveToNext()) {
String absolutePathOfImage = cursor.getString(columnIndex);
String nameOfFile = cursor.getString(columnDisplayname);
lastIndex = absolutePathOfImage.lastIndexOf(nameOfFile);
lastIndex = lastIndex >= 0 ? lastIndex : nameOfFile.length() - 1;
if (!TextUtils.isEmpty(absolutePathOfImage)) {
fileList.add(absolutePathOfImage);
}
}
cursor.close();
return fileList;
}
'''