我正在创建社交媒体应用程序,我已经用Retrieving database to recyclerator view和Onclick listener完成了这项工作,但我想为底部表单初始化布局膨胀器到适配器类。我使用适配器类来点击事件和播放类似的视频。我无法将布局充气器添加到适配器类中。因为我只是使用adapter和adapter类来膨胀XML仅包含回收器视图类,不包含Appcompat Activity。所以认为这是一个问题,以及为什么我要求在没有适配器的情况下检索firebase数据库到回收器视图。
@Suppress("DEPRECATION")
class CommentAdapter(private var context: Context, private var data:
List<Comment>) : RecyclerView.Adapter<CommentAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val row: View = LayoutInflater.from(context).inflate(R.layout.commentpostsnamashivaya, parent, false)
return MyViewHolder(row)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.commentTv.text = data[position].getComment()
}
override fun getItemCount(): Int {
return data.size
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var commentTv: TextView = itemView.findViewById(R.id.comment_text)
var report_post_of_lord: ImageView = itemView.findViewById(R.id.report_user_of_lordshivaproduct)
var sharepost: ImageView = itemView.findViewById(R.id.post_share_somam)
init {
itemView.setOnClickListener {
val intent = Intent(context, viewing_post_nama::class.java)
val position: Int = adapterPosition
intent.putExtra("Comment", data[position].getComment())
context.startActivity(intent)
}
report_post_of_lord.setOnClickListener{
Toast.makeText(context,"Reporting Post . . . . .", Toast.LENGTH_LONG).show()
//here is my problem here i just only mentioned layoutinflater only.
val layinf =
LayoutInflater.from(context).inflate(R.layout.thisisforbsheet, parent,
false)
}
sharepost.setOnClickListener{
val shareIntent = Intent(Intent.ACTION_SEND)
val text = data[position].getComment() + "BY COMMENTING APP FROM LORD SHIVA PRODUCTS FAMILY :) "
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TEXT, text)
context.startActivity(Intent.createChooser(shareIntent,"SHARE COMMENT"))
}
}
}
Ps:学习英语
编辑:我添加了适配器类的代码。
提前谢谢。
LayoutInflater
只是一个对象。很容易得到一个,例如在Activity
上调用getLayoutInflater()
。然后可以将它传递给RecyclerView.Adapter
,例如通过构造函数参数。
例如,这里有一个接受LayoutInflater
作为构造函数参数的RecyclerView.Adapter
:
/*
Copyright (c) 2018-2020 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
Covered in detail in the book _Elements of Android Jetpack_
https://commonsware.com/Jetpack
*/
package com.commonsware.jetpack.sampler.recyclerview
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.commonsware.jetpack.sampler.recyclerview.databinding.RowBinding
class ColorAdapter(private val inflater: LayoutInflater) :
ListAdapter<Int, ColorViewHolder>(ColorDiffer) {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ColorViewHolder {
return ColorViewHolder(RowBinding.inflate(inflater, parent, false))
}
override fun onBindViewHolder(holder: ColorViewHolder, position: Int) {
holder.bindTo(getItem(position))
}
private object ColorDiffer : DiffUtil.ItemCallback<Int>() {
override fun areItemsTheSame(oldColor: Int, newColor: Int): Boolean {
return oldColor == newColor
}
override fun areContentsTheSame(oldColor: Int, newColor: Int): Boolean {
return areItemsTheSame(oldColor, newColor)
}
}
}
活动可以通过ColorAdapter(layoutInflater)
:创建一个实例
/*
Copyright (c) 2018-2020 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
Covered in detail in the book _Elements of Android Jetpack_
https://commonsware.com/Jetpack
*/
package com.commonsware.jetpack.sampler.recyclerview
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.commonsware.jetpack.sampler.recyclerview.databinding.ActivityMainBinding
import java.util.*
class MainActivity : AppCompatActivity() {
private val random = Random()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.items.apply {
layoutManager = LinearLayoutManager(this@MainActivity)
addItemDecoration(
DividerItemDecoration(this@MainActivity, DividerItemDecoration.VERTICAL)
)
adapter = ColorAdapter(layoutInflater).apply {
submitList(buildItems())
}
}
}
private fun buildItems() = List(25) { random.nextInt() }
}
是的,我认为有办法解决您的问题。您可以使用firebaserecycleradapter来填充recyclerview中的数据。以下是我们如何使用的链接
https://firebaseopensource.com/projects/firebase/firebaseui-android/database/readme/