我创建了一个简单的应用程序来学习如何使用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){
}
}
这里我的Recycler视图片段代码
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) {
}
和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()
}
}
答案 0 :(得分:0)
在适配器类
中itemView.MainImage.setOnClickListener {
listener.onItemClicked(listOfData.get(adapterPosition))
acitivity.onItemClicked(listOfData.get(adapterPosition))
}
或 在片段中,
var adapter = ContentAdapter (MainActivity(),ITEMSList, <Pass the context of main activity here>)
或 在片段中
override fun onItemClicked(item: MainMarketTickClass) {
if(activity is MainActivity){
(activity as MainActivity).onItemClicked(item)
}
}
/ ************ /
传递片段中的数据 在MainActivity中
override fun onItemClicked(item: MainMarketTickClass) {
var ft1 : FragmentTransaction = supportFragmentManager.beginTransaction()
ft1.replace(R.id.MainFrame,AddCar.newInstanceaddcar(item.someValue))
ft1.commit()
}
在你的片段中
companion object{
fun newInstanceaddcar(someValue : String) : AddCar{
val frag = AddCar()
val args = Bundle()
args.putString("key_some_value", someValue);
frag.arguments = args
return frag
}
}
然后在onCreate方法的片段中从参数中获取值并将其放入某个变量
arguments?.let{
someVariableDeclaredInAddCarFrag = it.getString("key_some_value")
}