0

I implemented a viewPager inside my MotionLayout view, and created the Adapter and set the adapter. Here is the MotionLayout view:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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:id="@+id/bottom_component"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:background="@drawable/bottom_sheet_bg"
    android:clickable="true"
    android:elevation="10dp"
    android:minHeight="@dimen/_290sdp"
    app:behavior_hideable="true"
    app:behavior_peekHeight="@dimen/_124sdp"
    app:layoutDescription="@xml/bottom_sheet_scene"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <View
        android:id="@+id/slider"
        android:layout_width="50dp"
        android:layout_height="4dp"
        android:layout_marginTop="@dimen/_4sdp"
        android:background="@drawable/slider_bg"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/placeName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_18sdp"
        android:layout_marginTop="@dimen/_5sdp"
        android:layout_marginEnd="@dimen/_8sdp"
        android:fontFamily="@font/janna_bold"
        android:gravity="start|center_vertical"
        android:textColor="#464954"
        android:textSize="@dimen/_15sdp"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@id/threeDots"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/slider"
        tools:text="Irsal Gas Station" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/placeLocation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_5sdp"
        android:fontFamily="@font/janna_regular"
        android:text="Ramallah and Al Bireh - Al Irsal"
        android:textColor="#B3B3C6"
        android:textSize="@dimen/_12sdp"
        app:layout_constraintEnd_toEndOf="@id/threeDots"
        app:layout_constraintStart_toStartOf="@id/placeName"
        app:layout_constraintTop_toBottomOf="@id/placeName" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/threeDots"
        android:layout_width="@dimen/_20sdp"
        android:layout_height="@dimen/_24sdp"
        android:layout_marginEnd="@dimen/_18sdp"
        android:tint="@color/blueText"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@id/placeName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/placeName"
        app:srcCompat="@drawable/ic_three_dots" />

    <LinearLayout
        android:id="@+id/routesContainer"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_70sdp"
        android:layout_marginTop="@dimen/_7sdp"
        app:layout_constraintTop_toBottomOf="@id/placeLocation">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/routes"
            android:layout_width="match_parent"
            android:background="#EEEEEE"
            android:elevation="10dp"
            android:layout_height="match_parent" />
    </LinearLayout>

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/addedStop"
        android:layout_width="@dimen/_18sdp"
        android:layout_height="@dimen/_18sdp"
        android:visibility="invisible"
        app:layout_constraintStart_toStartOf="@id/routes"
        app:layout_constraintTop_toBottomOf="@id/routes"
        app:srcCompat="@drawable/ic_plus" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/stops"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/janna_regular"
        android:gravity="center_vertical"
        android:text="0 Stops"
        android:textColor="#464954"
        android:textSize="@dimen/_11sdp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@id/addedStop"
        app:layout_constraintStart_toEndOf="@id/addedStop"
        app:layout_constraintTop_toTopOf="@id/addedStop" />

    <LinearLayout
        android:id="@+id/actions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_9sdp"
        android:orientation="horizontal"
        android:paddingVertical="10dp"
        android:weightSum="4"
        app:layout_constraintTop_toBottomOf="@id/addedStop">

        <com.ideal.doroob.custom.ActionButton
            android:id="@+id/addStop"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:icon="@drawable/ic_addstop"
            app:title="@string/AddStop" />

        <com.ideal.doroob.custom.ActionButton
            android:id="@+id/share"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:icon="@drawable/ic_share"
            app:title="@string/Share" />

        <com.ideal.doroob.custom.ActionButton
            android:id="@+id/overview"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:icon="@drawable/ic_overview"
            app:title="@string/Overview" />

        <com.ideal.doroob.custom.ActionButton
            android:id="@+id/addToFavorite"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:icon="@drawable/ic_favorite"
            app:title="@string/Favorite" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/button_sticky"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/cancel"
            android:layout_width="0dp"
            android:layout_height="@dimen/_30sdp"
            android:layout_marginEnd="@dimen/_3sdp"
            android:layout_weight="1"
            android:background="@drawable/button_bg"
            android:fontFamily="@font/janna_regular"
            android:gravity="center"
            android:text="@string/cancel"
            android:textColor="@color/white"
            android:textSize="@dimen/_12sdp" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/startNavigation"
            android:layout_width="0dp"
            android:layout_height="@dimen/_30sdp"
            android:layout_marginStart="@dimen/_3sdp"
            android:layout_weight="2"
            android:background="@drawable/button_bg"
            android:backgroundTint="#7ED321"
            android:fontFamily="@font/janna_bold"
            android:gravity="center"
            android:text="@string/go"
            android:textColor="@color/white"
            android:textSize="@dimen/_12sdp" />
    </LinearLayout>

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

And the adapter is:

package com.ideal.doroob.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.ideal.doroob.data.RouteDetails;
import com.ideal.doroob.databinding.RouteTypeItemBinding;

import java.util.List;

public class RoutesAdapter extends PagerAdapter {

    List<RouteDetails> routeDetailsList;

    public RoutesAdapter(List<RouteDetails> routeDetails) {
        this.routeDetailsList = routeDetails;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        RouteDetails routeDetails = routeDetailsList.get(position);
        RouteTypeItemBinding binding = RouteTypeItemBinding.inflate(
                LayoutInflater.from(container.getContext()));
        binding.distance.setText(routeDetails.getDistance() + " " + routeDetails.getUnit());
        binding.time.setText(routeDetails.getHours() + ":" + routeDetails.getMinutes());
//        ViewPager.LayoutParams params = new ViewPager.LayoutParams();
//        params.width = MATCH_PARENT;
//        params.height = MATCH_PARENT;
//        binding.getRoot().setLayoutParams(params);
        container.addView(binding.getRoot(), 0);
        return binding.getRoot();
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    public void replaceRoutes(List<RouteDetails> details) {
        routeDetailsList.clear();
        routeDetailsList.addAll(details);
    }
}

The view pager is shown but the items are not shown inside the view pager. I checked the layout inspector and the views are added to the view pager, but they are not showing in the UI

I tried to add the view pager inside my activity and it worked well, so I guess the issue is in the MotionLayout. Any idea ?

Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
  • Have you checked whether the `ViewPager` is even visible on screen and not out of bounds of the screen because of any wrong constraint of `MotionLayout`? You've set a background color to `ViewPager`, can you see it on screen? – Lalit Fauzdar Jun 27 '21 at 18:17
  • I can see the viewpager on the screen, but items are not visible on screen – Khalid Taha Jun 27 '21 at 18:26

0 Answers0