Open full Screen Image when clicked on grid view android

前端 未结 1 1293
醉酒成梦
醉酒成梦 2021-02-11 09:46
// This is my On click function which opens New activity for image view.

  @Override
        public void onClick(View v) {
            Intent intent = new Intent(activi         


        
相关标签:
1条回答
  • 2021-02-11 10:14

    Since your getItem method returns a different position on the list, you should create another method to return the corresponding position, as shown below.

    @Override
    public Object getItem(int position) {
        //return this.itemList.get(itemList.size() - 1 - position);
        return this.itemList.get(getPosition(position));
    }
    
    public int getPosition(int position) {
        return itemList.size() - 1 - position;
    }
    

    Replace your intent in your onClick method with

    @Override
     public void onClick(View v) {
         Intent intent = new Intent(activity, ImageDisplayActivity.class);
    
         intent.putExtra("id", getPosition(position));
     ...
    

    That should solve the problem.

    To solve the image sharing problem, you need to modify the onCreateOptionsMenu method and add the onOptionsItemSelected method as I have shown below. You should reference the image path directly from the imagePaths list. You do not need to send the path from the first activity to the next activity via an intent because you already sent the position via the intent.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display,menu);
    
        // Locate MenuItem with ShareActionProvider
        MenuItem item = menu.findItem(R.id.menu_item_share);
    
        // Fetch and store ShareActionProvider
        shareActionProvider = (ShareActionProvider) item.getActionProvider();
    
        //   return true;
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
        case R.id.menu_item_share:
    
            Intent shareIntent = new Intent();
    
            if(shareActionProvider != null){            
                path = imagePaths.get(pager.getCurrentItem());
    
                imageFile = new File(path);
                imageUri = Uri.fromFile(imageFile);
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
                shareIntent.setType("image/jpeg");
    
                shareActionProvider.setShareIntent(shareIntent);
            }
    
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
提交回复
热议问题