[英]android-kotlin click Listener not working to replace fragment
i create an simple app to learn how to work with fragment in kotlin and as usual if i create recycler view i have to create a new page to show the details of an item so after search ad asking some one answer by create interface in my adapter include with a fun that i override in my MainActivity to replace the fragment and execute it on item Click and i do it but after i done the click do nothing but no error here my adapter 我创建了一个简单的应用程序,以学习如何在Kotlin中处理碎片,如果创建回收站视图,则照常进行操作,我必须创建一个新页面来显示项目的详细信息,因此在搜索广告后,通过在我的适配器中创建界面来询问一个答案包括一个有趣的功能,我可以在MainActivity中覆盖它以替换片段并在Click项上执行它,然后执行此操作,但是单击完后什么都不做,但这里没有错误
class ContentAdapter constructor(private val activity: MainActivity, private var listOfData: ArrayList<MainMarketTickClass>, val listener: ContentListener) : RecyclerView.Adapter<ContentAdapter.ViewHolder>() {
override fun getItemCount(): Int = listOfData.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
var inf = ViewHolder(LayoutInflater.from(parent!!.context).inflate(R.layout.maintick, parent, false))
return inf
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(listOfData, listener)
}
inner class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
fun bind(listOfData: ArrayList<MainMarketTickClass>, listener: ContentListener) {
val dataListin2 = listOfData[adapterPosition]
itemView.textView.text = dataListin2.title
itemView.MainImage.setOnClickListener {
listener.onItemClicked(listOfData.get(adapterPosition))
}
}
}
interface ContentListener {
fun onItemClicked(item: MainMarketTickClass){
}
}
and here my recycler view fragment code 这是我的回收者视图片段代码
class MainMarket: Fragment(),ContentAdapter.ContentListener{
var ITEMSList = ArrayList<MainMarketTickClass>()
companion object {
fun newInstance():Fragment{
var fb : MainMarket = MainMarket()
return fb
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var inf = inflater!!.inflate(R.layout.main_marker,container,false)
return inf
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
ITEMSList .add ( MainMarketTickClass("123"))
ITEMSList .add ( MainMarketTickClass(" 123"))
ITEMSList .add ( MainMarketTickClass("123"))
var adapter = ContentAdapter (MainActivity(),ITEMSList,this)
list.adapter = adapter
list.layoutManager = LinearLayoutManager(this.context,LinearLayoutManager.VERTICAL,false)
}
override fun onItemClicked(item: MainMarketTickClass) {
}
and MainActivity.kt 和MainActivity.kt
class MainActivity : AppCompatActivity(), ContentAdapter.ContentListener {
override fun onItemClicked(item: MainMarketTickClass) {
var ft1 : FragmentTransaction = supportFragmentManager.beginTransaction()
ft1.replace(R.id.MainFrame,AddCar.newInstanceaddcar())
ft1.commit()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var bnv = findViewById(R.id.navigation) as BottomNavigationView
bnv.setOnNavigationItemSelectedListener (object : BottomNavigationView.OnNavigationItemSelectedListener{
override fun onNavigationItemSelected(item: MenuItem): Boolean {
var selectFragment : Fragment? = null
when (item.itemId) {
R.id.navigation_home -> {
selectFragment = MainMarket.newInstance()
}
R.id.navigation_dashboard -> {
selectFragment = AddCar.newInstanceaddcar()
}
R.id.navigation_notifications -> {
}
}
var ft : FragmentTransaction = supportFragmentManager.beginTransaction()
ft.replace(R.id.MainFrame,selectFragment)
ft.commit()
return true
}
})
var ft : FragmentTransaction = supportFragmentManager.beginTransaction()
ft.replace(R.id.MainFrame,MainMarket.newInstance())
ft.commit()
}
}
In adapter class 在适配器类中
itemView.MainImage.setOnClickListener {
listener.onItemClicked(listOfData.get(adapterPosition))
acitivity.onItemClicked(listOfData.get(adapterPosition))
}
or In the fragment, 或在片段中
var adapter = ContentAdapter (MainActivity(),ITEMSList, <Pass the context of main activity here>)
or In the fragment 或在片段中
override fun onItemClicked(item: MainMarketTickClass) {
if(activity is MainActivity){
(activity as MainActivity).onItemClicked(item)
}
}
/************/ / ************ /
To pass data in your fragment in MainActivity 在MainActivity中传递片段中的数据
override fun onItemClicked(item: MainMarketTickClass) {
var ft1 : FragmentTransaction = supportFragmentManager.beginTransaction()
ft1.replace(R.id.MainFrame,AddCar.newInstanceaddcar(item.someValue))
ft1.commit()
}
In your fragment 在你的片段
companion object{
fun newInstanceaddcar(someValue : String) : AddCar{
val frag = AddCar()
val args = Bundle()
args.putString("key_some_value", someValue);
frag.arguments = args
return frag
}
}
and then in onCreate method of fragment get the value from arguments and put it in some variable 然后在片段的onCreate方法中从参数获取值并将其放在某个变量中
arguments?.let{
someVariableDeclaredInAddCarFrag = it.getString("key_some_value")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.