如何更改图像长按操作栏上的菜单?

时间:2017-02-21 08:52:35

标签: android menu

如何在图片的Longclick上更改我的图库应用程序的菜单? 我研究了所有这些链接 Link for Menu

此链接是关于更改菜单选项的菜单 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);
        }
    }

另一类是: 这是我定义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;
}

1 个答案:

答案 0 :(得分:2)

尝试为点击操作定义一个界面,这样你的持有者和适配器就变得更干净了。

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());
        }
    }

在您的活动中,实施界面并将您的活动参考提供给适配器,并在活动中执行其他工作

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
    }
}