简体   繁体   中英

How to change the Menu present on Action Bar on Longpress of an Image?

How to change the Menu of my gallery app on Longclick of an Image? I have studied all these links Link for Menu

This link is about Menu for changing the Menu options Onprepareoptionsmenu

  public class PictureHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    public ImageView pictureThumbnail;
    public PictureHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
        this.pictureThumbnail = (ImageView) itemView.findViewById(R.id.pictureThumbnail);
    }
    @Override
    public boolean onLongClick(View view) {

        GalleryBrowserActivity.a=0;
      //  g.tRY();
        Log.d("hi","hello Image");
        return true;
    }
    @Override
    public void onClick(View v) {
        Context context = v.getContext();
        Intent intent = new Intent(context, PictureDetailActivity.class);
        Picture picture = (Picture) galleryList.get(getAdapterPosition());
        intent.putExtra(AppConstants.PICTURE_PATH, picture.getPath());
        Activity activity = (Activity) context;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(activity, v, AppConstants.PICTURE_ANIMATION);
            context.startActivity(intent, options.toBundle());
        } else {
            context.startActivity(intent);
        }
    }

The other class is: This is the class in which i am defining Menu

public boolean onCreateOptionsMenu(Menu menu) {
  if(a==1)
    getMenuInflater().inflate(R.menu.menu_gallery_browser, menu);
    if(a==0)
   getMenuInflater().inflate(R.menu.picture_longclick, menu);

    return true;
}

Try to define an interface for click operations, so this way your holder and adapter becomes cleaner.

public interface IClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }


public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private IClickListener listener;
    private List<Object> mDataset;

    public MyAdapter(List<Object> mDataSet, IClickListener listener) {
        this.mDataset = mDataSet;
        this.listener = listener;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_loading, parent, false);
        return new PictureHolder(view, listener);
    }

    @Override
    public int getItemCount() {
        //...
        return 0;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       //...
    }

    private static class PictureHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        private IClickListener listener;
        public ImageView pictureThumbnail;

        public PictureHolder(View itemView, IClickListener listener) {
            super(itemView);
            this.listener = listener;
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
            this.pictureThumbnail = (ImageView) itemView.findViewById(R.id.pictureThumbnail);
        }

        @Override
        public boolean onLongClick(View view) {
            listener.onItemClick(view, getAdapterPosition());
            return true;
        }

        @Override
        public void onClick(View view) {
            listener.onItemLongClick(view, getAdapterPosition());
        }
    }

In your activity, implement interface and give reference of your activity to adapter and do other works in activity

public class MyActivity extends AppCompatActivity implements IClickListener {

    private MyAdapter mAdapter;
    private Menu mMenu;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //..
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        if(a==1)
            mMenu = getMenuInflater().inflate(R.menu.menu_gallery_browser, menu);
        if(a==0)
            mMenu =  getMenuInflater().inflate(R.menu.picture_longclick, menu);


        return super.onCreateOptionsMenu(menu);

    }

    private void initRecyclerview (){
        //Recyclerview initialization
        mAdapter = new MyAdapter(dataset, this);
    }


    @Override
    public void onItemClick(View view, int position) {
        //Do your work here like this, or change visibilities of menu items
        mMenu = getMenuInflater().inflate(R.menu.menu_gallery_browser, mMenu);
    }

    @Override
    public void onItemLongClick(View view, int position) {
        //Do your work here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM