0

Con el uso de recyclerview estoy haciendo un calendario, y lo que quiero hacer es de que cuando el usuario se deslize hacia abajo o hacia arriba el elemento que pase por el centro se pueda fijar y considerar que esta seleccionado.

introducir la descripción de la imagen aquí

Como se puede apreciar el numero que pasa por el marcador no se fija dentro de el.

Aqui les dejo el codigo hasta donde tengo hecho:

widget_bottom_sheet_fecha_nacimiento.xml:

<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="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/imageView19"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:contentDescription="@string/imageView_contentDescription"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent=".1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="1"
        app:srcCompat="@drawable/ic_drag_handle_grey700_20dp" />

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:divider="@drawable/divisor_horizontal"
        android:orientation="vertical"
        android:showDividers="middle"
        android:weightSum=".9"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent=".9"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView19"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="1">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView19">

            <TextView
                android:id="@+id/textView30"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:gravity="bottom"
                android:paddingStart="30dp"
                android:paddingEnd="30dp"
                android:text="@string/textView_acceso_recuperar_contrasena_busqueda_avanzada_buscar_cuenta_calendar_titulo"
                android:textColor="@color/Grey800"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@+id/textView31"
                app:layout_constraintEnd_toStartOf="@+id/imageView20"
                app:layout_constraintHeight_default="percent"
                app:layout_constraintHeight_percent=".5"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_chainStyle="packed"
                app:layout_constraintWidth_default="percent"
                app:layout_constraintWidth_percent=".7" />

            <TextView
                android:id="@+id/textView31"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:paddingStart="30dp"
                android:paddingEnd="30dp"
                android:text="@string/textView_acceso_recuperar_contrasena_busqueda_avanzada_buscar_cuenta_calendar_subtitulo"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="@+id/textView30"
                app:layout_constraintHeight_default="percent"
                app:layout_constraintHeight_percent=".5"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="@+id/textView30"
                app:layout_constraintTop_toBottomOf="@+id/textView30"
                app:layout_constraintWidth_default="percent"
                app:layout_constraintWidth_percent=".7" />

            <ImageView
                android:id="@+id/imageView20"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:contentDescription="@string/imageView_contentDescription"
                android:scaleType="centerInside"
                android:src="@drawable/ic_calendar_black_30dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_default="percent"
                app:layout_constraintHeight_percent="1"
                app:layout_constraintStart_toEndOf="@+id/textView30"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintWidth_default="percent"
                app:layout_constraintWidth_percent=".3" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".5">

            <LinearLayout
                android:id="@+id/linearLayout5"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:baselineAligned="false"
                android:divider="@drawable/divisor_vertical"
                android:orientation="horizontal"
                android:showDividers="middle"
                android:weightSum=".9"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".3">

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView_Dia"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:clipToPadding="false"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        tools:listitem="@layout/item_bottom_sheet_fecha_nacimiento" />

                    <View
                        android:id="@+id/view4"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:alpha=".4"
                        android:background="@drawable/select_dia_calendar"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHeight_default="percent"
                        app:layout_constraintHeight_percent=".35"
                        app:layout_constraintHorizontal_bias="1.0"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintWidth_default="percent"
                        app:layout_constraintWidth_percent=".95" />
                </androidx.constraintlayout.widget.ConstraintLayout>

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".3">

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView_Mes"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHeight_default="percent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <View
                        android:id="@+id/view5"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:alpha=".4"
                        android:background="@drawable/select_mes_calendar"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHeight_default="percent"
                        app:layout_constraintHeight_percent=".35"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintWidth_default="percent"
                        app:layout_constraintWidth_percent="1" />
                </androidx.constraintlayout.widget.ConstraintLayout>

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".3">

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView_Year"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <View
                        android:id="@+id/view6"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:alpha=".4"
                        android:background="@drawable/select_year_calendar"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHeight_default="percent"
                        app:layout_constraintHeight_percent=".35"
                        app:layout_constraintHorizontal_bias="0.0"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintWidth_default="percent"
                        app:layout_constraintWidth_percent=".95" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </LinearLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <Button
                android:id="@+id/btn_Aceptar"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginEnd="16dp"
                android:background="@drawable/button_corners_oval_transparent"
                android:foreground="?attr/selectableItemBackgroundBorderless"
                android:text="@string/button_aceptar"
                android:textColor="@color/tea700"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_default="percent"
                app:layout_constraintHeight_percent=".8"
                app:layout_constraintStart_toEndOf="@+id/btn_Cancelar"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintWidth_default="percent"
                app:layout_constraintWidth_percent=".4" />

            <Button
                android:id="@+id/btn_Cancelar"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginStart="16dp"
                android:background="@drawable/button_corners_oval_transparent"
                android:foreground="?attr/selectableItemBackgroundBorderless"
                android:text="@string/button_cancelar"
                android:textColor="@color/Red800"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="@+id/btn_Aceptar"
                app:layout_constraintEnd_toStartOf="@+id/btn_Aceptar"
                app:layout_constraintHeight_default="percent"
                app:layout_constraintHeight_percent=".8"
                app:layout_constraintHorizontal_chainStyle="spread_inside"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/btn_Aceptar"
                app:layout_constraintWidth_default="percent"
                app:layout_constraintWidth_percent=".4" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerAdapterCalendario.java:

public class RecyclerAdapterCalendario extends RecyclerView.Adapter<RecyclerAdapterCalendario.ViewHolderItem> {

// region Variables

private Context context;
private ArrayList<ItemCalendario> listaValues;

// endregion

// region Constructor

public RecyclerAdapterCalendario(Context context, ArrayList<ItemCalendario> listaValues) {
    this.context = context;
    this.listaValues = listaValues;
}

// endregion

// region CallBack System

@NonNull
@Override
public ViewHolderItem onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bottom_sheet_fecha_nacimiento, parent, false);
    return new ViewHolderItem(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolderItem holder, int position) {
    holder.textView.setText(listaValues.get(position).getValue());
}

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

// endregion

// region SuperClass

public class ViewHolderItem extends RecyclerView.ViewHolder {
    TextView textView;

    public ViewHolderItem(@NonNull View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.txtView);
    }
}

// endregion
}

Fragment.java:

private void mostrarCalendario() {
    final View view = getLayoutInflater().inflate(R.layout.widget_bottom_sheet_fecha_nacimiento, null);
    final Button buttonAceptar = view.findViewById(R.id.btn_Aceptar);
    final Button buttonCancelar = view.findViewById(R.id.btn_Cancelar);
    final LinearLayoutManager manager = new LinearLayoutManager(getContext());
    buttonAceptar.setClipToOutline(true);
    buttonCancelar.setClipToOutline(true);

    for (int i = 0; i < 30; i++) {
        listaValues.add(new ItemCalendario(String.valueOf(i)));
    }

    adapterCalendario = new RecyclerAdapterCalendario(getContext(), listaValues);
    recyclerViewDias = view.findViewById(R.id.recyclerView_Dia);
    recyclerViewDias.setHasFixedSize(true);
    recyclerViewDias.setAdapter(adapterCalendario);
    recyclerViewDias.setLayoutManager(manager);
    bottomSheetDialogCalendario.setContentView(view);
    bottomSheetDialogCalendario.setCancelable(false);
    bottomSheetDialogCalendario.create();
    bottomSheetDialogCalendario.show();
}

1 respuesta 1

0

Ya no es necesario establecer el layoutManger en el codigo java, se puede establecer directamente en el xml:

<androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView_Dias"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:clipToPadding="false"
                        android:nestedScrollingEnabled="false"
                        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        tools:listitem="@layout/item_bottom_sheet_fecha_nacimiento" />

                    <View
                        android:id="@+id/view4"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:alpha=".4"
                        android:background="@drawable/select_dia_calendar"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHeight_default="percent"
                        app:layout_constraintHeight_percent=".35"
                        app:layout_constraintHorizontal_bias="1.0"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintWidth_default="percent"
                        app:layout_constraintWidth_percent=".95" />

                </androidx.constraintlayout.widget.ConstraintLayout>

Para poder centrar un elemento mientras se desliza el recyclerView utilizar:

SnapHelper snapHelperDias = new LinearSnapHelper();
snapHelperDias.attachToRecyclerView(recyclerViewDias);

Quedando:

private void mostrarCalendario() {
    final View view = getLayoutInflater().inflate(R.layout.widget_bottom_sheet_fecha_nacimiento, null);
    final Button buttonAceptar = view.findViewById(R.id.btn_Aceptar);
    final Button buttonCancelar = view.findViewById(R.id.btn_Cancelar);
    final LinearLayoutManager manager = new LinearLayoutManager(getContext());
    buttonAceptar.setClipToOutline(true);
    buttonCancelar.setClipToOutline(true);

    for (int i = 0; i < 30; i++) {
        listaValues.add(new ItemCalendario(String.valueOf(i)));
    }

    adapterCalendario = new RecyclerAdapterCalendario(getContext(), listaValues);
    recyclerViewDias = view.findViewById(R.id.recyclerView_Dia);
    recyclerViewDias.setHasFixedSize(true);
    recyclerViewDias.setAdapter(adapterCalendario);
    SnapHelper snapHelperDias = new LinearSnapHelper();
    snapHelperDias.attachToRecyclerView(recyclerViewDias);
    bottomSheetDialogCalendario.setContentView(view);
    bottomSheetDialogCalendario.setCancelable(false);
    bottomSheetDialogCalendario.create();
    bottomSheetDialogCalendario.show();
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.