Skip to content Skip to sidebar Skip to footer

How To Start Activity From Onclick On Recyclerview In Actvity

Hello Everyone I am trying to start activity when I click on RecyclerView , it is started when I add the code in the Custom Adapter , but when I write it in Activity it Just give

Solution 1:

Try this:

in your onResume(): you are already attaching setOnItemClickListener into the PhotoRecyclerViewAdapter

@OverrideprotectedvoidonResume() {
    super.onResume();
    ((PhotoRecyclerViewAdapter) mAdapter).setOnItemClickListener(newPhotoRecyclerViewAdapter
            .MyClickListener() {
        @OverridepublicvoidonItemClick(int position, View v) {
            Log.i(LOG_TAG, " Clicked on Item " + position);
            Intentintent=newIntent(FrameListActivity.this , FinalActivity.class);
            intent.putExtra("resultpos", "" + position);
            startActivity(intent);
        }
    });
}

Solution 2:

Use this:

@OverridepublicvoidonItemClick(int position, View v) {
        Log.e("TAG", "You clicked number " + mAdapter.getItemId(position) + ", which is at cell position " + position);
//        this.startActivity(new Intent(FrameListActivity.this , FinalActivity.class));//        Bitmap frameSelected = results.get(position).getImage();//        Log.e("frameSelected" , frameSelected+"");//        createImageFromBitmap(frameSelected , "frameImage");
        positionId = "" + position;
        Contextcontext= FrameListActivity.this;
        Intentintent=newIntent(context , FinalActivity.class);
        intent.putExtra("resultpos", "" + positionId);
        context.startActivity(intent);
        finish();
    }

Hope this helps.

Solution 3:

I used setOnClickListener in itemView in the onBindViewHolder.

the itemView is like the container of all things that has the item...

publicclassMyClassAdapterextendsRecyclerView.Adapter<MyClassAdapter.MyClassViewHolder> {
....
....
....
@OverridepublicvoidonBindViewHolder(@NonNullfinal MyClassViewHolder holder, int position) {

holder.itemView.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                Contextcontext= v.getContext();
                Intentintent=newIntent(context , NewActivity.class);
                context.startActivity(intent);
            }
        });
 }
....
....
}

This is another example that I find

I created the setOnClickListener on view in onCreateViewHolder

publicclassMyClassAdapterextendsRecyclerView.Adapter<MyClassAdapter.MyClassViewHolder> {
....
....
....
@NonNull@Overridepublic MyClassViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflaterlayoutInflater= LayoutInflater.from(parent.getContext());
        Viewview= layoutInflater.inflate(R.layout.row_item_example, parent, false);
        view.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                System.out.println("Click in oncReateViewHolder");
            }
        });
        returnnewSolicitudViewHolder(view);
    }
....
....
}

Solution 4:

As far as I know, the proper way to attach an onClickListener to a recyclerView, is to do so in the onBindViewHolder method as mentioned as the first option in the answer below. This method binds the properties to each of the recyclerview item, therefore the onClickListener could also be attached similarly.

Post a Comment for "How To Start Activity From Onclick On Recyclerview In Actvity"