我有一個片段,我想在應用程式中有兩次,但略有變化。有沒有辦法使用某種抽象類?我已經嘗試自己找到解決方案,但是我找不到Viewmodel使用委托屬性從 Activity獲取Activity 的方法,因為 Android Studio 表示不能抽象。我面臨的另一個問題是我傳遞給 Fragment 的引數。
(澄清:應該更改的屬性是 Viewmodel;我想為第二個 Fragment 使用另一種 Viewmodel。另外,我在下面的代碼中使用的 Viewmodel 和另一個 Viewmodel 都繼承自同一個類,所以切換型別應該不是什么大問題)
這是片段的代碼:
package net.informatikag.thomapp.viewables.fragments.ThomsLine.main
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.android.volley.*
import com.android.volley.toolbox.JsonArrayRequest
import com.android.volley.toolbox.Volley
import com.google.android.material.snackbar.Snackbar
import net.informatikag.thomapp.MainActivity
import net.informatikag.thomapp.R
import net.informatikag.thomapp.databinding.ThomslineMainFragmentBinding
import net.informatikag.thomapp.utils.handlers.WordpressRecyclerAdapter
import net.informatikag.thomapp.utils.ArticleListSpacingDecoration
import net.informatikag.thomapp.utils.models.ArticleClickHandler
import net.informatikag.thomapp.utils.models.data.WordpressArticle
import net.informatikag.thomapp.utils.models.data.WordpressPage
import net.informatikag.thomapp.utils.models.view.ThomsLineViewModel
import java.util.*
import kotlin.collections.ArrayList
/**
* Pulls a list of articles from the JSON API of the Wordpress instance of the ThomsLine student newspaper.
* The articles are dynamically loaded with a RecyclerView.
*/
class ThomsLineFragment : Fragment(), SwipeRefreshLayout.OnRefreshListener, ArticleClickHandler {
private var _binding: ThomslineMainFragmentBinding? = null // Verweis zum Layout
private val viewModel: ThomsLineViewModel by activityViewModels() // Das Viewmodel in dem die wichtigen Daten des Fragments gespeichert werden
private lateinit var recyclerAdapter: WordpressRecyclerAdapter // Hier werden die Artikel angezeigt
private lateinit var swipeRefreshLayout: SwipeRefreshLayout // wird benutz um die Artikel neu zu laden
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
/**
* Will be executed when the fragment is opened
*/
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate Layout
_binding = ThomslineMainFragmentBinding.inflate(inflater, container, false)
val root: View = binding.root
//Instantiate Variables
recyclerAdapter = WordpressRecyclerAdapter(this, viewModel)
//Add Observer to articles to update Recyclerview
viewModel.articles.observe(viewLifecycleOwner, Observer {
swipeRefreshLayout.isRefreshing = false
})
//region Init SwipeRefresh Layout
swipeRefreshLayout = root.findViewById(R.id.thomsline_swipe_container)
swipeRefreshLayout.setOnRefreshListener(this)
swipeRefreshLayout.setColorSchemeResources(
R.color.primaryColor,
R.color.secondaryColor
)
if(viewModel.isEmpty()) {
swipeRefreshLayout.post {
// Display Refresh Indicator
swipeRefreshLayout.isRefreshing = true
// Load First Article Page
loadArticles(0, true)
}
}
//endregion
//region Init Recycler View
_binding?.thomslineRecyclerView?.apply {
layoutManager = LinearLayoutManager(this@ThomsLineFragment.context)
addItemDecoration(ArticleListSpacingDecoration())
adapter = recyclerAdapter
}
//endregion
return root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
/**
* Called when the SwipeRefresh Layout is triggerd
*/
override fun onRefresh() {
loadArticles(0, true)
}
/**
* Loads all Article pages until "page" and removes all cached pages after it
*/
fun loadArticles(page:Int, reloadAll: Boolean){
// Remove all cached pages after the given one
if(page == 0) {
viewModel.removeArticlePagesFromIndex(1, recyclerAdapter)
viewModel.lastPage = -1
}
// Create a new Request Queue
val requestQueue = Volley.newRequestQueue(this.context)
// Add requests to load the Pages to the requestQueue
if(reloadAll)
for (i in 0 until page 1) {
reloadPage(i, requestQueue)
}
else reloadPage(page)
}
// Reload a page without a given Request Queue
fun reloadPage(id:Int){
reloadPage(id, Volley.newRequestQueue(this.context))
}
// Reload a Page while adding the Requests to a given Request Queue
fun reloadPage(id: Int, requestQueue:RequestQueue) {
Log.d("ThomsLine", "Requesting Data for page $id")
// Start the Request
requestQueue.add(JsonArrayRequest(viewModel.BASE_URL MainActivity.WORDPRESS_BASE_URL_LITE "&&page=${id 1}",
{ response ->
Log.d("ThomsLine", "Got Data for page $id")
// A Variable to load the Articles to
val data = ArrayList<WordpressArticle>()
// Load the Articles from the JSON
for (j in 0 until response.length()) data.add(WordpressArticle(response.getJSONObject(j), true, viewModel.BASE_URL))
// Update the RecyclerView
viewModel.setArticlePage(id, WordpressPage(data.toTypedArray()), recyclerAdapter)
},
{ volleyError ->
Log.d("ThomsLine", "Request Error while loading Data for page $id")
// Check if the Error is caused because loading a non Existing Page
if (volleyError.networkResponse?.statusCode == 400){
// Update the Last Page Variable
viewModel.lastPage = if(id-1<viewModel.lastPage) viewModel.lastPage else id-1
recyclerAdapter.notifyItemChanged(recyclerAdapter.itemCount-1)
Log.d("ThomsLine", "Page does not exist (last page: ${viewModel.lastPage})")
} else {
Log.d("ThomsLine", "Request failed: ${volleyError.message.toString()}")
// Display a Snackbar, stating the Error
Snackbar.make(requireActivity().findViewById(R.id.app_bar_main), WordpressArticle.getVolleyError(volleyError, requireActivity()), Snackbar.LENGTH_LONG).show()
}
//recyclerAdapter.notifyItemChanged(id)
}
))
}
/**
* Called when a Article is clicked
*/
override fun onItemClick(wordpressArticle: WordpressArticle) {
val action = ThomsLineFragmentDirections.actionNavThomslineToNavThomslineArticleView(wordpressArticle.id)
findNavController().navigate(action)
}
}
uj5u.com熱心網友回復:
您不在ViewModel您的抽象類中分配 the (這是您通過使用委托所做的,by activityViewModels())。只需在您的抽象類中將其設定為抽象屬性,然后您的具體 Fragment 子類將需要為其分配一個值(by activityViewModels()如果您愿意,可以使用):
abstract class BaseFragment : Fragment() {
...
abstract val viewModel: BaseViewModel
...
}
class RealFragment : BaseFragment() {
...
// you can specify a subtype of BaseViewModel in your implementation
override val viewModel: SomeViewModel by activityViewModels()
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/391792.html
上一篇:將資料從活動傳遞到androidstudiokotlin中的片段(不重復,這些方法不起作用)
下一篇:使用navArgs將Fragment轉換為DialogFragment,如何從Fragment類外部導航到DialogFragment
