Untitled

 avatar
user_9588696695
plain_text
2 years ago
4.1 kB
4
Indexable
Never
// di xml nya

<RelativeLayout
    android:id="@+id/rl_newArrival"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/dp_8"
    android:layout_marginBottom="@dimen/dp_16"
    android:background="@drawable/bg_gradasi_home"
    android:visibility="gone"
    tools:visibility="visible">

    <ImageView
        android:layout_width="175dp"
        android:layout_height="125dp"
        android:src="@drawable/ic_line_new_arrival"
        android:layout_alignParentBottom="true"/>

    <RelativeLayout
        android:id="@+id/rl_parallax"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/dp_8"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:paddingStart="@dimen/dp_8">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="45dp"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_car_white"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/new_arrival"
                android:textColor="@color/md_white_1000"
                android:textSize="19sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:lineSpacingExtra="4dp"
                android:text="@string/new_arrival_text"
                android:textColor="@color/md_white_1000"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/tv_moreNewArrival"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_marginTop="@dimen/dp_16"
                android:background="@drawable/bg_line_white"
                android:paddingHorizontal="@dimen/dp_16"
                android:text="@string/see_all"
                android:textColor="@color/md_white_1000"
                android:textStyle="bold"
                android:gravity="center"/>
        </LinearLayout>
    </RelativeLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_newArrival"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginVertical="@dimen/dp_8"/>
</RelativeLayout>


//di activity nya

newArrivalAdapter = NewArrivalAdapter(requireActivity(), ArrayList())
val linearLayout = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
rv_newArrival.layoutManager = linearLayout
rv_newArrival.adapter = newArrivalAdapter

rv_newArrival.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy)
        if (linearLayout.findFirstVisibleItemPosition() == 0) {
            val firstVisibleItem: View =
                linearLayout.findViewByPosition(linearLayout.findFirstVisibleItemPosition())!!
            val distanceFromLeft =
                firstVisibleItem.left.toFloat() // distance from the left
            val translateX = distanceFromLeft * 0.2f // move x distance
            rl_parallax.translationX = translateX
            if (distanceFromLeft <= 0) {
                val itemSize =
                    firstVisibleItem.width.toFloat() // view size
                val alpha =
                    Math.abs(distanceFromLeft) / itemSize * 1.5f // view transparency
                //                        //Set alpha to image to bring 'fade out' and 'fade in' effect
                rl_parallax.alpha = 1 - alpha
            }
        }
    }
})