简体   繁体   中英

How to start activty on recycler view item click?

I am feteching data from server using JSON and showing that data in recycler view. But the problem is I may need to add some new data to the server. Using volley, request will be send to server and recycler view list will be updated. So the problem is I want to open new activity on each recycler view item click. But my recycler view list will increase if I added some new data to the server. So, I can't change layout/start activity based on itemclick postion. Is there any better way to implement this?

Just like whatsapp, I want to open new activity/fragment on itemclick of recycler view.

This what I tried to do but this is not proper way. Coz I don't know how many item will be present inside recyclerview. Here is my code:

private final Context context;

public MyViewHolder(View itemView) {
    super(itemView);
    context = itemView.getContext();

}

   @Override
public void onClick(View v) {          

    final Intent intent;
    switch (getAdapterPostion()){
        case 0:
           intent =  new Intent(context, FirstActivity.class);
           break;

        case 1:
            intent =  new Intent(context, SecondActivity.class);
            break;
           ...
        default:
           intent =  new Intent(context, DefaultActivity.class);
           break;
     }
    context.startActivity(intent);
}

If you trying to replicate the functionality of WhatsApp pay attention that when you click on an item on a list the app will not open a new Activity, it will open the same Activity sending different data to load the data for that specific user.

...
Intent intent = new Intent(context, Chat.class);
intent.putExtra("EXTRA_USER_ID", userId);
startActivity(intent);
...

On the Chat.class

...
Intent intent = getIntent();
String userId = intent.getStringExtra("EXTRA_USER_ID");
...

Using this user id you can load data for this specific user.

a proper approach is using an interface in your adapter class:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private List<Item> items;
    private final OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Item item);
    }

public static class MyViewHolder extends RecyclerView.ViewHolder {

        public void bind (final Item item, final OnItemClickListener listener) {

            yourclickableView.setOnClickListener(new View.OnClickListener() {
                @Override 
                public void onClick(View v) {
                    listener.onItemClick(item);
                }
            });
        }
    }

public MyAdapter(List<Item> items, OnItemClickListener listener) {
        ...
        this.listener = listener;
        items = items;
    }

and finally:

@Override 
public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.bind(items.get(position), listener);
        ...
    }

and whenever using your adapter class:

    myAdapter = new MyAdapter (itemsList, new MyAdapter.OnItemClickListener(){
                @Override 
                public void onItemClick(Item item) {
                  //bluh bluh...
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM