异步任务完成后,我在Fragment中面临怪异的RecyclerView滞后。在我的Web请求完成之前,它冻结了几毫秒。我觉得这个问题可能与某种ui线程阻塞有关,但是我无法弄清楚应该在我的代码中进行哪些更改才能使其正常工作。 以下代码在Activity 中可以正常工作,仅当我在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()
                }
            }
        }
    }
}


retrieve方法内部,我得到了:
Webservice.shared.getData()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.io())
    .subscribe ({ result ->
        //...
    }, { error ->
        //...
    })

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>

最佳答案

尝试从重载setup方法而不是onViewCreated方法初始化 View (在这种情况下,调用onCreateView)。希望它将解决滞后问题。

关于android - fragment 中奇怪的RecyclerView滞后,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61475268/

10-11 00:10