-1

I am making an app which has a main recycler view and an internal recycler view that goes inside the main recycler view. The problem is that the list of elements is not displayed inside the recycler view, I have tried several things that you mention in this forum but I have not had good results. I read about nestedscrollview but it has not worked either, the last thing I tried was what I leave you in the code. This works but it is not efficient since in the emulator there are times when paste the scroll view (so the data is displayed "in jerks"

recycler

public class adapterArguments extends RecyclerView.Adapter<adapterArguments.ViewHolder> {
    private List<ListArguments> data;
    private LayoutInflater inflater;
    private Context context;
    public String position_selected;
    public static String argument;
    public adapterArguments listArguments;
    RecyclerView recyclerView_arguments;



    public adapterArguments(List<ListArguments> itemList, Context context) {
        this.data = itemList;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.compontents_arguments_recyclerview,null);
        recyclerView_arguments= view.findViewById(R.id.recycler_argument);
        return new adapterArguments.ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ConexionSQliteHelper conn=new ConexionSQliteHelper(context,"db_testwuad_7",null,31);
        holder.arguments.setText(data.get(position).getArguments());
        holder.name_user.setText(data.get(position).getName_user());
        holder.date.setText(data.get(position).getDate_arguments());
        holder.position.setText(data.get(position).getPosition());
        int id_argument_selected = data.get(position).getId();
        position_selected = holder.position.getText().toString();
        holder.list_contrarguments = conn.getContrarguments(id_argument_selected);
        holder.recyclerView_contrarguments.setLayoutManager(new LinearLayoutManager(context));
        holder.recyclerView_contrarguments.setHasFixedSize(true);
        holder.listContrarguments = new adapterContrarguments(holder.list_contrarguments,context);
        holder.recyclerView_contrarguments.setAdapter(holder.listContrarguments);
        final int[] checkedItem = {-1};
        if(position_selected.equals("A favor")){
            holder.position.setBackgroundColor(Color.parseColor("#073FD7"));
        }
        else{
            holder.position.setBackgroundColor(Color.parseColor("#FC0C1E"));

        }

        holder.addContrarguments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
                alertDialog.setTitle("Añadir un Contrargumento");
                alertDialog.setMessage("¿Quieres añadir un contrargumento a este texto?");
                alertDialog.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                argument = holder.arguments.getText().toString();
                                contrargument fragment = new contrargument();
                                Bundle args = new Bundle();
                                args.putString("argument", argument);
                                args.putInt("id",id_argument_selected);
                                fragment.setArguments(args);
                                FragmentManager fragmentManager;
                                FragmentTransaction fragmentTransaction;
                                fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
                                fragmentTransaction = fragmentManager.beginTransaction();
                                fragmentTransaction.replace(R.id.container, fragment);
                                fragmentTransaction.addToBackStack(null);
                                fragmentTransaction.commit();
                            }
                        });
                alertDialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                            }
                        });
                AlertDialog customAlertDialog = alertDialog.create();
                customAlertDialog.show();
            }
        });

        holder.deleteDiscuss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
                alertDialog.setTitle("Eliminar argumento");
                alertDialog.setMessage("¿Quieres eliminar un argumento a este texto?");
                alertDialog.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        boolean user_create_debate = conn.verifyCreatorDebate(holder.name_user.getText().toString());
                        if(user_create_debate){
                            conn.deleteArgument(id_argument_selected);
                            data = conn.getArguments(id_argument_selected);
                            listArguments =  new adapterArguments(data,context);
                            notifyDataSetChanged();
                        }
                        else{
                            Toast.makeText(context, "No puede eliminar el argumento,ya que no es el creador del texto", Toast.LENGTH_SHORT).show();
                        }
                        opinionUserOnPostDebate fragment = new opinionUserOnPostDebate();
                        FragmentManager fragmentManager;
                        FragmentTransaction fragmentTransaction;
                        fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
                        fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.detach(fragment).attach(fragment).commit();

                    }
                });
                alertDialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    }
                });
                AlertDialog customAlertDialog = alertDialog.create();
                customAlertDialog.show();
            }
        });
        holder.changePosition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
                alertDialog.setTitle("Cambio de postura");
                final String[] listItems = new String[]{"Cambie de opinión","Me equivoque de botón","Otro"};
                alertDialog.setSingleChoiceItems(listItems, checkedItem[0], new DialogInterface.OnClickListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        checkedItem[0] = which;
                    }
                });
                alertDialog.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(position_selected.equals("A favor")){
                           //conn.changePosition(id_argument_selected,"En contra");
                        }
                        else{
                            //conn.changePosition(id_argument_selected,"A favor");

                        }

                    }
                });
                alertDialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog customAlertDialog = alertDialog.create();
                customAlertDialog.show();
            }
        });
        holder.Report.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
                alertDialog.setTitle("Reportar post");
                final String[] listItems = new String[]{"Opinión contiene lenguaje ofensivo","Opinión contiene información personal","Otro"};
                alertDialog.setSingleChoiceItems(listItems, checkedItem[0], new DialogInterface.OnClickListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        checkedItem[0] = which;
                    }
                });
                alertDialog.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(position_selected.equals("A favor")){
                            //conn.changePosition(id_argument_selected,"En contra");
                        }
                        else{
                            //conn.changePosition(id_argument_selected,"A favor");

                        }

                    }
                });
                alertDialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog customAlertDialog = alertDialog.create();
                customAlertDialog.show();
            }
        });

    }

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


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView arguments,name_user,date,position;
        ImageButton addContrarguments,deleteDiscuss,addFriend,changePosition,Report;
        List<ListContrarguments> list_contrarguments;
        RecyclerView recyclerView_contrarguments;
        public adapterContrarguments listContrarguments;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            arguments = itemView.findViewById(R.id.arguments_posted);
            name_user = itemView.findViewById(R.id.name_arguments);
            date = itemView.findViewById(R.id.date_arguments);
            position = itemView.findViewById(R.id.position);
            addContrarguments = itemView.findViewById(R.id.comments);
            deleteDiscuss = itemView.findViewById(R.id.delete);
            changePosition = itemView.findViewById(R.id.change_position);
            addFriend = itemView.findViewById(R.id.add_friend);
            Report = itemView.findViewById(R.id.report_argument);
            recyclerView_contrarguments = itemView.findViewById(R.id.recycler_contrargument);
            recyclerView_contrarguments.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
                @Override
                public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
                    int action = e.getAction();
                    switch (action) {
                        case MotionEvent.ACTION_MOVE:
                            rv.getParent().requestDisallowInterceptTouchEvent(true);
                            break;
                    }
                    return false;
                }

                @Override
                public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {

                }

                @Override
                public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

                }
            });


        }
    }
}

layout.compontents_arguments_recyclerview.xml:external recyclerview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/position"
                android:textColor="@color/white"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:textStyle="bold"
                android:textSize="25dp"
                android:background="#0AA842">
            </TextView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/image_person_contrarguments"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:layout_marginRight="5dp">

            </ImageView>

            <TextView
                android:id="@+id/name_arguments"
                android:layout_width="145dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="15dp"
                android:textSize="14sp"
                >

            </TextView>

            <TextView
                android:id="@+id/date_arguments"
                android:layout_width="145dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                >

            </TextView>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <TextView
                android:id="@+id/arguments_posted"
                android:layout_width="match_parent"
                android:layout_height="140dp"
                >

            </TextView>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:orientation="horizontal"
                android:background="#FEFEFE"
                >
                <ImageButton
                    android:id="@+id/comments"
                    android:layout_width="60dp"
                    android:layout_height="match_parent"
                    android:src="@drawable/add_comments"
                    android:background="#FEFEFE"
                    android:layout_marginLeft="50dp"
                    >
                </ImageButton>
                <ImageButton
                    android:id="@+id/delete"
                    android:layout_width="60dp"
                    android:layout_height="match_parent"
                    android:src="@drawable/delete"
                    android:background="#FEFEFE"
                    >
                </ImageButton>
                <ImageButton
                    android:id="@+id/change_position"
                    android:layout_width="60dp"
                    android:layout_height="match_parent"
                    android:src="@drawable/change_opinion"
                    android:background="#FEFEFE"
                    >

                </ImageButton>
                <ImageButton
                    android:id="@+id/add_friend"
                    android:layout_width="60dp"
                    android:layout_height="match_parent"
                    android:src="@drawable/add_friend"
                    android:background="#FEFEFE"
                    >
                </ImageButton>
                <ImageButton
                    android:id="@+id/report_argument"
                    android:layout_width="60dp"
                    android:layout_height="match_parent"
                    android:src="@drawable/display_report"
                    android:background="#FEFEFE"
                    >
                </ImageButton>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#A4A1A1"
                >
            </View>

            <include
                layout="@layout/recyclerview_contrarguments"
                android:layout_width="match_parent"
                android:layout_height="281dp">

            </include>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

recyclerviee_contrarguments:intern recyclerview

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scroll_discuss"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_contrargument"
                android:layout_width="442dp"
                android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
  • Do not put a vertical RV inside another vertical RV .. that defeats the whole purpose of recycling views .. you might wanna rethink your UI . maybe make inner RV a Horizontal one .. – ADM Aug 30 '22 at 05:27

1 Answers1

0

Check to see if this helps. https://stackoverflow.com/a/37338715/19652887

Also, can u try changing the parent layout of your recycler view from linear layout to constraint layout and use recycler view's width as 0dp? That's what fixed my problem

Hope this helps.

<androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/device_toolbar">

        <com.google.android.material.card.MaterialCardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_3sdp"
            android:layout_marginBottom="@dimen/_50sdp"
            android:elevation="@dimen/_5sdp"
            app:cardCornerRadius="@dimen/_20sdp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/_10sdp"
                android:paddingVertical="@dimen/_10sdp"
                android:paddingStart="@dimen/_5sdp"
                android:paddingEnd="@dimen/_5sdp">


                .......(other views).......
                
                

                <com.google.android.material.button.MaterialButton
                    android:id="@+id/grant_storage_permission"
                    style="@style/Widget.Material3.Button.OutlinedButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:text="Grant Permission"
                    android:textColor="@color/color_on_primary_light"
                    android:textSize="@dimen/_11ssp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/storage_text"
                    app:strokeColor="@color/color_secondary_variant_light" />

                <androidx.constraintlayout.widget.Group
                    android:id="@+id/group_storage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="visible"
                    app:constraint_referenced_ids="grant_storage_permission, storage_text" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/storage_rec_view"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:visibility="visible"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/used_ram_text"
                    android:layout_marginTop="@dimen/_10sdp" />

                <LinearLayout
                    android:id="@+id/storage_group_padding"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="@dimen/_20sdp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/grant_storage_permission" />


            </androidx.constraintlayout.widget.ConstraintLayout>

        </com.google.android.material.card.MaterialCardView>

    </androidx.core.widget.NestedScrollView>

M Ashhal
  • 389
  • 2
  • 12