屏幕方向改变之前,数据不会分段显示吗?

时间:2018-07-22 11:51:56

标签: android android-fragments android-recyclerview kotlin

我有一个应用程序,该应用程序在首次打开时会将API中的数据返回到片段,但是直到我更改方向再填充RecyclerView时,什么都没有显示。生命周期方法有什么用吗?如果是这样,那我应该重写哪一个?

ListAdapter

class ListAdapter @Inject constructor(private val context: Context) : RecyclerView.Adapter<ListAdapter.ViewHolder>() {

var collection: ArrayList<ListEntity> = ArrayList()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListAdapter.ViewHolder =
        ListAdapter.ViewHolder(LayoutInflater.from(context).inflate(R.layout.lists, parent, false))

override fun getItemCount(): Int {
    Timber.i("the size is ${collection.size}")
    return collection.size
}

override fun onBindViewHolder(holder: ListAdapter.ViewHolder, position: Int) {
    holder.bind(collection[position])
}

class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
    fun bind(listEntity: ListEntity) {

        val mytxt = view.findViewById<TextView>(R.id.txt)
        mytxt.text = listEntity.by

        }
    }
}

ListFragment

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

    view.list_recyclerview.layoutManager = LinearLayoutManager(activity)
    view.list_recyclerview.adapter = listAdapter
    view.list_recyclerview.setHasFixedSize(false)
    view.list_recyclerview.isNestedScrollingEnabled = true

    Timber.i("OnCreateView called")

    return view
  }

1 个答案:

答案 0 :(得分:0)

//In AndroidMainfest
     <activity
            android:name="com.Activity"
            android:configChanges="orientation|screenSize" >
        </activity>