小编Nir*_*han的帖子

返回到"导航架构组件"中的相同选项卡时,嵌套片段的状态将丢失

我正在探索上个月在Google I/O 2018中引入的"导航架构组件"概念.

假设我有一个带有底部导航视图和'片段'的活动来托管所有片段: -

<android.support.constraint.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.MainActivity">


        <fragment
            android:id="@+id/root_form"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:navGraph="@navigation/nav_graph"
            app:defaultNavHost="true"
            />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

我的底部导航视图中有2个动作/标签,分别显示FragmentA和FragmentB.FragmentB有一个嵌套的(子)片段FragmentC,它可以从FragmentB打开.

在我的Activity类中,我使用'setupWithNavController()'函数将底部导航视图与导航控制器链接起来.(包含在'androidx.navigation.ui'包中的Kotlin扩展函数): -

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

      bottom_navigation.setupWithNavController(findNavController(R.id.root_form))


 }
Run Code Online (Sandbox Code Playgroud)

一切都按预期工作正常.

但是当用户在FragmentC中并且返回agin到相同的选项卡时,用户导航到FragmentA然后他将只获得FragmentB而不是fragmentC.当用户返回相同的选项卡时,它是底部导航中的常见模式,它应显示他正在查看的最后一个子/嵌套片段.

我知道如果我们有自己的片段事务但是如何在"导航架构组件"域中解决此问题,我们可以处理这个问题?

navigation android bottomnavigationview android-architecture-components android-jetpack

10
推荐指数
1
解决办法
1148
查看次数

根据另一个列表的顺序对列表进行排序

我需要对Person对象列表进行排序(List<Person>,其中每个Person对象都有一些属性,例如id(unique)nameage...等)。

排序顺序基于另一个列表。该列表包含一组Person idList<String>已排序的A )。

用Kotlin或Java List<Person>按照与列表相同的顺序订购的最佳方法是什么id

例:

List Person {
(“ID1”,”PERSON1”,22,..), (“ID-2”,”PERSON2”,20,..) ), (“ID-3”,”PERSON3”,19,..),…..
}
Run Code Online (Sandbox Code Playgroud)

订购ID清单:

List of ID {(“ID2”), (“ID1”),(”ID3”)….}
Run Code Online (Sandbox Code Playgroud)

排序Person列表应为:

List PERSON {
 (“ID-2”,”PERSON 2”,20,..) ), (“ID1”,”PERSON 2”,22,..),  (“ID-3”,”PERSON 2”,19,..),…..
}
Run Code Online (Sandbox Code Playgroud)

如果Person列表包含列表中id未提及的任何id值,则这些值应位于已排序列表的末尾。


编辑:这是我目前使用Java的方式。我希望有一个比这更好的方法:

public static List<Person> getSortList(List <Person> unsortedList, List<String> orderList){

    if(unsortedList!=null && !unsortedList.isEmpty() && orderList!=null && !orderList.isEmpty()){
        List sortedList = …
Run Code Online (Sandbox Code Playgroud)

java sorting list kotlin

5
推荐指数
1
解决办法
3619
查看次数

Retrofit2使用RxJava的Observable概念处理HTTP 204(无内容响应)情况

我有一个Web服务,该服务返回一个主题的用户列表,如果该主题没有任何用户,则仅返回HTTP代码204(无内容)。

这是我对该服务的Retrofit2呼吁(在Kotlin中)

@GET("/user/{topic}")
fun getAllUserFor(@Path(value="topic",encoded=true) topic:String) :Observable<List<User>>
Run Code Online (Sandbox Code Playgroud)

我的执行是:

fun getAllUsers(topic: String, onSuccess: Consumer<List<User>>, onFail:Consumer<Throwable>){

    val api = NetworkLayer.getUserApi()

    api.getAllUserFor(topic)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(onSuccess,onFail)

}
Run Code Online (Sandbox Code Playgroud)

除非没有主题用户,否则工作正常,因为在没有用户的情况下,服务器以代码204响应,该代码作为异常接收。

我正在使用ScalarsConverterFactory和GsonConverterFactory用Retrofit2解析Json响应。

这里讨论了类似的问题但是他们的服务器仅使用http代码响应,没有任何内容!在我的情况下,当有一个或多个用户时,服务器将以Json主体的形式返回用户列表;在没有用户的情况下,响应为204,因此,我需要处理这两种情况以及其他常见的http错误。

android rx-java okhttp retrofit2

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

Kotlin 多平台移动 - 无法运行 iOS 项目 - 缺少此目标所需的一个或多个架构

我使用 Android Studio 4.1 RC3 创建 KMM(Kotlin 多平台移动)项目并尝试运行 iOS 应用程序(使用默认代码)但遇到以下错误:

错误:链接和嵌入式框架“shared.framework”缺少此目标所需的一种或多种架构:arm64。(在项目“iosApp”的目标“iosApp”中)

但是当我点击“构建”时,它就成功构建了。仅在从 Android Studio 中点击“运行”时才会发生。同时,我使用 Xcode 来运行同一个项目,并且在将其成功部署到模拟器中时没有遇到任何问题。

顺便说一句,我正在关注这个官方 KMM 指南 https://kotlinlang.org/docs/mobile/create-first-app.html

使用 Xcode 14(我尝试使用 iOS14、13.5、12.1 的模拟器)使用 Mac OS Catalina 10.15.6

mobile android ios kotlin kotlin-multiplatform

5
推荐指数
1
解决办法
1764
查看次数

MotionLayout:如何限制特定视图上的“OnSwipe”事件而不是整个屏幕上的“OnSwipe”

我有一个带有以下“layoutDescription”的motionlayout(场景文件)

    <Transition
    motion:constraintSetStart="@+id/start"
    motion:constraintSetEnd="@+id/end"
    motion:duration="1000">

    <OnSwipe
        motion:touchAnchorId="@+id/button"
        motion:touchAnchorSide="left"
        motion:dragDirection="dragLeft" />

</Transition>
<ConstraintSet android:id="@+id/start">
    <!--    ConstraintSet for starting -->
</ConstraintSet>

<ConstraintSet android:id="@+id/end">
    <!--    ConstraintSet for end -->
</ConstraintSet>
Run Code Online (Sandbox Code Playgroud)

我从这里期望的是,如果用户将“按钮”拖向左侧,那么它应该执行转换,但实际发生的是当用户从屏幕中的任何位置向左拖动(向左滑动)时,执行此转换!

如何仅在拖动指定视图时限制动画工作?

(我使用 "constraintlayout:2.0.0-beta2" )

android android-animation android-motionlayout

4
推荐指数
1
解决办法
2292
查看次数