gpt4 book ai didi

android - 将 RecycleRview 适配器连接到 fragment 时缺少参数

转载 作者:行者123 更新时间:2023-11-29 00:55:38 24 4
gpt4 key购买 nike

我正在尝试将我的 RecyclerView 适配器与 fragment 相关联,但它在适配器创建阶段需要另一个参数。我知道参数中需要包含与 fm 有关的内容,但我不确定我需要去那里做什么。

No value passed for parameter 'fm'

RecyclerView 适配器类

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.companyname.appname.*
import com.google.android.flexbox.FlexboxLayout
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.fragment_dialog.view.*

class MyRVAdapter(private val myList: ArrayList<Item>, private val fm: FragmentManager) : RecyclerView.Adapter<MyRVAdapter.ViewHolder>() {
override fun getItemCount(): Int {
return myList.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.myBtn.text = (myList[position].textBtnTitle)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.my_cv, parent, false)

return ViewHolder(v)
}

class ViewHolder (itemView : View):RecyclerView.ViewHolder(itemView) {
val myBtn = itemView.findViewById<Button>(R.id.btn_a)!!

init {
myBtn.setOnClickListener {
val builder = AlertDialog.Builder(myBtn.context)
builder.setTitle(myList[position].txtDialogTitle)
builder.setMessage(myList[position].txtDialogMessage)
builder.setPositiveButton(android.R.string.ok){ dialog, _ -> dialog.dismiss() }

val dialog: AlertDialog = builder.create()
dialog.show()

tabLayout = customView.findViewById(R.id.mTabLayout)
viewPager = customView.findViewById(R.id.mViewPager)

val adapter = TabbedDialogAdapter(fm)
adapter.addFragment("Tab A", TabbedDialogFragment.createInstance("Description A"))
adapter.addFragment("Tab B", TabbedDialogFragment.createInstance("Description B"))
adapter.addFragment("Tab C", TabbedDialogFragment.createInstance("Description C"))

customView.mViewPager.adapter = adapter
customView.mTabLayout.setupWithViewPager(customView.mViewPager)
}
}
}
}

TabbedDialogAdapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter

class TabbedDialogAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
private var mFragmentCollection: MutableList<Fragment> = ArrayList()
private var mTitleCollection: MutableList<String> = ArrayList()
fun addFragment(title: String, fragment: Fragment) {
mTitleCollection.add(title)
mFragmentCollection.add(fragment)
}

override fun getPageTitle(position: Int): CharSequence? {
return mTitleCollection[position]
}

override fun getItem(position: Int): Fragment {
return mFragmentCollection[position]
}

override fun getCount(): Int {
return mFragmentCollection.size
}
}

fragment 类

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.companyname.appname.R
import com.companyname.appname.MyRVAdapter
import java.util.*

class MyFragment : androidx.fragment.app.Fragment() {
private lateinit var mRecyclerView: RecyclerView

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_rv, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view

mRecyclerView = v!!.findViewById<RecyclerView>(R.id.my_recyclerview)

mRecyclerView.layoutManager = LinearLayoutManager(activity)

val myList = ArrayList<Item>()

val mAdapter = MyRVAdapter(myList)

mRecyclerView.adapter = mAdapter

super.onActivityCreated(savedInstanceState)
}
}

最佳答案

我认为这是一个简单的解决方案。您只是忘记提供一个参数。在您的 adapter 构造函数中,您有 ArrayListFragmentManager:

class MyRVAdapter(private val myList: ArrayList<Item>, private val fm: FragmentManager)

但是当您在此处调用您的适配器时:val mAdapter = MyRVAdapter(myList) 您只提供了ArrayList,因此缺少一个参数,并且这可能是 getFragmentManager() 如果适配器是在 ActivityFragment 中创建的,它将是 getSupportFragmentManager()

关于android - 将 RecycleRview 适配器连接到 fragment 时缺少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54963613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com