viewpager内部对话框未显示

r1zhe5dt  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(464)

我有一个自定义对话框 ViewPager 在它内部,当对话框显示 ViewPager 只是空白,在刷卡或按下“下一步”按钮时没有进展。我试着稍微修改一下我的代码,但没有成功。我看到过好几个这样的帖子,但他们的解决方案都不管用。ps如果有些东西没有意义或者名称不匹配,那是因为我重命名/删除了一些文件/变量来简化。
滑块适配器:

public class SliderAdapter extends PagerAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private ArrayList<String> text;

    public SliderAdapter(Context context, ArrayList<String> text) {
        this.context = context;
        this.text = text;
    }

    public String[] txtH = {
            "test1",
            "test2",
            "test3"
    };

    @Override
    public int getCount() {
        return txtH.length;
    }

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

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.slide_layout_wr, container, false);

        TextView txt1 = view.findViewById(R.id.txt11);
        TextView txt2 = view.findViewById(R.id.txt22);

        txt1.setText(txtH[position]);
        txt2.setText(text.get(position));

        container.addView(view);

        return view;
    }

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

对话框本身:

public class DialogWeeklyReport extends AppCompatDialogFragment {

    ...

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
                R.style.Dialog);

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog, null);

        preferences = getActivity().getSharedPreferences("label", 0);
        Random random = new Random();

        text.add("test1");
        text.add("test2");
        text.add("test3");

        viewPager = view.findViewById(R.id.viewPager);
        dotLayout = view.findViewById(R.id.dotLayout);

        next = view.findViewById(R.id.next);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (next.getText().toString().equals("Proceed")) {
                    dismiss();
                } else {
                    viewPager.setCurrentItem(currentPage + 1);
                }
            }
        });

        back = view.findViewById(R.id.back);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(currentPage--);
            }
        });

        builder.setView(view)
                .setCancelable(true);

        addDotsIndicator(0);
        viewPager.setOnPageChangeListener(viewListener);
        adapter = new SliderAdapter(getActivity(), text);

        return builder.create();
    }

    private void addDotsIndicator(int position) {
        dots = new TextView[3];
        dotLayout.removeAllViews();

        for (int i = 0; i<dots.length; i++) {
            dots[i] = new TextView(getActivity());
            dots[i].setText(Html.fromHtml("&#8226"));
            dots[i].setTextSize(35);
            dots[i].setTextColor(Color.parseColor("#404040"));

            dotLayout.addView(dots[i]);
        }

        if(dots.length > 0) {
            dots[position].setTextColor(Color.BLACK);
        }

        if (currentPage == 0) {

            back.setEnabled(false);
            next.setEnabled(true);
            back.setText("");
            next.setText("Next");

        }
    }

    ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            addDotsIndicator(position);
            currentPage = position;

            if (currentPage == 0) {

                back.setEnabled(false);
                next.setEnabled(true);
                back.setText("");
                next.setText("Next");

            } else if (currentPage == 1) {

                back.setEnabled(true);
                next.setEnabled(true);
                back.setText("Back");
                next.setText("Next");

            } else if (currentPage == 2) {

                back.setEnabled(true);
                next.setEnabled(false);
                back.setText("Back");
                next.setText("Proceed");

            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };
}

对话框xml:

<RelativeLayout
    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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_bg"
    app:cardCornerRadius="20dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/dotLayout"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_centerHorizontal="true"
        android:layout_margin="15dp"
        android:layout_below="@+id/viewPager"
        android:orientation="horizontal" />

    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_below="@+id/viewPager"
        android:background="@android:color/transparent"
        android:elevation="0dp"
        android:text="Back"
        android:textColor="@android:color/black" />

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/viewPager"
        android:layout_margin="5dp"
        android:background="@android:color/transparent"
        android:elevation="0dp"
        android:text="Next"
        android:textColor="@android:color/black" />

</RelativeLayout>

viewpager的幻灯片布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <TextView
        android:id="@+id/txt11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST"
        android:textColor="@android:color/black"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.24000001" />

    <TextView
        android:id="@+id/txt22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Test"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/txt11"
        app:layout_constraintStart_toStartOf="@+id/txt11"
        app:layout_constraintTop_toBottomOf="@+id/txt11"
        app:layout_constraintVertical_bias="0.26" />

</androidx.constraintlayout.widget.ConstraintLayout>
wztqucjr

wztqucjr1#

问题是你没有设置 ViewPager 适配器

public class DialogWeeklyReport extends AppCompatDialogFragment {

    ...

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

 ...
        viewPager = view.findViewById(R.id.viewPager);
  ...
        adapter = new SliderAdapter(getActivity(), text);

        viewPager.setAdapter(adapter); // <<<<<< change here

        return builder.create();
    }
...

这是我的测试

相关问题