Kotlin 同一片段多项活动
Kotlin Same Fragment Multiple Activities
朋友们,我有一个问题。我可以在 activity 中使用图像中的片段。但我希望能够在不同的 activity 中查看此片段。可能吗?我使用了类似于图片中的结构,但我也在写我收到的错误。如果你能帮上忙,我会很高兴。
BreakingNewsFragment.kt
class BreakingNewsFragment : Fragment(R.layout.fragment_breaking_news) {
lateinit var viewModel: NewsViewModel
lateinit var viewModel2:NewsViewModel
lateinit var newsAdapter: NewsAdapter
val TAG = "BreakingNewsFragment"
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = (activity as NewsActivity).viewModel
viewModel2=(activity as UsNewsActivity).viewModel
setupRecyclerView()
newsAdapter.setOnItemClickListener {
val bundle = Bundle().apply {
putSerializable("article", it)
}
findNavController().navigate(
R.id.action_breakingNewsFragment_to_articleFragment,
bundle
)
}
viewModel.breakingNews.observe(viewLifecycleOwner, Observer { response ->
when(response) {
is Resource.Success -> {
hideProgressBar()
response.data?.let { newsResponse ->
newsAdapter.differ.submitList(newsResponse.articles.toList())
val totalPages=newsResponse.totalResults/ QUERY_PAGE_SIZE+2 //Sayfada boşluk oluşuyor
isLastPage=viewModel.breakingNewsPage==totalPages
if(isLastPage){
rvBreakingNews.setPadding(0,0,0,0)
}
}
}
is Resource.Error -> {
hideProgressBar()
response.message?.let { message ->
Log.e(TAG, "An error occured: $message")
}
}
is Resource.Loading -> {
showProgressBar()
}
}
})
}
Error
原因:java.lang.ClassCastException:com.androiddevs.mvvmnewsapp.ui.NewsActivity 无法转换为 com.androiddevs.mvvmnewsapp.ui.UsNewsActivity
在 com.androiddevs.mvvmnewsapp.ui.fragments.BreakingNewsFragment.onViewCreated(BreakingNewsFragment.kt:34)
我想你在从 NewsActivity
创建片段时得到了这个?
您正试图将 UsNewsActivity
视为 NewsActivity
,但事实并非如此。
去吧:
val viewModel: NewsViewModel by activityViewModels()
按照建议 here。它应该给你在 Activity
.
中使用的 ViewModel
如果您想坚持现在的做事方式,您可以在您的活动中实施 interface
,例如
interface NewsActivityInterface {
val viewModel: NewsViewModel
}
class MainActivity : AppCompatActivity(), NewsActivityInterface {
override val viewModel: NewsViewModel by viewModels()
(...)
}
然后让你的视图模型像
viewModel = (activity as NewsActivityInterface).viewModel
或检查类型:
val viewModel = activity.let{
when (it){
is MainActivity -> it.viewModel
is AnotherActivity -> it.viewModel
else -> error ("Needs correct activity")
}
}
朋友们,我有一个问题。我可以在 activity 中使用图像中的片段。但我希望能够在不同的 activity 中查看此片段。可能吗?我使用了类似于图片中的结构,但我也在写我收到的错误。如果你能帮上忙,我会很高兴。
BreakingNewsFragment.kt
class BreakingNewsFragment : Fragment(R.layout.fragment_breaking_news) {
lateinit var viewModel: NewsViewModel
lateinit var viewModel2:NewsViewModel
lateinit var newsAdapter: NewsAdapter
val TAG = "BreakingNewsFragment"
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = (activity as NewsActivity).viewModel
viewModel2=(activity as UsNewsActivity).viewModel
setupRecyclerView()
newsAdapter.setOnItemClickListener {
val bundle = Bundle().apply {
putSerializable("article", it)
}
findNavController().navigate(
R.id.action_breakingNewsFragment_to_articleFragment,
bundle
)
}
viewModel.breakingNews.observe(viewLifecycleOwner, Observer { response ->
when(response) {
is Resource.Success -> {
hideProgressBar()
response.data?.let { newsResponse ->
newsAdapter.differ.submitList(newsResponse.articles.toList())
val totalPages=newsResponse.totalResults/ QUERY_PAGE_SIZE+2 //Sayfada boşluk oluşuyor
isLastPage=viewModel.breakingNewsPage==totalPages
if(isLastPage){
rvBreakingNews.setPadding(0,0,0,0)
}
}
}
is Resource.Error -> {
hideProgressBar()
response.message?.let { message ->
Log.e(TAG, "An error occured: $message")
}
}
is Resource.Loading -> {
showProgressBar()
}
}
})
}
Error
原因:java.lang.ClassCastException:com.androiddevs.mvvmnewsapp.ui.NewsActivity 无法转换为 com.androiddevs.mvvmnewsapp.ui.UsNewsActivity 在 com.androiddevs.mvvmnewsapp.ui.fragments.BreakingNewsFragment.onViewCreated(BreakingNewsFragment.kt:34)
我想你在从 NewsActivity
创建片段时得到了这个?
您正试图将 UsNewsActivity
视为 NewsActivity
,但事实并非如此。
去吧:
val viewModel: NewsViewModel by activityViewModels()
按照建议 here。它应该给你在 Activity
.
ViewModel
如果您想坚持现在的做事方式,您可以在您的活动中实施 interface
,例如
interface NewsActivityInterface {
val viewModel: NewsViewModel
}
class MainActivity : AppCompatActivity(), NewsActivityInterface {
override val viewModel: NewsViewModel by viewModels()
(...)
}
然后让你的视图模型像
viewModel = (activity as NewsActivityInterface).viewModel
或检查类型:
val viewModel = activity.let{
when (it){
is MainActivity -> it.viewModel
is AnotherActivity -> it.viewModel
else -> error ("Needs correct activity")
}
}