'Performing different actions based on the list populating the recyclerview
How do I populate my android recyclerview with different lists such that different actions are performed based on the list. For example,clicking on an item of list A would do something different from clicking on an item on list B. Do I have to create separate adapters for each.
Solution 1:[1]
You should have an interface which handle the different actions from the different lists:
public interface handleRecyclerViewClicksInterface {
public void handleListAClick();
public void handleListBClick();
public void handleListCClick();
}
Then your adapter should looks like that:
package com.wimova.driver.service;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.wimova.driver.component.adapter.NewRidesAdapter;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private HandleRecyclerViewClicksInterface handleRecyclerViewClicksInterface;
private List<String> myDataset = new ArrayList<>();
public MyAdapter(HandleRecyclerViewClicksInterface handleRecyclerViewClicksInterface){
this.handleRecyclerViewClicksInterface = handleRecyclerViewClicksInterface;
}
@NonNull
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
switch (viewType) {
case 2:
return new MyViewHolder(parent, view -> handleRecyclerViewClicksInterface.handleBClick());
case 3:
return new MyViewHolder(parent, view -> handleRecyclerViewClicksInterface.handleCCLick());
default:
return new MyViewHolder(parent, view -> handleRecyclerViewClicksInterface.handleAClick()); // here we will treat the cases < 2 or > 3
}
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
// handle on bind
}
@Override
public int getItemCount() {
return myDataset.size();
}
protected static class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(@NonNull View itemView, View.OnClickListener onClickListener) {
super(itemView);
itemView.setOnClickListener(onClickListener);
}
}
@Override
public int getItemViewType(int position) {
// Return the correct item view type depending on the dataset (you need to implement your own logic)
if(myDataset.get(position).equals("handleA")){
return 1;
}
if(myDataset.get(position).equals("handleB")){
return 2;
}
if(myDataset.get(position).equals("handleC")){
return 3;
}
// it it match no case, return the default one
return 1;
}
}
class MainActivity extends Activity {
private RecyclerView recyclerView;
private MyAdapter adapter;
private HandleRecyclerViewClicksInterface handleRecyclerViewClicksInterface;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = findViewById(R.id.main_recyclerview);
handleRecyclerViewClicksInterface = new HandleRecyclerViewClicksInterface() {
@Override
public void handleAClick() {
// Example : Launch other activity
}
@Override
public void handleBClick() {
// Example : Send notification
}
@Override
public void handleCCLick() {
// Example : Delete row
}
};
adapter = new MyAdapter(handleRecyclerViewClicksInterface);
recyclerView.setAdapter(adapter);
}
}
Solution 2:[2]
better way is to create separate adapters (if list are not too many) put list inside framelayout in main file where you want to show list
<FrameLayout.....</FrameLayout>
then replace the frame on the basis of the list selected create different fragment for all list and handle them in that their respective fragment class
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Skylek |
| Solution 2 | tintin |
