在recyclerview的onbindviewholder中显示进度条

t2a7ltrp  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(476)

我想在加载recyclerview中的元素(大约要填充150个元素)时显示活动中的progressbar(在subitemadapter.kt中),但现在这个progressbar根本不显示。这是我的密码:
currencylistfragment.kt

  1. class CurrencyListFragment : Fragment(), MainContract.View {
  2. companion object {
  3. private val TAG = CurrencyListFragment::class.qualifiedName
  4. }
  5. private val restModel: RestModel = RestModel()
  6. private val handler: Handler = Handler(Looper.getMainLooper())
  7. private lateinit var mainPresenter: MainPresenter
  8. private lateinit var itemAdapter: ItemAdapter
  9. private lateinit var _layoutManager: LinearLayoutManager
  10. private lateinit var onChangeFragment: OnChangeFragment
  11. private lateinit var currentDate: String
  12. private var isLoading: Boolean = false
  13. private var apiResponseList: MutableList<ApiResponse> = arrayListOf()
  14. private var listSize: Int = 0
  15. override fun onAttach(context: Context) {
  16. super.onAttach(context)
  17. try {
  18. if (activity is OnChangeFragment) onChangeFragment = activity as OnChangeFragment
  19. } catch (error: ClassCastException) {
  20. error.message?.let { Log.e(TAG, it) }
  21. }
  22. }
  23. // @formatter:off
  24. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  25. return inflater.inflate(R.layout.currency_list_fragment, container, false)
  26. }
  27. // @formatter:on
  28. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  29. super.onViewCreated(view, savedInstanceState)
  30. _layoutManager = LinearLayoutManager(activity)
  31. mainPresenter = MainPresenter(this, restModel, SharedPreferencesModel(activity as Activity))
  32. currentDate = mainPresenter.convertCurrentDate()
  33. if (mainPresenter.checkIfSuchDateExistsinSp(currentDate)) {
  34. Log.i(TAG, "Date $currentDate already exists in SharedPreferences")
  35. mainPresenter.processDateWithoutMakingACall(currentDate)
  36. } else {
  37. mainPresenter.makeACall(currentDate)
  38. Log.i(TAG, "Date $currentDate does not exist in SharedPreferences. Retrofit call made")
  39. }
  40. mainPresenter.saveNumberOfMinusDaysIntoSp(0)
  41. addScrollerListener()
  42. }
  43. override fun showProgressBarOnLoadingCurrencies() {
  44. progress_bar.visibility = View.VISIBLE
  45. }
  46. override fun hideProgressBarOnFinishedLoadingCurrencies() {
  47. progress_bar.visibility = View.GONE
  48. }
  49. override fun setRecyclerViewStateToLoading() {
  50. if (apiResponseList.size > 0) {
  51. apiResponseList.add(ApiResponse("", "", listOf(Currency("", 0f)), true))
  52. itemAdapter.notifyItemInserted(apiResponseList.size - 1)
  53. }
  54. }
  55. override fun removeRecyclerViewStetOfLoading() {
  56. if (apiResponseList.size > 1) {
  57. apiResponseList.removeAt(apiResponseList.size - 1)
  58. listSize = apiResponseList.size
  59. itemAdapter.notifyItemRemoved(listSize)
  60. }
  61. isLoading = false
  62. }
  63. override fun getApiResponseList(): List<ApiResponse> {
  64. return apiResponseList
  65. }
  66. override fun showLogAboutExistingDateInSp(date: String) {
  67. Log.i(TAG, "Date $date already exists in SharedPreferences (new element)")
  68. }
  69. override fun showLogAboutNotExistingDateInSp(date: String) {
  70. Log.i(TAG, "Date $date does not exist in SharedPreferences. Retrofit call made (new element)")
  71. }
  72. override fun assignResponseToRecyclerview(apiResponse: ApiResponse?) {
  73. rv_item.apply {
  74. layoutManager = _layoutManager
  75. apiResponseList.add(apiResponse!!)
  76. itemAdapter = activity?.let { ItemAdapter(apiResponseList, it) }!!
  77. adapter = itemAdapter
  78. }
  79. }
  80. private fun addScrollerListener() {
  81. rv_item.addOnScrollListener(object : RecyclerView.OnScrollListener() {
  82. override fun onScrollStateChanged(rvItem: RecyclerView, newState: Int) {
  83. super.onScrollStateChanged(rvItem, newState)
  84. mainPresenter.processRvitemOnScroll(isLoading, rvItem, newState)
  85. }
  86. })
  87. }
  88. private fun loadMore() {
  89. setRecyclerViewStateToLoading()
  90. var numberOfDays = mainPresenter.getNumberOfMinusDays()
  91. numberOfDays++
  92. mainPresenter.saveNumberOfMinusDaysIntoSp(numberOfDays)
  93. val dateMinusXDays = mainPresenter.currentDateMinusXDaysToStr(numberOfDays)
  94. val nextLimit = listSize + 1
  95. for (i in listSize until nextLimit) {
  96. if (mainPresenter.checkIfSuchDateExistsinSp(dateMinusXDays)) {
  97. Log.i(TAG, "Date $dateMinusXDays already exists in SharedPreferences (new element)")
  98. handler.postDelayed({
  99. mainPresenter.processDateWithoutMakingACall(dateMinusXDays)
  100. }, 2000)
  101. } else {
  102. Log.i(TAG, "Date $dateMinusXDays does not exist in SharedPreferences. Retrofit call made (new element)")
  103. mainPresenter.makeACall(dateMinusXDays)
  104. }
  105. }
  106. itemAdapter.notifyDataSetChanged()
  107. }
  108. override fun notifyChangedItemAdapter() {
  109. itemAdapter.notifyDataSetChanged()
  110. }
  111. override fun onDestroy() {
  112. super.onDestroy()
  113. restModel.cancelJob()
  114. }
  115. }

itemadapter.kt

  1. class ItemAdapter(private var items: MutableList<ApiResponse>, private val activity: Activity) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
  2. companion object {
  3. private const val VIEW_TYPE_DATA = 0
  4. private const val VIEW_TYPE_PROGRESS = 1
  5. }
  6. override fun onCreateViewHolder(parent: ViewGroup, p1: Int): RecyclerView.ViewHolder {
  7. return when (p1) {
  8. VIEW_TYPE_DATA -> {
  9. val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
  10. DataViewHolder(view, activity)
  11. }
  12. VIEW_TYPE_PROGRESS -> {
  13. val view = LayoutInflater.from(parent.context).inflate(R.layout.progress_bar_layout, parent, false)
  14. ProgressViewHolder(view)
  15. }
  16. else -> throw IllegalArgumentException("Different View type")
  17. }
  18. }
  19. override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
  20. if (holder is DataViewHolder)
  21. holder.bind(items[position])
  22. }
  23. override fun getItemCount() = items.size
  24. override fun getItemViewType(position: Int): Int {
  25. val viewtype = items[position]
  26. return when (viewtype.isLoading) {//if data is load, returns PROGRESSBAR viewtype.
  27. true -> VIEW_TYPE_PROGRESS
  28. false -> VIEW_TYPE_DATA
  29. }
  30. }
  31. class DataViewHolder(view: View, activity: Activity) : RecyclerView.ViewHolder(view) {
  32. private var isRvSubitemVisible = false
  33. private val tvDate = view.tv_date
  34. private val rvSubitem = view.rv_subitem
  35. private val activity = activity
  36. fun bind(apiResponse: ApiResponse) {
  37. tvDate.text = String.format(itemView.context.getString(R.string.day_x), apiResponse.date)
  38. tvDate.setOnClickListener {
  39. if (isRvSubitemVisible) {
  40. rvSubitem.visibility = View.GONE
  41. isRvSubitemVisible = false
  42. } else {
  43. rvSubitem.visibility = View.VISIBLE
  44. isRvSubitemVisible = true
  45. }
  46. }
  47. rvSubitem.apply {
  48. layoutManager = LinearLayoutManager(itemView.context)
  49. adapter = SubitemAdapter(apiResponse.rates, apiResponse.date, activity)
  50. }
  51. }
  52. }
  53. inner class ProgressViewHolder(view: View) : RecyclerView.ViewHolder(view)
  54. }

子项适配器.kt

  1. class SubitemAdapter(private val subitems: List<Currency>, private val day: String, private val activity: Activity) : RecyclerView.Adapter<SubitemAdapter.SubitemViewHolder>() {
  2. override fun onCreateViewHolder(parent: ViewGroup, p1: Int): SubitemViewHolder {
  3. val view = LayoutInflater.from(parent.context).inflate(R.layout.subitem, parent, false)
  4. return SubitemViewHolder(view, day, activity)
  5. }
  6. override fun onBindViewHolder(holder: SubitemViewHolder, position: Int) {
  7. if(position < subitems.size - 1) {
  8. activity.progress_bar.visibility = View.VISIBLE
  9. }
  10. else
  11. activity.progress_bar.visibility = View.GONE
  12. holder.bind(subitems[position], position)
  13. }
  14. override fun getItemCount() = subitems.size
  15. class SubitemViewHolder(view: View, day: String, activity: Activity) : RecyclerView.ViewHolder(view) {
  16. private val subitemRootView = view.subitem_root
  17. private val tvCurrencyName = view.tv_currency_name
  18. private val tvCurrencyValue = view.tv_currency_value
  19. private val day = day
  20. private val activity = activity
  21. fun bind(currency: Currency, position: Int) {
  22. subitemRootView.setOnClickListener { v ->
  23. activity as OnChangeFragment
  24. activity.changeFragment(SpecificCurrencyFragment(), ChangeFragmentData(hashMapOf(currency.currencyName to currency.currencyValue.toString()), day))
  25. }
  26. tvCurrencyName.text = currency.currencyName
  27. tvCurrencyValue.text = currency.currencyValue.toString()
  28. }
  29. }
  30. }

这是我认为一切可以帮助我的。但是如果你需要更多的东西,那就用aks。任何帮助我们都将不胜感激。提前谢谢你!

dfddblmv

dfddblmv1#

你已经通过了考试 List<Currency> 哪些是要加载到recyclerview中的项目。你不应该在里面检查这个 onBindViewHolder 因为当您将这些项目作为 argumentrecyclerview adapter 相反,当您通过 List<Currency> 对于适配器,此时必须从活动本身更新progressbar。您可以编辑您的问题并为您的问题添加代码 Activity 如果这对你没有帮助,我会尽力回答你:)

相关问题