0

I am trying to use a Carousel to show different picture. the carousel is in a MotionLayout and the animation are working fine (swipe right and left to navigate and swipe up to remove the item) but when removing the item on a swipe up from the list given to the Carousel.Adapter, the carousel is not updated. When doing a carousel.refresh() it is updated but laggy and some picture are invisible until I scroll again.

layout :


<androidx.constraintlayout.motion.widget.MotionLayout
    android:id="@+id/motionLayout"
    android:layout_width="match_parent"
    android:layout_height="450dp"
    android:layout_marginBottom="40dp"
    app:layoutDescription="@xml/fragment_background_scene"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView00"
        android:layout_width="180dp"
        android:layout_height="320dp"
        app:cardCornerRadius="8dp"
        android:elevation="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ImageView
            android:id="@+id/imageView00"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black"
            android:scaleType="centerCrop"
            />
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView0"
        android:layout_width="180dp"
        android:layout_height="320dp"
        app:cardCornerRadius="8dp"
        android:layout_margin="5dp"
        android:elevation="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

                <ImageView
                    android:id="@+id/imageView0"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/black"
                    android:scaleType="centerCrop"
                    />
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView1"
        android:layout_width="180dp"
        android:layout_height="320dp"
        app:cardCornerRadius="8dp"
        android:layout_margin="5dp"
        android:elevation="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/blue"
            android:scaleType="centerCrop"
            />
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="180dp"
        android:layout_height="320dp"
        app:cardCornerRadius="8dp"
        android:layout_margin="5dp"
        android:elevation="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/yellow"
            android:scaleType="centerCrop"
            />
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView3"
        android:layout_width="180dp"
        android:layout_height="320dp"
        app:cardCornerRadius="8dp"
        android:layout_margin="5dp"
        android:elevation="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/light_blue"
            android:scaleType="centerCrop"
            />
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView4"
        android:layout_width="180dp"
        android:layout_height="320dp"
        app:cardCornerRadius="8dp"
        android:layout_margin="5dp"
        android:elevation="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/pink"
            android:scaleType="centerCrop"
            />
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView5"
        android:layout_width="180dp"
        android:layout_height="320dp"
        app:cardCornerRadius="8dp"
        android:layout_margin="5dp"
        android:elevation="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black"
            android:scaleType="centerCrop"

            />
    </androidx.cardview.widget.CardView>

    <androidx.constraintlayout.helper.widget.Carousel
        android:id="@+id/carousel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:carousel_backwardTransition="@+id/backward"
        app:carousel_firstView="@+id/cardView2"
        app:carousel_forwardTransition="@+id/forward"
        app:carousel_nextState="@+id/next"
        app:carousel_previousState="@+id/previous"
        app:constraint_referenced_ids="cardView00, cardView0,cardView1,cardView2, cardView3, cardView4, cardView5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.motion.widget.MotionLayout>

Transition :

<?xml version="1.0" encoding="utf-8"?>
<MotionScene
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetStart="@id/start"
        motion:constraintSetEnd="@+id/next"
        motion:duration="1000"
        android:id="@+id/forward">

        <OnSwipe
            motion:dragDirection="dragLeft"
            motion:touchAnchorSide="left" />
    </Transition>

    <Transition
        motion:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/previous"
        android:id="@+id/backward">

        <OnSwipe
            motion:dragDirection="dragRight"
            motion:touchAnchorSide="right" />

    </Transition>

    <Transition
        motion:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/after"
        motion:duration="10000">
        <OnSwipe
            motion:touchAnchorId="@id/cardView2"
            motion:dragDirection="dragUp"
            motion:touchAnchorSide="bottom"/>
    </Transition>


    <ConstraintSet android:id="@+id/previous">
        <Constraint
            android:id="@+id/cardView00"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-150dp"
            android:elevation="0dp"
            android:alpha="1"
            />
        <Constraint
            android:id="@+id/cardView0"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-80dp"
            android:elevation="1dp"
            />
        <Constraint
            android:id="@+id/cardView1"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="0dp"
            android:elevation="2dp"
            />
        <Constraint
            android:id="@+id/cardView2"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="80dp"
            android:elevation="1dp"
            />
        <Constraint
            android:id="@+id/cardView3"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="150dp"
            android:elevation="0dp"
            />
        <Constraint
            android:id="@+id/cardView4"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="200dp"
            android:alpha="0"
            />
        <Constraint
            android:id="@+id/cardView5"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="200dp"
            android:elevation="0dp"
            android:alpha="0"
            />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/cardView00"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-200dp"
            android:elevation="0dp"
            android:alpha="0"
            />
        <Constraint
            android:id="@+id/imageView0">
            <CustomAttribute
                motion:attributeName="Saturation"
                motion:customFloatValue="0" />
        </Constraint>


        <Constraint
            android:id="@+id/cardView0"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-150dp"
            android:elevation="0dp"
            />
        <Constraint
            android:id="@+id/cardView1"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-80dp"
            android:elevation="1dp"
            />
        <Constraint
            android:id="@+id/cardView2"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:elevation="2dp"
            />
        <Constraint
            android:id="@+id/cardView3"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="80dp"
            android:elevation="1dp"
            />
        <Constraint
            android:id="@+id/cardView4"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="150dp"
            android:elevation="0dp"
            />
        <Constraint
            android:id="@+id/cardView5"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="200dp"
            android:elevation="0dp"
            android:alpha="0"
            />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/next">
        <Constraint
            android:id="@+id/cardView00"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-200dp"
            android:elevation="0dp"
            android:alpha="0"
            />
        <Constraint
            android:id="@+id/cardView0"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:alpha="0"
            android:layout_marginStart="-200dp"
            />
        <Constraint
            android:id="@+id/cardView1"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-150dp"
            />
        <Constraint
            android:id="@+id/cardView2"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-80dp"
            android:elevation="1dp"
            />
        <Constraint
            android:id="@+id/cardView3"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="0dp"
            android:elevation="2dp"
            />
        <Constraint
            android:id="@+id/cardView4"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="80dp"
            android:elevation="1dp"
            />
        <Constraint
            android:id="@+id/cardView5"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="150dp"
            android:elevation="0dp"
            android:alpha="1"
            />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/after">
        <Constraint
            android:id="@+id/cardView00"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-200dp"
            android:elevation="0dp"
            android:alpha="0"
            />
        <Constraint
            android:id="@+id/cardView0"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-150dp"
            android:elevation="0dp"
            />
        <Constraint
            android:id="@+id/cardView1"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="-80dp"
            android:elevation="1dp"
            />
        <Constraint
            android:id="@+id/cardView2"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginBottom="200dp"
            android:elevation="3dp"
            android:alpha="0"
            />
        <Constraint
            android:id="@+id/cardView3"
            motion:layout_constraintStart_toEndOf="parent"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="0dp"
            android:elevation="2dp"
            />
        <Constraint
            android:id="@+id/cardView4"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="80dp"
            android:elevation="1dp"
            />
        <Constraint
            android:id="@+id/cardView5"
            android:layout_width="180dp"
            android:layout_height="320dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="150dp"
            android:elevation="0dp"
            android:alpha="1"
            />
    </ConstraintSet>


</MotionScene>

Maijn.java

package com.example.wordpaper.ui.background;

import static android.app.Activity.RESULT_OK;

import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.helper.widget.Carousel;
import androidx.constraintlayout.motion.widget.MotionLayout;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import com.example.wordpaper.R;
import com.example.wordpaper.database.AppDatabase;
import com.example.wordpaper.database.Background;
import com.example.wordpaper.databinding.FragmentBackgroundBinding;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class BackgroundFragment extends Fragment {

    private FragmentBackgroundBinding binding;

    private AppDatabase appDatabase;

    private FloatingActionButton hamburger;
    private Button button;

    private Carousel carousel;
    private MotionLayout motionLayout;
    private HorizontalScrollView backgroundScrollView;
    private LinearLayout scrollLinearLayout;


    int images[] = {
            R.drawable.square,
            R.drawable.oval,
            R.drawable.pencil,
            R.drawable.sunset
    };
    int numImages = images.length;

    int pastels[] = {
            R.drawable.pastel_blue,
            R.drawable.pastel_green,
            R.drawable.pastel_orange,
            R.drawable.pastel_pink,
            R.drawable.pastel_purple,
            R.drawable.pastel_red
    };
    int numPastel = pastels.length;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentBackgroundBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        button = binding.buttonImage;

        carousel = binding.carousel;
        motionLayout = binding.motionLayout;


        setupCarousel();

        images[] = {
            R.drawable.square,
            R.drawable.oval,
            R.drawable.pencil
            };
            numImages = images.length;

            carousel.refresh()

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    private void setupCarousel(){
        if(carousel == null){
            return;
        }

        carousel.setAdapter(new Carousel.Adapter() {
            @Override
            public int count() {
                return numImages;
            }

            @Override
            public void populate(View view, int index) {
                if (view instanceof CardView){
                    CardView cardView = (CardView) view;
                    ImageView imageView = (ImageView) cardView.getChildAt(0);
                    imageView.setImageResource(images[index]);
                }
            }

            @Override
            public void onNewItem(int index) {

                if (button != null) {
                    if (index == carousel.getCount() - 1) {
                        button.setText("" + index);
                    }
                    if (index == 0) {
                        button.setText("" + index);
                    }
                }
            }
        });
    }
}

I tried to use recylcer.Adapter but didnt work, I tried modifying the transition and the layout but didn't work either

0 Answers0