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

在BRAVH中怎么使用呢 #24

Open
xyggs opened this issue Apr 28, 2021 · 1 comment
Open

在BRAVH中怎么使用呢 #24

xyggs opened this issue Apr 28, 2021 · 1 comment

Comments

@xyggs
Copy link

xyggs commented Apr 28, 2021

比如在BRAVH中使用viewbind, 不会自己去写viewholder, 是直接使用adapter 提供的 BaseViewHolder, 照着文档中的例子写,编译通不过

@hi-dhl hi-dhl added the bug Something isn't working label Apr 28, 2021
hi-dhl added a commit that referenced this issue May 15, 2021
@hi-dhl
Copy link
Owner

hi-dhl commented May 15, 2021

你好,因近期比较忙,所以现在才看这个问题,这是使用上的一个问题,我已经写了一个和开源库 BRVAH 一起使用的例子。

class BRVAHActivity : AppCompatActivity() {
private val binding: ActivityBrvahBinding by viewbind()
private val viewModel: ListViewModel by viewModel()
private val adapter: BRVAHAdapter by lazy { BRVAHAdapter() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
with(binding) {
recyclerView.adapter = adapter
}
viewModel.liveData.observe(this@BRVAHActivity) {
adapter.addData(it)
adapter.notifyDataSetChanged()
}
}
companion object {
fun startActivity(activity: Activity) {
activity.startActivity(Intent(activity, BRVAHActivity::class.java))
}
}
}
class BRVAHAdapter : BaseQuickAdapter<Product, BRVAHViewHolder>(0) {
override fun convert(holder: BRVAHViewHolder, item: Product) {
holder.bindData(item)
}
override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): BRVAHViewHolder {
val view = inflateView(parent, viewType)
return BRVAHViewHolder(view)
}
private fun inflateView(viewGroup: ViewGroup, @LayoutRes viewType: Int): View {
val layoutInflater = LayoutInflater.from(viewGroup.context)
return layoutInflater.inflate(viewType, viewGroup, false)
}
override fun getDefItemViewType(position: Int): Int = R.layout.recycle_item_product
}
class BRVAHViewHolder(view: View) : BaseViewHolder(view) {
val binding: RecycleItemProductBinding by viewbind()
fun bindData(data: Product) {
binding.apply {
product = data
executePendingBindings()
}
}
}

@hi-dhl hi-dhl added discuss / 讨论 and removed bug Something isn't working labels May 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants