TIL) 0908

Hanseul Lee·2022년 9월 8일
0

TIL

목록 보기
8/23

ActionBar Title 수정하기

onCreateView에서 다음과 같이 실행한다. 그러나 이 방법은 메모리 누수를 일으킬 수 있다.

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("your title");

How to set title in app bar with Navigation Architecture Component

Fragment에서 Activity 접근할 때 메모리 누수 방지하기

  • Interface
    interface Owner {
        fun setActionBarTitle (title: String)
    }
  • Fragment
    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)
        }
    
    }
  • Activity
    class MainActivity : AppCompatActivity(), Owner {
    
        ...
    
        override fun setActionBarTitle(title: String) {
            supportActionBar?.title = title
        }
    }

블로그 포스트 “Fragment에서 Activity 접근하기”

ActionBar CustomView

Android Custom Action Bar Example Tutorial | DigitalOcean

RecyclerView Item마다 여백 설정하기

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)
        })
    }

0개의 댓글