У меня вопрос, я создаю приложение для изучения Kotlin, но у меня есть ошибка, и я не могу найти решение.
Я объясняю свою проблему:
IЯ создал проект только с одним действием, в этом упражнении у меня есть bottomNavigationView для изменения моего экрана.Каждый элемент BottomNavigation создает новый фрагмент.
В одном из них (PeopleFragment) я создал viewPager с двумя экранами.На первом экране (OffertionFragment) у меня есть просмотрщик.Я хотел бы щелкнуть по элементу моего recyclerView и заменить его новым фрагментом (выбор будет сделан позже).
Для проверки введите код здесь
Я предоставляю в ваше распоряжение мой код:
class HomeActivity : AppCompatActivity(), SuggestionFragment.suggestionListener {
val manager = supportFragmentManager
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_search -> {
startFragment(SearchFragment())
return@OnNavigationItemSelectedListener true
}
R.id.navigation_favorite -> {
startFragment(FavoriteFragment())
return@OnNavigationItemSelectedListener true
}
R.id.navigation_people -> {
startFragment(PeopleFragment())
return@OnNavigationItemSelectedListener true
}
R.id.navigation_user -> {
startFragment(UserFragment())
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
startFragment(SearchFragment())
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
val suggestionFragment = SuggestionFragment()
suggestionFragment.listener = this
}
override fun onClick() {
Log.i("test","log for a test")
manager.beginTransaction()
.replace(R.id.fragment_container, SearchFragment())
}
fun startFragment(view: Fragment){
val transaction = manager.beginTransaction()
val fragment = view
transaction.replace(R.id.fragment_container, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
}
class PeopleFragment : Fragment(), View.OnClickListener {
lateinit var adapter: ViewPagerAdapter
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_people, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val manager = childFragmentManager
adapter = ViewPagerAdapter(manager)
adapter.addFragments(SuggestionFragment(), "Suggestion")
adapter.addFragments(ActivitiesFragment(),"Activités")
viewPager.adapter = adapter
tabs.setupWithViewPager(viewPager)
}
override fun onClick(v: View?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
class SuggestionFragment : Fragment(), View.OnClickListener {
interface suggestionListener {
fun onClick()
}
var listener: suggestionListener? = null
lateinit var activities: MutableList<Activity> // declaration de la var notes de type liste mutable (car le contenu peut changer)
lateinit var adapter: ActivityAdapter
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sugg, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val manager = childFragmentManager
activities = mutableListOf<Activity>() // initialisation de la var notes en un tableau mutable de type Note
activities.add(Activity("Note 1", "45€", "18/20"))
activities.add(Activity("Note 1", "45€", "18/20"))
activities.add(Activity("Note 1", "45€", "18/20"))
activities.add(Activity("Note 1", "45€", "18/20"))
activities.add(Activity("Note 1", "45€", "18/20"))
activities.add(Activity("Note 1", "45€", "18/20"))
activities.add(Activity("Note 1", "45€", "18/20"))
activities.add(Activity("Note 1", "45€", "18/20"))
activities.add(Activity("Note 1", "45€", "18/20"))
activities.add(Activity("Note 1", "45€", "18/20"))
adapter = ActivityAdapter(activities, this)
suggestion_recyclerView.layoutManager = LinearLayoutManager(context)
suggestion_recyclerView.adapter = adapter
}
override fun onClick(v: View?) {
//Log.i("test","sisi")
listener?.onClick()
}
}
Надеюсь, вы найдете ответ.