kotlinのmvp構成でアプリケーション開発をしており、bottom navの画面遷移がせず困っております。
デフォルトのbottomnavviewではなく、メニューバーをbottomnavにして使っております。
今、実現したいことはbottomnavのボタンを押すことで、originfriendからoriginmypageに飛ばすことです。
情報の不足等がありましたら、ご指摘お願いします。
bottom_nav.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/originFriendActivity" android:icon="@drawable/ic_action_friend" android:title="@string/bottomnav_friend" /> <item android:id="@+id/originEventActivity" android:icon="@drawable/ic_action_event" android:title="@string/bottomnav_event" /> <item android:id="@+id/originMyPageActivity" android:icon="@drawable/ic_action_mypage" android:title="@string/bottomnav_mypage" /> </menu>
###activity_origin_friend.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:background ="@color/colorPrimary" tools:context=".originFriend.OriginFriendActivity"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/btm_nav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentBottom="true" android:layout_marginBottom="1dp" app:itemBackground="@color/whiteColor" app:menu="@menu/bottom_nav" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:titleTextColor="@android:color/white" /> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="543dp"> </FrameLayout> </LinearLayout> </RelativeLayout>
###OriginFriendActivity.kt
kotlin
1lateinit var originFriendFragment: OriginFriendFragment 2 3 4 override fun onCreate(savedInstanceState: Bundle?) { 5 super.onCreate(savedInstanceState) 6 setContentView(R.layout.activity_origin_friend) 7 8 9 setupActionBar(R.id.toolbar){ 10 title = getString(R.string.friend_text) 11 setDisplayHomeAsUpEnabled(true) 12 } 13 14 val originFriendFragment = supportFragmentManager 15 .findFragmentById(R.id.content_frame) as OriginFriendFragment? ?: 16 OriginFriendFragment.newInstance().also { 17 replaceFragmentInActivity(it, R.id.content_frame) 18 } 19 20 21 OriginFriendPresenter(FriendsRepository.getInstance(FriendsRemoteDataSource.getInstance(), 22 FriendsLocalDataSource.getInstance()), UserRepository.getInstance( 23 UserRemoteDataSource(), 24 UserLocalDataSource.getInstance 25 ()),originFriendFragment) 26 27 28 29 30 31 } 32 33 companion object{ 34 const val EXTRA_FRIEND_ID = "FRIEND_ID" 35 const val EXTRA_GROUP_ID = "GROUP_ID" 36 } 37}
###OrigiinFriendFragment.kt
kotlin
1package com.example.gotcha2.originFriend 2 3---import省略 4 5class OriginFriendFragment : Fragment() ,OriginFriendContract.View{ 6 7 override lateinit var presenter: OriginFriendContract.Presenter 8 9 10 11----private lateinitia varは省略 12 13 14 override var isActive: Boolean = false 15 get() = isAdded 16 17 18 19 override fun onResume() 20 { 21 super.onResume() 22 presenter.start() 23 } 24 25 26 27 override fun onCreateView( 28 inflater: LayoutInflater, 29 container: ViewGroup?, 30 savedInstanceState: Bundle? 31 ): View? 32 { 33 val root = inflater.inflate(R.layout.fragment_origin_friend, container, false) 34 35 val friendQuery = userDocRef.collection("friends") 36 friendsAdapter = FriendsAdapter(friendQuery, recyclerViewItemListener) 37 38 val groupQuery = userDocRef.collection("groups") 39 groupsAdapter = GroupsAdapter(groupQuery, recyclerViewItemListener) 40 41 with(root){ 42 //リニアーレイアウトたちの初期化 43 recentlyFriendsLayout = findViewById(R.id.recentlyfriends_layout) 44 friendsLayout = findViewById(R.id.friends_layout) 45 groupsLayout = findViewById(R.id.groups_layout) 46 47 48 //recyclerviewの初期化 49 recentlyFriendsRecyclerView = (findViewById<RecyclerView>(R.id.recentlyfriends_recyclerview)).apply { 50 //adapter = recentlyFriendsAdapter 51 } 52 friendsRecyclerView = findViewById<RecyclerView>(R.id.friends_recyclerview).apply { 53 adapter = friendsAdapter 54 layoutManager = GridLayoutManager(context, 3) 55 } 56 groupsRecyclerView = (findViewById<RecyclerView>(R.id.groups_recyclerview)).apply { 57 58 } 59 60 //テキスト押下による表示、日表示の切り替え 61 recentlyFriendsText = (findViewById<TextView>(R.id.recenetly_friends)).also { 62 it.setOnClickListener { 63 switchViewVisibility(recentlyFriendsLayout) 64 } 65 } 66 friendsText = (findViewById<TextView>(R.id.friends)).also { 67 it.setOnClickListener { 68 switchViewVisibility(friendsLayout) 69 } 70 } 71 groupsText = findViewById<TextView>(R.id.groups).also { 72 it.setOnClickListener { 73 switchViewVisibility(groupsLayout) 74 } 75 } 76 77 78 addFriendsBtn = (findViewById<ImageButton>(R.id.addfriendButton)).also { 79 it.setOnClickListener { 80 presenter.openAddFriends() 81 } 82 } 83 settingBtn = (findViewById<ImageButton>(R.id.setting_button)).also{ 84 it.setOnClickListener { 85 presenter.openSettings() 86 } 87 } 88 89 searchView = (findViewById(R.id.originfriend_search)) 90 91 progressbar =(findViewById<ProgressBar>(R.id.originfriend_progressbar)) 92 93 norecenttext = (findViewById(R.id.no_recentlyfriends_text)) 94 95 nofriendtext = (findViewById(R.id.no_frineds_text)) 96 97 nogrouptext = (findViewById(R.id.no_groups_text)) 98 } 99 100 return root 101 } 102 103 104 override fun onStart() 105 { 106 super.onStart() 107// recentlyFriendsAdapter 108 friendsAdapter.startListening() 109// groupsAdapter 110 } 111 112 override fun onStop() 113 { 114 super.onStop() 115// recentlyFriendsAdapter 116 friendsAdapter.stopListening() 117// groupsAdapter 118 } 119 120 override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) 121 { 122 inflater.inflate(R.menu.bottom_nav, menu) 123 } 124 125 override fun onOptionsItemSelected(item: MenuItem): Boolean { 126 when (item.itemId) { 127 R.id.originFriendActivity ->{} 128 R.id.originEventActivity -> {presenter.openOriginEvent() 129 return true} 130 R.id.originMyPageActivity -> {presenter.openOriginMyPage() 131 return true 132 } 133 } 134 135 return super.onOptionsItemSelected(item) 136 } 137 138 139 140 ---関係のなさそうなfunctionは省略 141 142 143 override fun transitionOriginEvent() { 144 val intent = Intent(this.context,OriginEventActivity::class.java) 145 startActivity(intent) 146 } 147 148 override fun transitionOriginMyPage() { 149 val intent = Intent(this.context, OriginMyPageActivity::class.java) 150 startActivity(intent) 151 } 152 153 154 155-----関係のなさそうなfunctioは省略 156 157 158 companion object{ 159 fun newInstance() = OriginFriendFragment() 160 } 161 162} 163 164 165
###OriginFriendPresenter.kt
kotlin
1package com.example.gotcha2.originFriend 2 3----importは省略 4 5class OriginFriendPresenter( 6 private val friendsRepository: FriendsRepository, 7// private val groupsRepository: GroupsRepository, 8 private val userRepository: UserRepository, 9 private val originFriendView: OriginFriendContract.View, 10 private val uiContext: CoroutineContext = Dispatchers.Main 11): OriginFriendContract.Presenter { 12 13 14 init 15 { 16 originFriendView.presenter = this 17 } 18 19 override fun start() 20 { 21// loadFriends() 22 //loadGroups() 23 } 24 25 26 -----関係のなさそうなfunctionは省略 27 28 29 override fun openOriginEvent() 30 { 31 originFriendView.transitionOriginEvent() 32 } 33 34 override fun openOriginMyPage() 35 { 36 originFriendView.transitionOriginMyPage() 37 } 38 39
回答1件
あなたの回答
tips
プレビュー