E / RecyclerView:未连接适配器,跳过布局

时间:2019-09-26 17:30:02

标签: kotlin android-recyclerview recycler-adapter

我在RecyclerView中遇到Fragment的问题。在MainActivity中,我有一个TabLayout和一个ViewPager。我想在第一个标签中显示RecyclerView,但仍然出现以下错误:

  

E / RecyclerView:未连接适配器;跳过布局

我尝试将回收站适配器初始化移至OnViewCreated,但没有结果。

这是我的片段代码:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        listOfGreetings?.add(0, GreetingModel("Greeting 1", R.drawable.ic_launcher_background))
        listOfGreetings?.add(1, GreetingModel("Greeting 2", R.drawable.greeting_1))
        listOfGreetings?.add(2, GreetingModel("Greeting 3", R.drawable.greeting_1))
        listOfGreetings?.add(3, GreetingModel("Greeting 4", R.drawable.greeting_1))
        listOfGreetings?.add(4, GreetingModel("Greeting 5", R.drawable.greeting_1))
        listOfGreetings?.add(5, GreetingModel("Greeting 6", R.drawable.greeting_1))
        listOfGreetings?.add(6, GreetingModel("Greeting 7", R.drawable.greeting_1))
        listOfGreetings?.add(7, GreetingModel("Greeting 8", R.drawable.greeting_1))


        val recyclerView = container?.findViewById<RecyclerView>(R.id.recyclerView)
        val manager = LinearLayoutManager(activity)



        recyclerView?.setHasFixedSize(true)
        recyclerView?.layoutManager = manager



        recyclerView?.adapter = greetingAdapter

        return inflater.inflate(R.layout.fragment_category, container, false)

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        greetingAdapter = GreetingAdapter(activity!!, listOfGreetings!!.toList())
    }

这是XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
             tools:context=".CategoryFragment">

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            />

</FrameLayout>

1 个答案:

答案 0 :(得分:1)

您看到此错误消息是因为?.中的安全调用recyclerView?.adapter = greetingAdapter将返回null,所以它不会被执行,因为您是在视图{{1 }},因此可以解决该问题: 创建一个具有膨胀视图值的变量,然后使用它从中获取视图并返回它,就像这样:

inflater.inflate(R.layout.fragment_category, container, false)

此外,您在代码中过度使用了安全呼叫运算符 override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { listOfGreetings?.add(0, GreetingModel("Greeting 1", R.drawable.ic_launcher_background)) listOfGreetings?.add(1, GreetingModel("Greeting 2", R.drawable.greeting_1)) listOfGreetings?.add(2, GreetingModel("Greeting 3", R.drawable.greeting_1)) listOfGreetings?.add(3, GreetingModel("Greeting 4", R.drawable.greeting_1)) listOfGreetings?.add(4, GreetingModel("Greeting 5", R.drawable.greeting_1)) listOfGreetings?.add(5, GreetingModel("Greeting 6", R.drawable.greeting_1)) listOfGreetings?.add(6, GreetingModel("Greeting 7", R.drawable.greeting_1)) listOfGreetings?.add(7, GreetingModel("Greeting 8", R.drawable.greeting_1)) //This varaible will holds a reference to the inflated view. val view = inflater.inflate(R.layout.fragment_category, container, false) //Get the recyclerView from the inflated view. val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView) val manager = LinearLayoutManager(activity) //No need to the safe call ?. here because recyclerView won't be null. recyclerView.setHasFixedSize(true) recyclerView.layoutManager = manager recyclerView.adapter = greetingAdapter return view } ?.可以变得更加简单,您可以在安全调用中将listGreetings用于对象apply()

listGreeting

这将检查如果listOfGreetings?.apply { add(0, GreetingModel("Greeting 1", R.drawable.ic_launcher_background)) add(1, GreetingModel("Greeting 2", R.drawable.greeting_1)) add(2, GreetingModel("Greeting 3", R.drawable.greeting_1)) add(3, GreetingModel("Greeting 4", R.drawable.greeting_1)) add(4, GreetingModel("Greeting 5", R.drawable.greeting_1)) add(5, GreetingModel("Greeting 6", R.drawable.greeting_1)) add(6, GreetingModel("Greeting 7", R.drawable.greeting_1)) add(7, GreetingModel("Greeting 8", R.drawable.greeting_1)) } 不等于null,将执行listGreetings块中的代码:

apply