onCreateView에서 다음과 같이 실행한다. 그러나 이 방법은 메모리 누수를 일으킬 수 있다.
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("your title");
How to set title in app bar with Navigation Architecture Component
interface Owner {
fun setActionBarTitle (title: String)
}
class NewsDetailFragment : BaseFragment<FragmentNewsDetailBinding>(
FragmentNewsDetailBinding::bind,
R.layout.fragment_news_detail
) {
lateinit var owner: Owner
override fun onAttach(context: Context) {
super.onAttach(context)
owner = context as Owner
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
sharedViewModel.article.value?.title?.let { owner.setActionBarTitle(it) }
return super.onCreateView(inflater, container, savedInstanceState)
}
}
class MainActivity : AppCompatActivity(), Owner {
...
override fun setActionBarTitle(title: String) {
supportActionBar?.title = title
}
}
블로그 포스트 “Fragment에서 Activity 접근하기”
Android Custom Action Bar Example Tutorial | DigitalOcean
class RecyclerViewMargin : ItemDecoration() {
private val margin = 70
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
outRect.bottom = margin
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
binding.newsListRecyclerView.addItemDecoration(RecyclerViewMargin())
binding.newsListRecyclerView.adapter = NewsListAdapter(ArticleListener { article ->
sharedViewModel.onArticleClicked(article)
findNavController().navigate(R.id.action_newsListFragment_to_newsDetailFragment)
})
}