0

May I know if there's any way I can move my current CardView to center horizontal when I launch the activity, code below is from my main activity and I wanted to launch with the 3rd card (which is the middle one). Any help is much appreciated, thanks!

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.cardRecyclerView);
recyclerView.setLayoutManager(layoutManager);
CardAdapter adapter = new CardAdapter(this, mNames, mImages, mCards);
adapter.getItemId(3);
recyclerView.setAdapter(adapter);
recyclerView.scrollToPosition(2);

Here's my main activity

<?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="match_parent"
    tools:context=".HomeActivity"
    android:background="#f2f2f2">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/cardRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

        </androidx.recyclerview.widget.RecyclerView>
    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

and my list layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerInParent="true">

    <androidx.cardview.widget.CardView
        android:id="@+id/card"
        android:layout_width="300dp"
        android:layout_height="500dp"
        android:layout_centerInParent="true"
        android:layout_marginStart="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginBottom="15dp"
        app:cardCornerRadius="60dp"
        app:cardElevation="1dp"
        app:cardMaxElevation="2dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/cardImage"
                android:layout_width="240dp"
                android:layout_height="240dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="60dp"
                app:srcCompat="@android:color/holo_blue_dark" />

            <TextView
                android:id="@+id/cardText"
                android:layout_width="240dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="330dp"
                android:textAlignment="center"
                android:textColor="#FFF"
                android:textSize="60sp"
                android:textStyle="bold" />
        </RelativeLayout>
    </androidx.cardview.widget.CardView>

</RelativeLayout>

(Sorry if my explanation isn't really clear)

Here's my current results: sample 1

Here's what I'm trying to achieve: sample 2

Noxagon
  • 15
  • 6

1 Answers1

0

You can use LayoutManager#scrollToPositionWithOffset, you only have to calculate the offset which is I believe the (recyclerView.width - itemWidth) / 2, so the final code should be something like this

private void scrollToCenter(int position) {
    int itemWidth = recyclerView.get(position).getWidth();
    recyclerView.scrollToPositionWithOffset(position, (recyclerView.getWidth() - itemWidth)/2);
}

    
Filip Sollár
  • 91
  • 1
  • 5
  • I've tried your method but I couldn't find get(position) from both recyclerView or LayoutManager though, but I managed to solve it using Dushyant's comment, still thanks for your help! – Noxagon Jun 28 '20 at 02:59