片段中奇怪的RecyclerView滞后现象。

问题描述 投票:1回答:1

我的Fragment在执行异步任务后,遇到了一个奇怪的RecyclerView滞后现象。它在我的网页请求完成之前冻结了几毫秒。我觉得这个问题可能与某种ui线程阻塞有关,但我不知道应该在我的代码中修改什么才能让它工作。下面的代码在Activity中可以正常工作我只有在我的Fragment中使用它时才会出现这个问题。

这是我的Fragment。

class MyFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.my_fragment, container, false)
        setup(view)
        return view
    }

    private fun setup(view: View) {
        val myAdapter = MyAdapter(
            SharedData.dataViewModel,
            object :
                MyAdapter.Callback {
                //...
            })
        view.recyclerView.adapter = myAdapter
        val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(activity)
        view.recyclerView.layoutManager = layoutManager

        view.progressBarLayout.visibility = View.VISIBLE
        SharedData.dataViewModel.retrieve { success, error ->
            view.progressBarLayout.visibility = View.INVISIBLE
            if (success) {
                view.recyclerView.adapter?.notifyDataSetChanged()
            } else {
                error?.let {
                    Toast.makeText(
                        requireContext(), it.localizedMessage,
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
        }
    }
}

Inside retrieve 我得到的方法。

Webservice.shared.getData()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.io())
    .subscribe ({ result ->
        //...
    }, { error ->
        //...    
    })

And getData 是一个简单的 retrofit2 GET 请求。

@GET("get_data")
    fun getData(): Observable<DataModelResult>

编辑

这是我的xml。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@android:color/white"
    android:orientation="vertical"
    tools:context=".screens.sample.fragment.MyFragment">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/_14sdp"
            android:paddingRight="@dimen/_14sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_marginBottom="@dimen/_10sdp">
            <TextView
                android:id="@+id/topTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/sample_text"
                android:lineSpacingMultiplier="1.2"
                android:textColor="@android:color/black"
                android:textSize="@dimen/_17ssp"
                android:gravity="center_horizontal" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="@dimen/_14sdp"
            android:paddingRight="@dimen/_14sdp"
            android:weightSum="1">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                tools:listitem="@layout/item_layout_child"
                android:layout_weight="1"/>

            <Button
                android:id="@+id/nextButton"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:background="@drawable/button_rounded"
                android:backgroundTint="@color/CustomColor"
                android:layout_marginLeft="32dp"
                android:layout_marginRight="32dp"
                android:layout_marginTop="14dp"
                android:layout_marginBottom="22dp"
                android:textAllCaps="true"
                android:textColor="@drawable/button_text_color"
                android:textSize="@dimen/_16ssp"
                style="?android:attr/borderlessButtonStyle"
                android:elevation="5dp"
                android:translationZ="1dp" />
        </LinearLayout>

    </LinearLayout>

    <include
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/indicator_layout" />

</FrameLayout>
android kotlin android-recyclerview retrofit2
1个回答
0
投票

尝试启动视图(调用 setup 在这种情况下),因为过载的 onViewCreated 方法而不是 onCreateView 方法。希望能解决滞后的问题。


0
投票

在你的情况下,这取决于你在你的 success 因为你在你的retrieve方法中观察到了这个回调。AndroidSchedulers.mainThread() 如果你在做一些繁重的计算或在本地保存数据等,就会产生你所说的滞后现象,试着在后台线程上观察和解析结果,只在主线程上更新UI。


0
投票

最后,我想我已经找到了导致我的问题的原因,我在导航图中设置了一些动画,很可能是它们导致了某种线程锁。把代码放在后台线程上对我一点帮助都没有。所以在这种情况下,我想到了2种可能的解决方案。

  1. 以编程方式设置你的动画(不幸的是,我还没有找到一种为我的xml动画添加监听器的方法)并添加监听器。之后,当过渡动画完成后,你可以很容易地开始做你的异步任务。比如说 https:/stackoverflow.coma463623507110268

  2. 如果你有很多不同的动画,或者你想在动画结束前启动async任务,你也可以把你的 completion 归附 Handler(). 这个办法肯定不是最好的但如果你只需要快速修复,你可以试试。

Handler().postDelayed({
    completion(true, null)
}, context.resources.getInteger(R.integer.fragment_animation_length).toLong())
© www.soinside.com 2019 - 2024. All rights reserved.