Смущен во время надувания представления в пейджере - PullRequest
1 голос
/ 10 июня 2019

Я пытаюсь разработать базовый слайдер imageView, используя простой View Pager & Pager Adapter.

Итак, сначала я добавляю пейджер просмотра в свой MainActivity. Я пытаюсь разработать видовой пейджер с простым макетом, содержащим два изображения.

Итак, вот мой image_view_simple.xml файл:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/imageViewActionThumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:elevation="100dp"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:srcCompat="@drawable/ic_launcher_background" />

</android.support.constraint.ConstraintLayout>

А в моем PagerAdapter я делаю что-то вроде этого:

public class MyPager extends PagerAdapter {
    private Context context;
    private ArrayList<String> imageList;

    MyPager(Context context, ArrayList<String> imageList) {
        this.context = context;
        this.imageList = imageList;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ConstraintLayout view = (ConstraintLayout) LayoutInflater.from(context).inflate(R.layout.image_view_sample, container, false);
        ImageView imageView = view.findViewById(R.id.image);
        ImageView imageViewActionThumbnail = view.findViewById(R.id.imageViewActionThumbnail);
        if (position == 0) {
            imageViewActionThumbnail.setVisibility(View.VISIBLE);
        }
        GlideApp.with(context)
                .load(imageList.get(position))
                .into(imageView);

        container.addView(view);
        return view;
    }

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

    @Override
    public int getCount() {
        return imageList.size();
    }

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

}

Но в вышеприведенном случае вывод не показывает imageViewActionThumbnail на первом слайде, как я сделал visibility=true в адаптере (я даже пытался сделать его видимым на всех слайдах по умолчанию из XML, но это не так появляется!).

enter image description here

Итак, я создал один файл temp.xml, который имеет только TextView как один дочерний элемент, и добавил код для его раздувания и прикрепил его к просмотру. как это

// Same code as above, just add the below two lines inside instantiateItem() method.

View view1 = LayoutInflater.from(context).inflate(R.layout.temp, view, false);
view.addView(view1);

container.addView(view);
return view;

И вывод показывает imageView (в верхнем левом углу) на каждом слайде, как показано на рисунке ниже. enter image description here

Я хочу знать:

1: Почему вид изображения не отображается в первом случае? Зачем мне для этого раздувать другой макет и добавлять его к родителю?

2: Что мне делать, если я хочу отобразить оба imageView без надувания какого-либо другого макета.

...