从适配器清除数据时,scroolListener从recyclerView分离

时间:2018-07-04 11:28:03

标签: android android-recyclerview kotlin onscrolllistener

在Kotlin中,我将滚动侦听器附加到片段的onViewCreated()中的recyclerview上。 我先加载20条记录,然后加载20条记录,依此类推...,当我出于搜索目的清除适配器数据并重新加载搜索到的数据时,滚动侦听器将无法正常工作。 我在加载更多功能时会遇到此问题。

在清除适配器后,我通过重新连接滚动侦听器来解决此问题。

为什么在清除适配器后分离滚动侦听器?

下面是我的片段

class Fragment: MvpFragment<View, PresenterImpl>(), View{

private lateinit var presenterImpl: PresenterImpl
private lateinit var myListAdapter: MyListAdapter
private var OFFSET: Long = 0
private var layoutManager: LinearLayoutManager? = null

private var mSearchTextWatcher: TextWatcher = object : EditextTextWatcher() {
    override fun afterTextChanged(editable: Editable) {
        if (TextUtils.isEmpty(editable)) {
            addScrool()
            myListAdapter.clear()
            loadData()

        } else {
            recyclerView.clearOnScrollListeners()

        }
    }
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

}

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

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

    recyclerView!!.setHasFixedSize(true)
    addData!!.visibility = View.GONE
    layoutManager = LinearLayoutManager(activity)
    recyclerView!!.layoutManager = layoutManager

    searchData!!.addTextChangedListener(mSearchTextWatcher)

    myListAdapter= MyListAdapter(context!!, mutableListOf<Bean>(), this)
    recyclerView!!.adapter = myListAdapter

    idBtSearch.setOnClickListener {
        searchClick()
    }

    addScrool()

}

private fun addScrool() {

    recyclerView.addOnScrollListener(object : RecyclerViewScrollListener() {
        override fun onScrollUp() {

        }

        override fun onScrollDown() {

        }

        override fun onLoadMore(totalItemsCount: Int) {
            myListAdapter.setLoaded(true)
            myListAdapter.notifyItemInserted(myListAdapter.getListSize() - 1)
            myListAdapter.notifyDataSetChanged()

            OFFSET = totalItemsCount.toLong()

            Handler().postDelayed({
                if (search_name != null && search_name.text.toString().isEmpty()) {
                    if (idSortedOption.text == getString(R.string.name)) {
                        presenterImpl.loadDataByName(context!!, OFFSET, LIMIT)
                    } else {
                        presenterImpl.loadDataByDate(context!!, OFFSET, LIMIT)
                    }
                }
            }, 1000)

        }
    })
}

override fun onResume() {
    super.onResume()
    if (TextUtils.isEmpty(search_patient!!.text.toString())) {
        loadData()
    } else {
        searchClick()
    }
    OFFSET = 0
}

private fun searchClick() {
    val searchText = search_name!!.text.toString()
    if (searchText.length < 3) {
        Aarogya.showToastShort(context, getString(R.string.pls_enter_at_least_3_char))
    } else {
        Aarogya.hideKeyboard(activity!!)
        presenterImpl.searchData(context!!, search_name!!.text.toString().trim { it <= ' ' }, 1L, 20L)
    }
}

override fun createPresenter(): PresenterImpl {
    presenterImpl = PresenterImpl(this)
    return presenterImpl
}

override fun showData(list: MutableList<Bean>) {
    myListAdapter.setLoaded(false)

    if (OFFSET == 0L) {
        myListAdapter.run {
            clear()
            addAll(list)
        }
    } else {
        myListAdapter.addAll(list)
    }
}

override fun showSearchData(beanList: MutableList<Bean>) {
    myListAdapter.setLoaded(false)
    myListAdapter.clear()
    myListAdapter.addAll(beanList)

}

private fun loadData() {
    presenterImpl.loadDataByName(context!!, 0L, LIMIT)
}

companion object {
    private val LIMIT = 20L

    fun newInstance(): Fragment {
        return Fragment()
    }
}
}

0 个答案:

没有答案