android camera2 api recyclerview onclick

android camera2 api recyclerview o'clock

Implementing an interface to get the onClick position from the ImageAdapter to the activity

This tutorial explains how to setup the android camera2 api recyclerview onclick position so we can find the location of the image file.

Get Code

The code has been uploaded to github here

https://github.com/mobapptuts/recyclerview_image_gallery.git TAG camera2-recyclerview-onclick or

else you can run this command

git clone –branch camera2-recyclerview-onclick https://github.com/mobapptuts/recyclerview_image_gallery.git

Code Samples

Create the interface file

RecyclerViewClickPositionInterface

package nigelhenshaw.com.cameraintenttutorial;

/**
 * Created by nigelhenshaw on 28/09/2015.
 */
public interface RecyclerViewClickPositionInterface {
    public void getRecyclerViewAdapterPosition(int position);
}

CamaraIntentActivity

Implement the callback

public class CamaraIntentActivity extends Activity implements RecyclerViewClickPositionInterface {
@Override
  public void getRecyclerViewAdapterPosition(int position) {

      Toast.makeText(this, Integer.toString(position), Toast.LENGTH_SHORT).show();

  }

Add the interface to the ImageAdapter call

@Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_camara_intent);

       createImageGallery();

       mRecyclerView = (RecyclerView) findViewById(R.id.galleryRecyclerView);
       GridLayoutManager layoutManager = new GridLayoutManager(this, 1);
       layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
       mRecyclerView.setLayoutManager(layoutManager);
       RecyclerView.Adapter imageAdapter = new ImageAdapter(sortFilesToLatest(mGalleryFolder), this);
       mRecyclerView.setAdapter(imageAdapter);

       final int maxMemorySize = (int) Runtime.getRuntime().maxMemory() / 1024;
       final int cacheSize = maxMemorySize / 10;

       mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {

           @Override
           protected int sizeOf(String key, Bitmap value) {
               return value.getByteCount() / 1024;
           }
       };

       mTextureView = (TextureView) findViewById(R.id.textureView);
   }
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
     if(requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {
         RecyclerView.Adapter newImageAdapter = new ImageAdapter(sortFilesToLatest(mGalleryFolder), this);
         mRecyclerView.swapAdapter(newImageAdapter, false);

     }
 }
private void swapImageAdapter() {
    RecyclerView.Adapter newImageAdapter = new ImageAdapter(sortFilesToLatest(mGalleryFolder), this);
    mRecyclerView.swapAdapter(newImageAdapter, false);
}

ImageAdapter

Create the interface member in the ImageAdapter & initialise the member inside the constructor

  private static RecyclerViewClickPositionInterface mPositionInterface;

public ImageAdapter(File[] folderFiles, RecyclerViewClickPositionInterface positionInterface) {
      mPositionInterface = positionInterface;
      mImageFiles= folderFiles;
  }

Add the onClickListener to the ViewHolder & call the interface

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
       private ImageView imageView;

       public ViewHolder(View view) {
           super(view);

           view.setOnClickListener(this);
           imageView = (ImageView) view.findViewById(R.id.imageGalleryView);
       }

       public ImageView getImageView() {
           return imageView;
       }

       @Override
       public void onClick(View v) {
           mPositionInterface.getRecyclerViewAdapterPosition(this.getPosition());
       }
   }
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>