问题
I get Application terminated after click and I don't know where I must type the order of click in adapter or in fragment of RecyclerView Mainadapter.kt
class ContentAdapter(
private val activity: MainActivity,
private var listOfData: ArrayList<MainMarketTickClass>
) : RecyclerView.Adapter<ContentAdapter.ViewHolder>(), View.OnClickListener {
override fun onClick(v: View?) {
val manager = activity.supportFragmentManager
val transaction = manager.beginTransaction()
val trs = AddCar()
transaction.add(android.R.id.content, trs, "IndividualBook")
transaction.addToBackStack(null)
transaction.commit()
var heroActivity: MainActivity = activity
}
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)
holder.itemView.MainImage.setOnClickListener(this)
}
class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
fun bind(listOfData: ArrayList<MainMarketTickClass>) {
val dataListin2 = listOfData[adapterPosition]
itemView.textView.text = dataListin2.title
}
}
RecyclerView fragment
class MainMarket : Fragment() {
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)
list.adapter = adapter
list.layoutManager = LinearLayoutManager(this.context, LinearLayoutManager.VERTICAL, false)
}
}
I hade some errore in OnClick fun FATAL EXCEPTION
回答1:
I would recommend you to use a different approach than having the on click listener transactions in the Adapter.
This is the Adapter
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)
}
class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
fun bind(listOfData: ArrayList<MainMarketTickClass>, listener: ContentListener) {
val dataListin2 = listOfData[adapterPosition]
itemView.textView.text = dataListin2.title
// this is the click listener. It calls the onItemClicked interface method implemented in the Activity
itemView.setOnClickListener {
listener.onItemClicked(listOfData.get(adapterPosition))
}
}
}
public interface ContentListener {
fun onItemClicked(item: MainMarketTickClass)
}
}
And this is the MainActivity
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)
list.adapter = adapter
list.layoutManager = LinearLayoutManager(this.context,LinearLayoutManager.VERTICAL,false)
}
override onItemClicked(item: MainMarketTickClass) {
// this method gets executed everytime an item gets clicked. You should
// delegate the fragment transactions to the activity that hosts the fragment.
// You should not do a fragment transaction from inside the fragment.
// You can use a similar interface approach to contact the activity from the Fragment.
}
来源:https://stackoverflow.com/questions/49802541/kotlin-cant-replace-fragment-from-recyclerview-adapter