I am developing an app for that i used carousel with motion layout for image slider but i couldn't populate the carousel. And I am unable to set the adapter for the carousel how to populate the it with images or data.
Once this basic motion scene is created, we only need to add a Carousel helper to the layout and references those views (in the same order we implemented our previous/next animation).
This is the XML file.
<?xml version="1.0" encoding="utf-8"?>
<!-- activity_main.xml -->
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layoutDescription="@xml/activity_product_view_3d_scene">
<ImageView android:id="@+id/imageView0"
android:layout_width="match_parent"
android:layout_height="400dp"
android:src="@drawable/nature2"/>
<ImageView android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="400dp"
android:src="@drawable/nature1"/>
<ImageView android:id="@+id/imageView2"
android:layout_width="match_parent"
android:src="@drawable/nature"
android:layout_height="400dp" />
<androidx.constraintlayout.helper.widget.Carousel
android:id="@+id/carousel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:carousel_forwardTransition="@+id/forward"
app:carousel_backwardTransition="@+id/backward"
app:carousel_previousState="@+id/previous"
app:carousel_nextState="@+id/next"
app:carousel_infinite="true"
app:carousel_firstView="@+id/imageView2"
app:constraint_referenced_ids="imageView0,imageView1" />
</androidx.constraintlayout.motion.widget.MotionLayout>
This is the motion scenefile.
<?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>
</MotionScene>
This is the .java file.
public class ProductView_3d extends AppCompatActivity {
Carousel carousel;
int [] images = {R.drawable.nature1, R.drawable.nature, R.drawable.nature2};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_view_3d);
carousel = findViewById(R.id.carousel);
carousel.setAdapter(new Carousel.Adapter() {
@Override
public int count() {
return images.length;
}
**@Override
public void populate(View view, int index){
}**
@Override
public void onNewItem(int index) {
}
});
}
}
I implemented from the official documentation of android link here
But I am unable to populate the carousel could anyone help. Thanks in advance.
It should be as simple as adding
((ImageView)view).setImageResource(images[index]);
to
public void populate(View view, int index){
}