For every RoutineModel item you recreate detailAdapter but you need new instance. Check TODO in code below:
public class RoutineAdapter extends RecyclerView.Adapter<RoutineAdapter.ViewHolder> {
Context context;
ArrayList<RoutineModel> routineItems = new ArrayList<>();
//public RoutineDetailAdapter detailAdapter; //TODO move it to local variable in method onCreateViewHolder
OnRoutineItemClickListener listener;
like this
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.routine_item, parent, false);
ViewHolder holder = new ViewHolder(itemView);
RoutineDetailAdapter detailAdapter = new RoutineDetailAdapter(); //TODO Create local variable
detailAdapter.addItem(new RoutineDetailModel());
holder.setRoutineDetailRecyClerView();
holder.rv_detail.setAdapter(detailAdapter);
return holder;
}
But this is not good way to solve this... Its better to use one Adapter with multiple item views. I have no time now to explain, I will post some code later...
EDITED
I had same problem and and solved this on this (better) way:
Create Adapter that support multiple views
public class MultipleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<Object> items; //List<Object or Generic class or abstarct base Model class parent of Routine and RoutineDetails>
private OnItemClickListener listener; //TODO new interface
..............
}
Override onCreateViewHolder like this
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if(viewType == 1){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.routine_item, parent, false);
return new RoutineViewHolder (itemView);
}/*else{*/
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.routine_detail_item, parent, false);
return new RoutineDetailsViewHolder (itemView);
//}
}
Override onBindViewHolder like this and create your updateViews methods
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Object object = items.get(position); //Object or Generic class or abstarct base Model class parent of Routine and RoutineDetails>
if(object instanceof RoutineModel) {
//create method
updateRoutineViews((RoutineViewHolder) holder, (RoutineModel) object, position);
}else if(object instanceof RoutineDetailsModel) {
//create method
updateRoutineDetailsViewHolder((RoutineDetailsViewHolder) holder, (RoutineDetailModel) object);
}
}
and override getItemViewType method
@Override
public int getItemViewType(int position) {
Object object = items.get(position);
if(object instanceof RoutineModel){
return 1;
}
//else if instanceOf RoutineDetailModel return 0
return 0;
}
This is so simple and better for performance...
Also in OnItemClickListener get item by position and check instanceOf object like above. You can add new item to the clickedItemPosition + 1 with adapter.notifyDataSetChanges()
In MainActivity you have to create mixed list with Routine and Routine details
List<Object> mixedList = new ArrayList<>();
for(RoutineModel rm: routineList){
mixedList.add(rm);
if(rm has routineDetailModels){ //pseudo code
for(RoutineDetailModels rmdetilas: rm.getRoutineDetailModels()){
mixedList.add(rmdetilas);
}
}
}
adpater.swapData(mixedList); //create your method to swap or set data to adapter
EDITED
handle click
public interface OnItemClickListener{
void onClick(View view, int position);
void onLongClick(View view, int position);
}
and
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onClick(View view, int position) {
Object item = (Object) adapter.getItem(position);
if(item instanceof Routine) {
if (view.getId() == R.id.delete_id) {
//TODO delete form list, notifyItemRemoved or generate all mixeddata again and swap
} else if (view.getId() == R.id.whateveryouwant) {
//TODO doSomething()
}
}
}
@Override
public void onLongClick(View view, int position) {
}
});
The code above is just an example of how to solve the problem
For more help feel free to comment
EDITED 2
public class RoutineViewHolder extends RecyclerView.ViewHolder {
public RecyclerView rv_detail;
public TextView routine;
public Button addSet;
public Button deleteSet;
public ViewHolder(@NonNull View itemView) {
super(itemView);
//initViews(); in constructor
routine = itemView.findViewById(R.id.routine);
rv_detail = itemView.findViewById(R.id.detail_routine);
addSet = itemView.findViewById(R.id.add_set);
deleteSet = itemView.findViewById(R.id.delete_set);
}
}
update method called from onBindViewHolder
private void updateRoutineViews(RoutineViewHolder holder, RoutineModel routineItem, position){
holder.routine.setText(routineItem.getRoutine());
holder.rv_detail.setText(routineItem.getWhateverYouWant());
holder.deleteSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onClick(v, position); //or holder.getAdapterPosition()
}
});
}
COMPLETE CODE
Create complete new app to test it and understand (copy/paste this code), then integrate in your app...
There are more improvements but I don't have time, this is a quick fix
Activity
public class RoutineActivity extends AppCompatActivity {
private MultipleViewAdapter adapter;
private List<RoutineModel> routineList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routine);
RecyclerView contentList = findViewById(R.id.list);
contentList.setLayoutManager(new LinearLayoutManager(this));
adapter = new MultipleViewAdapter(this, new ArrayList<>());
contentList.setAdapter(adapter);
adapter.setOnItemClickListener(new MultipleViewAdapter.OnItemClickListener() {
@Override
public void onClick(View view, int position) {
Object item = (Object) adapter.getItem(position);
if(item instanceof RoutineModel) {
RoutineModel routineModel = (RoutineModel) item;
if (view.getId() == R.id.add_set) {
int weight = randomInt(99);
Toast.makeText(RoutineActivity.this, "New item with weight: " + weight + " kg", Toast.LENGTH_SHORT).show();
routineModel.addDetails(new RoutineDetailsModel(routineModel.getDetailsSize() + 1, weight));
adapter.swapData(getMixedList()); // OR add item to adapter and notify item inserted
//TODO implement SnappingLinearLayoutManager and set list.smoothScroleto... routineModel
} else if (view.getId() == R.id.delete_set) {
//TODO doSomething()
boolean deleted = routineModel.removeDetails(routineModel.getDetailsSize() - 1); // -1 !!! to delete last item
Toast.makeText(RoutineActivity.this, deleted ? "Last item is deleted": "No more items", Toast.LENGTH_SHORT).show();
adapter.swapData(getMixedList()); // OR remove item from adapter and notify item removed
//TODO implement SnappingLinearLayoutManager and set list.smoothScroleto.... routineModel
}
}else if(item instanceof RoutineDetailsModel) {
RoutineDetailsModel routineModel = (RoutineDetailsModel) item;
Toast.makeText(RoutineActivity.this, "Weight: " + routineModel.getWeight() + " kg", Toast.LENGTH_SHORT).show();
//TODO EDITED 4 (copy/paste this) random new Weight
routineModel.setWeight(randomInt(99));
adapter.notifyItemChanged(position);
Toast.makeText(RoutineActivity.this, "New Weight: " + routineModel.getWeight() + " kg", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLongClick(View view, int position) {
}
});
initFakeData();
adapter.swapData(getMixedList());
}
private List<Object> getMixedList() {
List<Object> mixedList = new ArrayList<>();
for(RoutineModel rm: routineList){
mixedList.add(rm);
if(rm.getRoutineDetailsModel() != null && rm.getRoutineDetailsModel().size() > 0){
for(RoutineDetailsModel rmdetilas: rm.getRoutineDetailsModel()){
mixedList.add(rmdetilas);
}
}
}
return mixedList;
}
private void initFakeData() {
routineList = new ArrayList<>();
for(int i = 0; i < 5; i++){
RoutineModel routineModel = new RoutineModel(String.valueOf(i + 1));
for(int j = 0; j < 4; j++){
routineModel.addDetails(new RoutineDetailsModel(j+1, randomInt(99)));
}
routineList.add(routineModel);
}
}
private int randomInt(int max) {
return (int) Math.floor(Math.random() * max);
}
}
Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Adapter
public class MultipleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context; // TODO only if you need conetxt
private List<Object> items;
private OnItemClickListener onItemClickListener;
public MultipleViewAdapter(Context context, List<Object> items) {
this.context = context;
this.items = items;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewH