我将 CustomScrollView 与 SliverGrid 一起使用。我使用以下命令在网格项之间添加空间:mainAxisSpacing:8,crossAxisSpacing:8。但是是否可以在第一列之前和最后一列之后添加空间(边距/填充)。我尝试将 SliverGrid 包裹在 Padding 中,但出现错误:
RenderViewport 期望有一个 RenderSliver 类型的子代,但收到了一个 RenderPadding 类型的子代。
渲染对象需要特定类型的子对象,因为它们在布局和绘制期间与其子对象进行协调。例如,RenderSliver 不能是 RenderBox 的子级,因为 RenderSliver 不理解
请帮助我如何添加 GridView 的开始和结束边距。
我需要编写一个方法来确定设备是平板电脑还是手机。我不需要根据此显示不同的用户界面。我只需要有关设备的信息,以便将来我可以将其发送到指标。
在网上,我找到了很多判断设备是否是平板电脑的方法。我已经测试了所有这些方法并且它们有效。当然,我无法在所有类型的设备上进行测试。所以我想知道确定设备是否是平板电脑的最佳且最准确的方法。
这是我能找到的方法的列表:
1) 使用最小宽度限定符
在res/values-sw600dp/attrs.xml:
<resources>
<bool name="isTablet">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)
在res/values/attrs.xml
<resources>
<bool name="isTablet">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后:
fun isTablet() = context.resources.getBoolean(R.bool.isTablet)
Run Code Online (Sandbox Code Playgroud)
我对这种方式有顾虑。可能值得添加res/values-sw720dp/attrs.xml以下资源:
<resources>
<bool name="isTablet">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)
2) 使用 TelephonyManager
fun isTablet(context: Context) =
with(context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager) {
this.phoneType == TelephonyManager.PHONE_TYPE_NONE
}
Run Code Online (Sandbox Code Playgroud)
3) 使用 Configuration:
fun isTablet(context: Context): Boolean {
return ((context.resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE)
}
Run Code Online (Sandbox Code Playgroud)
4) 使用 DisplayMetrics:
fun isTablet(activity: Activity): Boolean {
val metrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(metrics)
val yInches = metrics.heightPixels / …Run Code Online (Sandbox Code Playgroud) 是否可以创建带有参数的枚举。例如,在 Kotlin 中我可以这样做:
enum class TestType(val testText: String, val number: Int) {
STATIC("", 0),
DYNAMIC("", 1)
}
Run Code Online (Sandbox Code Playgroud)
每个枚举项包含 testText 和 number。是否可以用 Dart 编程语言来实现这一点。请帮我。
我用于paging3在 中显示无尽的列表RecyclerView。当我单击特定项目时,我需要显示详细信息片段。详细信息片段包含一个视图分页器,其适配器也是PagingDataAdapter。也就是说,具有详细信息的片段应该显示我在上一屏幕上选择的元素,并且还可以水平滚动到也逐页加载的其他元素。问题是我无法从某个位置开始在视图寻呼机中显示分页列表。该列表始终从头开始显示。我找不到这个问题的解决方案。是否可以从特定位置开始监听页面源(RemoteMediator)的数据?请帮我:(
android android-view android-recyclerview android-viewpager2 android-paging-3
我正在使用MVVM干净的架构编写应用程序。在其中一个屏幕上,我需要使用 来RecyclerView实现pagination。我要使用图书馆Paging3。
Android 开发者建议在存储库层使用PagingSource和。RemoteMediator但同时,在许多来源中,我读到数据层和领域层不应该了解有关android框架的任何信息。
但现在我必须在数据层的数据源中使用android库。这在 a 的上下文中正确吗clean architecture?
请帮我弄清楚,我不明白如何使用干净的架构实现分页。
android android-recyclerview clean-architecture android-mvvm android-paging-3
我正在尝试使图像的圆角。这是我的代码:
ClipRRect(
borderRadius: BorderRadius.circular(14),
child: Image.asset(
"assets/images/test.png"
))
Run Code Online (Sandbox Code Playgroud)
一切正常,但是当我尝试将图像放入具有固定高度和宽度的容器中时,圆形边框停止工作。这是我的代码:
LimitedBox(
maxWidth: 95,
maxHeight: 95,
child: ClipRRect(
borderRadius: BorderRadius.circular(14),
child: Image.asset(
"assets/images/test.png"
),
),
)
Run Code Online (Sandbox Code Playgroud)
为什么会出现这种情况,请帮帮我。
我需要使用 来创建链ConstraintLayout。我希望将 TextView 附加到左侧,文本后紧跟一个ImageView,另一个ImageView附加到屏幕的右侧。看一个例子。
如果TextView包含长文本,我需要将文本转到另一行。也就是说,TextView不与右侧的图像视图重叠,但仅限于边距。看一个例子。
这是我的代码:
<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:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingPrefix">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"
android:textColor="@color/black"
android:textSize="@dimen/text_size_14"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0" />
<ImageView
android:id="@+id/ivFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp"
android:src="@drawable/ic_first"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/ivSecond"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toEndOf="@id/tvTitle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0" />
<ImageView
android:id="@+id/ivSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_second"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
如果文本不长,则一切正常,但如果文本很长,则它会叠加在屏幕上ImageView并超出屏幕。我尝试使用链条但没有任何效果。请帮我。
android margins chain android-layout android-constraintlayout
我有一份清单Lessons。这是我的Lessons课程:
data class Lessons(
val id: Long,
val name: String,
val time: Long,
val key: String
)
Run Code Online (Sandbox Code Playgroud)
我需要将元素移动到列表的开头,其key字段的值为“priority”。这是我的代码:
val priorityLesson = lessons.find { it.key == "priority" }
if (priorityLesson != null) {
lessons.remove(priorityLesson)
lessons.add(0, priorityLesson)
}
Run Code Online (Sandbox Code Playgroud)
一切正常,但我不喜欢这个解决方案,也许有一种更有效的方法来执行这个算法。此外,我需要将列表转换为可变的,并且我想将其保留为不可变的。
请帮我。
我正在用来com.google.accompanist:accompanist-pager实现页面的无限滚动。我按照 HorizontalPagerLoopingSample 中的描述实现了所有内容。我需要从第三页开始显示我的寻呼机。但是当我设置initialPage = 2 HorizontalPager 显示错误的页面。在示例中设置显示第一页initalPage = Int.MAX_VALUE / 2。是否可以用无限寻呼机计算真实的具体位置?
我尝试做这样的事情:
val positionFromIWantToStart = 2
val startIndex = (Int.MAX_VALUE / 2) + positionFromIWantToStart
val pagerState = rememberPagerState(initialPage = startIndex)
Run Code Online (Sandbox Code Playgroud)
但这工作不正确,HorizontalPager总是显示第一页。
请帮我。
android infinite-scroll android-viewpager android-jetpack-compose jetpack-compose-accompanist
我需要对我的列表进行一些操作。
例如我有以下列表TestData:
data class TestData (
val value: Int?,
val name: String
)
Run Code Online (Sandbox Code Playgroud)
我需要将列表映射TestData到列表String。这是我的代码:
val names = listOfTestData
.map { data -> getName(data.value) } <- Type mismatch. Required: Int, found Int?
.distinct()
Run Code Online (Sandbox Code Playgroud)
问题是该函数getName(value: Int)仅接受不可为 null 的类型。我可以以某种方式跳过 null 的元素listOfTestData吗value?
我可以在制作地图之前过滤这些值,但我必须在地图内部使用!!,我想要一个更优雅的解决方案。
val names = listOfTestData
.filter { it.value != null }
.map { data -> getName(data.value!!) }
.distinct()
Run Code Online (Sandbox Code Playgroud)
请告诉我如何在不使用的情况下完成此操作!!
collections arraylist kotlin kotlin-extension kotlin-null-safety
android ×5
flutter ×3
arraylist ×2
collections ×2
kotlin ×2
algorithm ×1
android-mvvm ×1
android-view ×1
chain ×1
dart ×1
data-class ×1
enums ×1
jetpack-compose-accompanist ×1
margins ×1