-2

I try to move to a new activity when I click on RecyclerView. like if click the first item in Recycler view I will move to activity 1 and if click the second item in Recycler view I will move to activity 2 like that.

this is a code of adapter RecyclerView.

public class AdapterCatgery extends RecyclerView.Adapter<AdapterCatgery.ViewHolder> {
    private Context mContext;
    private ArrayList<ListCatgery> mExampleList;
    private AdapterCatgery.OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    public void setOnItemClickListener(AdapterCatgery.OnItemClickListener listener) {
        mListener = listener;
    }
    public AdapterCatgery(Context context, ArrayList<ListCatgery> exampleList) {
        mContext = context;
        this.  mExampleList = exampleList;
    }
    @Override
    public AdapterCatgery.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.activity_example_item, parent, false);
        return new AdapterCatgery.ViewHolder(v);

    }
    @Override
    public void onBindViewHolder(final AdapterCatgery.ViewHolder holder, int position) {
        ListCatgery currentItem = mExampleList.get(position);
        String image = currentItem.getImageurlte();
        String price = currentItem.getNamete();
        holder.text_titel.setText(price);

        Picasso.get().load(image).fit().centerInside().into(holder.mImageView);
    }

    @Override
    public int getItemCount() {
        return mExampleList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageView;
        public TextView text_titel;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.image_view);
            text_titel = itemView.findViewById(R.id.text_titel);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            mListener.onItemClick(position);
                        }
                    }
                }
            });
        }

    }
}

If anyone knows the solution please help me

brew
  • 91
  • 2
  • 11
marwan
  • 115
  • 11
  • I don't understand the purpose of the this, let us suppose that if you 100 items in a recycler view then you probably not going to create 100 activities – brew May 24 '20 at 20:47
  • 1
    @brew sometimes it's best not to question _why_ and instead just help with the _how_ :) this is a valid scenario for menus or categories with a limited amount of entries in them – a_local_nobody May 24 '20 at 20:52
  • Does this answer your question? [RecyclerView onClick](https://stackoverflow.com/questions/24471109/recyclerview-onclick) – a_local_nobody May 24 '20 at 20:53
  • I will explain to you \ I will add 12 item \ I am using (RecyclerView) in order not use (Button) I have put (RecyclerView) in the main screen through which I can move to item 12 so If you are as I user in my app and you are open the app now and you want go for sports section yo will go in (RecyclerView)and choose image of sport that appears to you – marwan May 24 '20 at 21:01
  • The idea will be like this – marwan May 24 '20 at 21:01
  • I was can use (image view )and make it as onclick in my app but wanted to use (RecyclerView ) so that I could update the image at all times in the database – marwan May 24 '20 at 21:03
  • @a_local_nobody actually I use it to make menus – marwan May 24 '20 at 21:19
  • @a_local_nobody thanks for giving me the idea to use it as menus. I had never thought of using Recycler view like menus – brew May 24 '20 at 21:43
  • @marwan I had posted the code you check it – brew May 24 '20 at 21:44
  • @brew The programming style is different and each programmer has his style – marwan May 24 '20 at 22:04
  • @marwan thanks you so much – brew May 24 '20 at 22:06

1 Answers1

1

Step 1: You need to create a Utility class which has the mapping for your navigation

public class Constant {
  public static final Class CAT_ONE = YourActivityclass.class;
  public static final Class CAT_TWO = YourActivityclass.class;
  public static final Class CAT_THREE = YourActivityclass.class;

 public static Class getActivity(int navigateTo) {
   switch(navigateTo) {
      case 1:
        return CAT_ONE;
      case 2:
        return CAT_ONE;
      case 3:
        return CAT_ONE;
   }
   return null;
 }

}

Step 2: then in your Activity class after setting up adapter setup on OnItemClickListener here you need to get the activity at specific position using Utility class

mAdapter.setOnItemClickListener(new AdapterCategory.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            Class navigateTo = Constant.getActivity(position);
            //avoid null and class class cast exception
            if (navigateTo != null && navigateTo instanceof Activity) {
                start(navigateTo);
            }
        }
    });


  //this is the custom start method to start your activity based on your position 
  private void start(Class<? extends Activity> token) {
   Intent intent = new Intent(this, token);
   startActivity(intent);
 }
brew
  • 91
  • 2
  • 11
  • Thank you so much.Also I have used from your code (switch) in AdapterCatgery it's work also – marwan May 24 '20 at 22:02