简体   繁体   English

Android:每 10 秒动态切换布局

[英]Android :switch between Layouts dynamically every 10 seconds

android beginner here.安卓初学者在这里。 any help is much appreciated .任何帮助深表感谢 。

I am working on an app that can do a simple experiment.我正在开发一个可以进行简单实验的应用程序。 the app displays images and asks users to rate it from 1 to 10. I have 1 activity and two layouts experiment begins.该应用程序显示图像并要求用户从 1 到 10 对其进行评分。我有 1 个活动和两个布局实验开始。 I have two layouts merged under FrameLayout.我在 FrameLayout 下合并了两个布局。 what I want to achieve is : ' ====>start experiment >show first image for 10 seconds >change layout to ratings layout>after user selects rating>loopback with different image>finish() when imagelist is empty.我想要实现的是:' ====>开始实验>显示第一张图片10秒>将布局更改为评级布局>用户选择评级后>使用不同的图片环回>当imagelist为空时完成()。 ' here is what I tried I have tried viewflipper but shownext() and showprevious() methods dont update values. ' 这是我尝试过的,我尝试过 viewflipper,但 shownext() 和 showprevious() 方法不更新值。 now i am trying using layout visibility ,showing and hiding the layouts现在我正在尝试使用布局可见性,显示和隐藏布局

class Presenter : AppCompatActivity() {
    //val currentTime = Calendar.getInstance().time
    private lateinit var binding: ActivityPresenterBinding
    lateinit var layout1:View
    lateinit var layout2:View
    val imgList=Constants.getImages()


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    //setContentView(R.layout.activity_presenter)

    binding = ActivityPresenterBinding.inflate(layoutInflater)
    setContentView(binding.root)

    layout1 = binding.imageView
    layout2=binding.ratingView
    
    startSlider()
    
}

private fun startSlider() {
    Handler(Looper.getMainLooper()).apply {
        //arr.shuffle()
        var index = 0
        var imageView = binding.imgPresenter
        val runnable = object : Runnable {
            override fun run() {
                imageView.setImageResource(imgList[index].image)
                layout1.visibility= View.VISIBLE
                Log.i("number ","${index}")
                postDelayed(this, 5000)
                index++
                layout1.visibility=View.GONE
                layout2.visibility=View.VISIBLE
                binding.sbSeekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
                    override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {

                        Toast.makeText(applicationContext, "$p1", Toast.LENGTH_LONG).show()
                        //save rating (1-10)

                    }

                    override fun onStartTrackingTouch(p0: SeekBar?) {

                    }

                    override fun onStopTrackingTouch(p0: SeekBar?) {

                    }

                })

            }


        }

        postDelayed(runnable, 1000)
    }
}

here my layout file这是我的布局文件

<FrameLayout android:id="@+id/viewFliper"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img_presenter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ic_bg"
        />

</LinearLayout>

<LinearLayout 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/ratingView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="how do you feel about this image from one to 10"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_marginBottom="20dp"
        />

    <SeekBar
        android:id="@+id/sb_seekbar"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:paddingStart="6dp"
        android:paddingEnd="6dp"
        android:progress="3"
        android:stepSize="1" />


</LinearLayout>
this is not hiding the views after the second image what am I doing wrong? 这不是在第二张图片之后隐藏视图我做错了什么?

try this and apply viewbinding试试这个并应用视图绑定

    lateinit var  runnable: Runnable

private fun startSlider() {
    Handler(Looper.getMainLooper()).apply {

        var flag = 0
        var index = 0


        runnable = Runnable {
            if (flag == 0) {

                img_presenter.setImageResource(imgList[index])
                layout1.visibility = View.VISIBLE
                layout2.visibility = View.GONE

                postDelayed(runnable, 5000)

                flag = 1

                index++
            } else {

                layout1.visibility = View.GONE
                layout2.visibility = View.VISIBLE

                sb_seekbar.setOnSeekBarChangeListener(object :
                    SeekBar.OnSeekBarChangeListener {
                    override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {

                        //                                Toast.makeText(applicationContext, "$p1", Toast.LENGTH_LONG).show()
                        //save rating (1-10)

                    }

                    override fun onStartTrackingTouch(p0: SeekBar?) {


                    }

                    override fun onStopTrackingTouch(p0: SeekBar?) {
                        index++

                        flag = 0

                        postDelayed(runnable, 1000)
                    }
                })
            }
        }

        postDelayed(runnable, 1000)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM