Kotlin Json 按 activity 选择列出
Kotlin Json listing by activity selection
我有一个问题。我在我的项目中列出了来自 API 的数据。我在我的项目中添加了导航抽屉菜单。这些是 NewsActivity 和 UsNewsActivity.2 activity 中的相同片段。我想要做的是在单击 NewsActivity 时列出 getBreakingNews 并列出 UsGetBreakingNews.When 我单击 UsNewsActivity.In 最后一部分,我想通过检查哪个 activity 来列出突发新闻片段你来自。我已经删除了所需的 类。如果你能帮上忙,我会很高兴。
NewsRepository.kt
class NewsRepository(
val db: ArticleDatabase
) {
suspend fun getBreakingNews(countryCode: String, pageNumber: Int) =
RetrofitInstance.api.getBreakingNews(countryCode, pageNumber)
suspend fun getUsBreakingNews(countryCode: String, pageNumber: Int) =
RetrofitInstance.api.getUsBreakingNews(countryCode, pageNumber)
NewsActivity
nav_view.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.country1 -> {
val intent = Intent(this, NewsActivity::class.java)
startActivity(intent)
}
R.id.country2->{
val intent = Intent(this, UsNewsActivity::class.java)
startActivity(intent)
}
}
true
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (toggle.onOptionsItemSelected(item)){
return true
}
return super.onOptionsItemSelected(item)
}
NewsViewModel.kt
val breakingNews: MutableLiveData<Resource<NewsResponse>> = MutableLiveData()
var breakingNewsPage = 1
var breakingNewsResponse:NewsResponse?=null
val usBreakingNews:MutableLiveData<Resource<NewsResponse>> = MutableLiveData()
var usBreakingNewsPage = 1
var usBreakingNewsResponse:NewsResponse?=null
init {
getBreakingNews("tr")
getUsBreakingNews("us")}
fun getBreakingNews(countryCode: String) = viewModelScope.launch {
breakingNews.postValue(Resource.Loading())
val response = newsRepository.getBreakingNews(countryCode, breakingNewsPage)
breakingNews.postValue(handleBreakingNewsResponse(response))
}
fun getUsBreakingNews(countryCode: String) = viewModelScope.launch {
usBreakingNews.postValue(Resource.Loading())
val response = newsRepository.getBreakingNews(countryCode, breakingNewsPage)
usBreakingNews.postValue(handleBreakingNewsResponse(response))
}
BreakingNewsFragment.kt
if(shouldPaginate){
viewModel.getUsBreakingNews("tr")
isScrooling=false
}
控制
创建片段并将其附加到 activity 后,您可以检查 activity 它附加到什么。
在您的片段中,将其放入 onCreate()
if(activity!! is NewsActivity){
//do something
}
我有一个问题。我在我的项目中列出了来自 API 的数据。我在我的项目中添加了导航抽屉菜单。这些是 NewsActivity 和 UsNewsActivity.2 activity 中的相同片段。我想要做的是在单击 NewsActivity 时列出 getBreakingNews 并列出 UsGetBreakingNews.When 我单击 UsNewsActivity.In 最后一部分,我想通过检查哪个 activity 来列出突发新闻片段你来自。我已经删除了所需的 类。如果你能帮上忙,我会很高兴。
NewsRepository.kt
class NewsRepository(
val db: ArticleDatabase
) {
suspend fun getBreakingNews(countryCode: String, pageNumber: Int) =
RetrofitInstance.api.getBreakingNews(countryCode, pageNumber)
suspend fun getUsBreakingNews(countryCode: String, pageNumber: Int) =
RetrofitInstance.api.getUsBreakingNews(countryCode, pageNumber)
NewsActivity
nav_view.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.country1 -> {
val intent = Intent(this, NewsActivity::class.java)
startActivity(intent)
}
R.id.country2->{
val intent = Intent(this, UsNewsActivity::class.java)
startActivity(intent)
}
}
true
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (toggle.onOptionsItemSelected(item)){
return true
}
return super.onOptionsItemSelected(item)
}
NewsViewModel.kt
val breakingNews: MutableLiveData<Resource<NewsResponse>> = MutableLiveData()
var breakingNewsPage = 1
var breakingNewsResponse:NewsResponse?=null
val usBreakingNews:MutableLiveData<Resource<NewsResponse>> = MutableLiveData()
var usBreakingNewsPage = 1
var usBreakingNewsResponse:NewsResponse?=null
init {
getBreakingNews("tr")
getUsBreakingNews("us")}
fun getBreakingNews(countryCode: String) = viewModelScope.launch {
breakingNews.postValue(Resource.Loading())
val response = newsRepository.getBreakingNews(countryCode, breakingNewsPage)
breakingNews.postValue(handleBreakingNewsResponse(response))
}
fun getUsBreakingNews(countryCode: String) = viewModelScope.launch {
usBreakingNews.postValue(Resource.Loading())
val response = newsRepository.getBreakingNews(countryCode, breakingNewsPage)
usBreakingNews.postValue(handleBreakingNewsResponse(response))
}
BreakingNewsFragment.kt
if(shouldPaginate){
viewModel.getUsBreakingNews("tr")
isScrooling=false
}
控制
创建片段并将其附加到 activity 后,您可以检查 activity 它附加到什么。
在您的片段中,将其放入 onCreate()
if(activity!! is NewsActivity){
//do something
}