小编Mah*_*iya的帖子

在kotlin协程中取消后如何重新启动作业?

job取消后如何重新启动kotlin coroutines

我有 2 个按钮,1 个用于启动协程,另一个用于取消作业。但是在我取消作业后,协程不会再次启动。

class TestFragment : Fragment(), CoroutineScope {

    private lateinit var job: Job
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = SettingFragmentBinding.inflate(inflater, container, false)

        job = Job()

        button1.setOnClickListener {
            launch {
                val currentTime = LocalDateTime.now()
                println(currentTime)
            }
        }

        button2.setOnClickListener {
            job.cancel()
        }

        return binding.root
    }

}
Run Code Online (Sandbox Code Playgroud)

kotlin kotlin-coroutines

8
推荐指数
2
解决办法
4221
查看次数

页面必须填满整个ViewPager2(使用match_parent)

我有一个项目布局,在其中显示图像,产品名称和产品图像。我必须使用约束布局以1:1.5的比例显示图像。但是,当我加载小图像时,下面的文本不显示。

以下是我的XML项目代码:-

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

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

        <com.jackandphantom.circularimageview.RoundedImage
            android:id="@+id/imageViewSlider"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toTopOf="@id/tvTitle"
            app:layout_constraintDimensionRatio="WH,1:1.4"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:rounded_radius="0"
            tools:src="@tools:sample/avatars" />

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="Fitted crew neck sweater"
            android:textColor="@color/colorBlack"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/imageViewSlider" />

        <TextView
            android:id="@+id/tvPrice"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="$34.00"
            android:textColor="@color/colorBlack"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvTitle" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
  1. 输出长图:-https : //i.imgur.com/QVnljX6.png
  2. 输出小图像:-https : //i.imgur.com/0ZwkVwE.png

而且,如果我用wrap_content替换match_parent,则应用程序崩溃并显示以下错误:-

java.lang.IllegalStateException:页面必须填充整个ViewPager2(使用match_parent)

android android-viewpager2

4
推荐指数
7
解决办法
620
查看次数

基于 kotlin 中的另一个 ArrayList 对 ArrayList 进行排序

String我有以下清单:-

val a = listOf("G", "F", "E", "D", "C", "B", "A")
Run Code Online (Sandbox Code Playgroud)

我将从服务器获取另一个列表。例如:-

val b = listOf("A", "G", "C")
Run Code Online (Sandbox Code Playgroud)

来自服务器的列表可能包含更少或更多元素,但不会包含除第一个列表之外的元素。

所以,排序后输出应该是这样的

// G、C、A

sorting list kotlin

2
推荐指数
2
解决办法
3855
查看次数