在片段中使用自动视图翻转器

zlwx9yxi  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(179)

我试图在HomeFragment中创建一个自动视图翻转器,但它不会自动翻转。我用过视频资源,但到目前为止运气不好。我试图在一个片段中实现这一点,但我还不太习惯片段。应用程序运行时没有任何问题,但视图翻转器只是停留在第一个图像上。我删除了视图翻转器中的imageviews,并使用了图像数组,但是没有显示任何内容。
我该怎么做?我错过了什么吗?

package com.example.coffeeinn.Home;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.example.coffeeinn.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    private ViewFlipper v_flipper;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);

            //  StatisticCarouselView = findViewById(R.id.statisticcorousal);

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        int images[] = {R.drawable.coffee_statistic, R.drawable.coffee_statistic01,
                R.drawable.coffee_statistic02, R.drawable.coffee_statistic03};

        v_flipper = rootView.findViewById(R.id.viewFlipper);
          for (int image: images){
            flipperImages(image);
        }

        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    public void flipperImages(int image) {
        ImageView imageView = new ImageView(getActivity());
        imageView.setBackgroundResource(image);

        v_flipper.startFlipping();
        v_flipper.addView(imageView);
        v_flipper.setFlipInterval(3000);
        v_flipper.setAutoStart(true);

        v_flipper.setOutAnimation(getActivity(), android.R.anim.fade_out);
        v_flipper.setInAnimation(getActivity(), android.R.anim.fade_in);

    }
}

XML代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FFFFFF"
    android:backgroundTint="#00FFFFFF"
    android:fadingEdge="none"
    android:foregroundTint="#00FFFFFF"
    tools:context=".Home.HomeFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/BGblue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/top_layouta"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/Logo"
                android:layout_width="495px"
                android:layout_height="79px"
                android:layout_marginStart="24dp"
                android:layout_marginTop="16dp"
                android:src="@drawable/coffee_inn_logo"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/BgIconMenu"
                android:layout_width="381dp"
                android:layout_height="296dp"
                android:layout_marginTop="64dp"
                android:adjustViewBounds="true"
                android:src="@drawable/bg_home"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/CoffeeIn_Info" />

            <TextView
                android:id="@+id/CoffeeIn_Info"
                android:layout_width="360dp"
                android:layout_height="46dp"
                android:layout_marginTop="30dp"
                android:text="@string/coffeein_slogan"
                android:textAlignment="center"
                android:textColor="#FFFFFF"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/Logo" />

            <ImageButton
                android:id="@+id/indonesiancoffee"
                android:layout_width="105dp"
                android:layout_height="104dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="30dp"
                android:adjustViewBounds="true"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/icon_indonesiancoffe"
                app:layout_constraintStart_toStartOf="@+id/BgIconMenu"
                app:layout_constraintTop_toTopOf="@+id/BgIconMenu" />

            <ImageButton
                android:id="@+id/PopularCoffee"
                android:layout_width="105dp"
                android:layout_height="104dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="30dp"
                android:adjustViewBounds="true"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/icon_popularcoffee"
                app:layout_constraintStart_toEndOf="@+id/indonesiancoffee"
                app:layout_constraintTop_toTopOf="@+id/BgIconMenu" />

            <ImageButton
                android:id="@+id/breewingmethod"
                android:layout_width="105dp"
                android:layout_height="104dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="30dp"
                android:adjustViewBounds="true"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/icon_brewingmethod"
                app:layout_constraintStart_toEndOf="@+id/PopularCoffee"
                app:layout_constraintTop_toTopOf="@+id/BgIconMenu" />

            <ImageButton
                android:id="@+id/coffeebeans"
                android:layout_width="105dp"
                android:layout_height="104dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="35dp"
                android:adjustViewBounds="true"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/icon_coffeebeans"
                app:layout_constraintStart_toStartOf="@+id/BgIconMenu"
                app:layout_constraintTop_toBottomOf="@+id/indonesiancoffee" />

            <ImageButton
                android:id="@+id/video"
                android:layout_width="105dp"
                android:layout_height="104dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="35dp"
                android:adjustViewBounds="true"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/icon_video"
                app:layout_constraintStart_toEndOf="@+id/coffeebeans"
                app:layout_constraintTop_toBottomOf="@+id/PopularCoffee" />

            <ImageButton
                android:id="@+id/imageButton8"
                android:layout_width="105dp"
                android:layout_height="104dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="35dp"
                android:adjustViewBounds="true"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/icon_nearestlocation"
                app:layout_constraintStart_toEndOf="@+id/video"
                app:layout_constraintTop_toBottomOf="@+id/breewingmethod" />

            <ViewFlipper
                android:id="@+id/viewFlipper"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginTop="32dp"

                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/BgIconMenu">

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">

                    <ImageView
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:scaleType="centerCrop"
                        android:src="@drawable/coffee_statistic" />

                    <ImageView
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:scaleType="centerCrop"
                        android:src="@drawable/coffee_statistic01" />

                    <ImageView
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:scaleType="centerCrop"
                        android:src="@drawable/coffee_statistic02" />

                    <ImageView
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:scaleType="centerCrop"
                        android:src="@drawable/coffee_statistic03" />
                </RelativeLayout>

      </ViewFlipper>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题