在我的应用程序中,我有一个列表,在这个列表中,我还有另一个列表!(我知道这对UI是个坏主意,但我应该开发它!)
为了显示此列表,我使用了RecyclerView
并将其他RecyclerView
初始化为先前的RecyclerView
适配器!
活动代码:
class DashboardCardsActivity : BaseActivity(), DashboardCardsContracts.View {
@NonNull
private lateinit var presenter: DashboardCardsPresenterImpl
private var context: Context = this
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: DashboardCardsAdapter
private lateinit var layoutManager: LinearLayoutManager
private val modelList: MutableList<UserPackageOrderResponse.Res.UserPackageOrder> = mutableListOf()
private var isLoadingFlag = false
private var isHasLoadedAll = false
private var nextPage = 1
//Token
private var token = GoodPrefs.getInstance().getString(PrefsKey.USER_JWT_TOKEN.name, "")
override var layoutId: Int = R.layout.activity_dashboard_cards
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//Initialize
context = this
presenter = DashboardCardsPresenterImpl(context, this)
layoutManager = LinearLayoutManager(context)
adapter = DashboardCardsAdapter(context, modelList)
//Init recyclerView
recyclerView = dashboardCards_pullList.recyclerView
recyclerView.initRecyclerView(layoutManager, adapter)
//Set image to loader
dashboardCards_pullList.setColorSchemeResources(R.color.colorAccent)
//Init toolbar
toolbarBase_toolbar.title = getString(R.string.basketCard)
toolbarBase_toolbar.setToolbarBackWithFinish(this)
//Call api
getLazyPullLoader()
}
我写了以下代码:
class DashboardListAdapter constructor(
val context: Context, val model: MutableList<UserPackageOrderResponse.Res.UserPackageOrder>
) : RecyclerView.Adapter<DashboardCardsAdapter.ViewHolder>() {
private var expansionlayout: ExpansionLayoutCollection = ExpansionLayoutCollection()
private lateinit var registerTimeUtil: TimeUtils
private var registerDateSplit: List<String> = emptyList()
private var registerDate: List<String> = emptyList()
private var orderState: String = ""
private var layoutManager: LinearLayoutManager? = null
private var adapter: DashboardCardsOrderAdapter
private val orderModelList: MutableList<UserPackageOrderResponse.Res.UserPackageOrder.Order> = mutableListOf()
init {
expansionlayout.openOnlyOne(true)
layoutManager = object : LinearLayoutManager(context) {
override fun canScrollVertically(): Boolean {
return false
}
}
adapter = DashboardCardsOrderAdapter(orderModelList)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return ViewHolder(inflater.inflate(R.layout.row_dashboard_card_list, parent, false))
}
override fun getItemCount(): Int {
return model.size
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
expansionlayout.add(holder.getExpansions())
//header
holder.orderNumber.text = model[position].hashcode
holder.rowNumber.text = "${position + 1}"
//Set date
registerDateSplit = model[position].createdAt.split(" ")
registerDate = registerDateSplit[0].split("-")
registerTimeUtil = TimeUtils(registerDate[0].toInt(), registerDate[1].toInt(), registerDate[2].toInt())
holder.orderRegisterDate.text = registerTimeUtil.getIranianDate()
//Order state
orderState = model[position].status
holder.setViewWithState(orderState, context)
//Content prices
holder.finalPrice.text = "${model[position].price.moneySeparating()} ${context.getString(R.string.toman)}"
holder.paymentPrice.text = "${model[position].price.moneySeparating()} ${context.getString(R.string.toman)}"
holder.postPrice.text = "${model[position].postFee.moneySeparating()} ${context.getString(R.string.toman)}"
holder.discountPrice.text = "${model[position].discount.moneySeparating()} ${context.getString(R.string.toman)}"
//init order list
orderModelList.clear()
orderModelList.addAll(model[position].orders)
layoutManager?.let {
holder.orderList.initRecyclerView(it, adapter)
}
}
}
但是在运行应用程序时显示 ForceClose 错误,并在 logCat 中显示以下消息:
java.lang.IllegalArgumentException: LayoutManager com.app.android.ui.home.fragments.dashboard.activities.carts_list.DashboardCardsAdapter$1@d33c634 is already attached to a RecyclerView: androidx.recyclerview.widget.RecyclerView{5a76c5d VFED..... ......ID 0,0-682,178 #7f080089 app:id/dashboardCard_orderList}, adapter:com.app.android.ui.home.fragments.dashboard.activities.carts_list.DashboardCardsOrderAdapter@83ba9d2, layout:com.app.android.ui.home.fragments.dashboard.activities.carts_list.DashboardCardsAdapter$1@d33c634, context:com.app.android.ui.home.fragments.dashboard.activities.carts_list.DashboardCardsActivity@79c2f05
at androidx.recyclerview.widget.RecyclerView.setLayoutManager(RecyclerView.java:1340)
at com.app.android.utils.ExtensionsKt.initRecyclerView(Extensions.kt:74)
at com.app.android.ui.home.fragments.dashboard.activities.carts_list.DashboardCardsAdapter.onBindViewHolder(DashboardCardsAdapter.kt:77)
at com.app.android.ui.home.fragments.dashboard.activities.carts_list.DashboardCardsAdapter.onBindViewHolder(DashboardCardsAdapter.kt:22)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
但是用下面的情况更改我的代码后,没有显示任何错误,但是滚动非常缓慢,并显示出滞后!
//init order list
orderModelList.clear()
orderModelList.addAll(model[position].orders)
layoutManager = object : LinearLayoutManager(context) {
override fun canScrollVertically(): Boolean {
return false
}
}
layoutManager?.let {
holder.orderList.initRecyclerView(it, adapter)
}
我在layoutManager
中初始化了onBindView()
,然后在滚动项目时不显示错误但显示很多延迟!
我该如何解决?
答案 0 :(得分:0)
您已经定义了:
private var layoutManager: LinearLayoutManager? = null
定义另一个说法,
private var layoutManager1: LinearLayoutManager? = null
将其分配给第二个RecyclerView
并运行它。应该可以。
P.S:不熟悉Kotlin
,在Java中,我们使用
recyclerview.setLayoutManager(new LinearLayoutManager(MyActivity.this));