RecyclerView从不显示

时间:2019-02-24 18:11:56

标签: android kotlin

我尝试从Internet上获取数据,完成后我更新了Recyclerview,但它从未显示。方法onDone()的吐司尺寸为result,不为空。

我的代码:

class SearchFragment : Fragment() {

    private lateinit var requestSearchPresenter: RequestSearchPresenter
    private lateinit var recyclerViewAdapter: RecyclerViewAdapter
    private var searchResult = ArrayList<ItemNewFeedCollection?>()

    private val onRequestSearchResult = object : OnRequestSearchResult {
        override fun onEmpty(result: ListNewFeedCollection) {
            recyclerViewAdapter.notifyDataSetChanged()
        }

        override fun onDone(result: ListNewFeedCollection) {
            searchResult = result.post
            Toast.makeText(requireContext(), "${searchResult.size}", Toast.LENGTH_SHORT).show()
            recyclerViewAdapter.notifyDataSetChanged()
        }

        override fun onFail(t: String) {
            recyclerViewAdapter.notifyDataSetChanged()
        }
    }

    private var recyclerViewItemClickListener = object : OnRecyclerViewItemClickListener {
        override fun onItemClick(view: View, postion: Int) {
            (requireActivity() as NewFeedActivity).apply {
                val post = searchResult
                if (post[postion]!!.date == "null" && post[postion]!!.date == post[postion]!!.description &&
                    post[postion]!!.date == post[postion]!!.full_post && post[postion]!!.date == post[postion]!!.image &&
                    post[postion]!!.date == post[postion]!!.full_post
                ) {
                } else {
                    val link = post[postion]!!.link
                    val source = post[postion]!!.source
                    val intent = Intent(requireContext(), PostActivity::class.java)
                    intent.putExtra("link", link)
                    intent.putExtra("source", source)
                    startActivity(intent)
                }
            }
        }

        override fun onLongItemClick(view: View, postion: Int) {
        }
    }

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

    private fun configRecyclerView() {
        recyclerViewAdapter = RecyclerViewAdapter(requireContext(), searchResult, recyclerViewItemClickListener)

        recycler_view.layoutManager = LinearLayoutManager(requireContext())
        recycler_view.adapter = recyclerViewAdapter
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        configRecyclerView()

        val keyWord = arguments?.getString("key_word")

        keyWord?.let {
            requestSearchPresenter = RequestSearchPresenter(onRequestSearchResult)
            requestSearchPresenter.search(it)
        }
    }
}

但是我将方法configRecyclerView()移至onDone(),它显示在:

override fun onDone(result: NewFeedCollection) {
        searchResult = result.post
        Toast.makeText(requireContext(), "${searchResult.size}", Toast.LENGTH_SHORT).show()

        configRecyclerView()

        recyclerViewAdapter.notifyDataSetChanged()
    }

1 个答案:

答案 0 :(得分:2)

fun onDone中,您要重新分配结果:searchResult = result.post 而不是您必须更新最初传递给适配器的列表的引用。这意味着您需要更改代码: 来自:

searchResult = result.post

searchResult.clear();
searchResult.addAll(result.post);