即使我使用数据视图模型,但recyclerview仍会在每次屏幕方向更改时一直滚动到顶部。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(activity as AppCompatActivity).setSupportActionBar(binding?.toolbar)
val host: NavHostFragment = activity?.supportFragmentManager
?.findFragmentById(R.id.nav_host_fragment) as NavHostFragment?
?: return
// Set up Action Bar
val navController = host.navController
// Setup bottom navigation view
binding?.bottomNav?.setupWithNavController(navController)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = DataBindingUtil.inflate(inflater , com.angelstudio.newsapp.R.layout.fragment_feed,container , false)
myView= binding.root
return myView
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel =ViewModelProviders.of(this,viewModelFactory).get(FeedFragmentViewModel::class.java)
bindUi()
(activity as? AppCompatActivity)?.supportActionBar?.title = getString(R.string.Naws_App)
(activity as? AppCompatActivity)?.supportActionBar?.subtitle = getString(R.string.Feed)
binding.mySwiperefresh.setOnRefreshListener {
refresh()
mySwiperefresh.setRefreshing(false)
}
}
private fun bindUi()=launch {
val topHeadline =viewModel.topHeadline.await()
val naviagte =viewModel.navigateToDetail.await()
topHeadline.observe(this@FeedFragment, Observer {
if(it == null || it.isEmpty()) return@Observer
binding.recyclerView.apply {
showShimmerAdapter()
topHeadlineAdapter = TopHeadlineAdapter(TopHeadlineListener {
url -> viewModel.onTopHeadlineClicked(url)
})
adapter = topHeadlineAdapter
topHeadlineAdapter.submitList(it)
hideShimmerAdapter()
}
})
这是我在片段上的代码,我找不到我在做错什么,是否有一些东西可以处理viewmodel上的recyclerview位置状态?
答案 0 :(得分:0)
将android:configChanges="orientation|keyboardHidden"
添加到manifest
中的活动标签中,如下所示:
<activity
android:name=".activity.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@style/AppTheme.NoActionBar">
</activity>
答案 1 :(得分:0)
Take a look at this stackoverflow link
您需要以已保存的实例状态存储回收站视图的位置,一旦更改了配置,您必须将回收站视图滚动到存储的位置