Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notifyItemChanged触发调用onRefresh #311

Closed
bibubi opened this issue Dec 16, 2022 · 2 comments
Closed

notifyItemChanged触发调用onRefresh #311

bibubi opened this issue Dec 16, 2022 · 2 comments
Labels
Bug Something isn't working

Comments

@bibubi
Copy link

bibubi commented Dec 16, 2022

class Model(
    override var itemPosition: Int = 0,
    var text: String = "测试"
) : ItemPosition

class RefreshFragment : EngineFragment<FragmentRefreshBinding>(R.layout.fragment_refresh) {
    private val total = 2
    override fun initView() {
        setHasOptionsMenu(true)
        binding.rv.linear().setup {
            addType<Model>(R.layout.item_multi_type_simple)
            onBind {
                val binding = getBinding<ItemMultiTypeSimpleBinding>()
                binding.item.setOnClickListener {
                    Log.d("RefreshFragment","Click")
                    this.getModel<Model>().text = "哈哈哈"
                    bindingAdapter?.notifyItemChanged(modelPosition)
                }
            }
        }

        binding.page.onRefresh {
            Log.d("RefreshFragment","onRefresh")
            val runnable = { // 模拟网络请求, 创建假的数据集

                //上方item每次点击,都会触发onRefresh
                //在通过scope作用域里面设置binding.rv.models = getData()也会造成这样问题
                val data = getData()
                addData(data)

//                //加上index < 1控制 上方item每次点击,则触发onRefresh
//                val data = getData()
//                addData(data) {
//                    index < 1
//                }
            }
            postDelayed(runnable, 1000)
        }.autoRefresh()
    }

    private fun getData(): List<Any> {
        return mutableListOf<Any>().apply {
            for (i in 0..2) {
                add(Model())
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.menu_refresh, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu_loading -> binding.page.showLoading()  // 加载中
            R.id.menu_pull_refresh -> binding.page.autoRefresh() // 下拉刷新
            R.id.menu_refresh -> binding.page.refresh() // 静默刷新
            R.id.menu_content -> binding.page.showContent() // 加载成功
            R.id.menu_error -> binding.page.showError(force = true) // 强制加载错误
            R.id.menu_empty -> binding.page.showEmpty() // 空数据
            R.id.menu_refresh_success -> binding.page.finish() // 刷新成功
            R.id.menu_refresh_fail -> binding.page.finish(false) // 刷新失败
            R.id.menu_no_load_more -> binding.page.finishLoadMoreWithNoMoreData() // 没有更多数据
        }
        return super.onOptionsItemSelected(item)
    }

    override fun initData() {
    }
}

Snipaste_2022-12-16_11-48-12

@bibubi bibubi added the 寻求帮助 Extra attention is needed label Dec 16, 2022
@liangjingkanji
Copy link
Owner

Duplicate of #250

@liangjingkanji liangjingkanji marked this as a duplicate of #250 Dec 16, 2022
@liangjingkanji
Copy link
Owner

liangjingkanji commented Dec 16, 2022

notifyItemChanged回调onBindViewHolder导致的触发预加载导致的问题

@liangjingkanji liangjingkanji added Bug Something isn't working and removed 寻求帮助 Extra attention is needed labels Dec 16, 2022
@liangjingkanji liangjingkanji changed the title #250我也遇到了 重复调用onRefresh notifyItemChanged触发调用onRefresh Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants