AsyncTaskLoader返回旧值,而不是新值

时间:2018-08-03 06:59:07

标签: android android-fragments url android-recyclerview asynctaskloader

我有以下问题。我的应用程序将两个片段(FragmentA,FragmentB)托管到一个活动中。我正在尝试从URL获取数据并将它们显示在recyclerview的fragmentA中。我还在FragmentA中实现了AsyntaskLoader,以从url中获取数据

问题是,我正在尝试实现刷新按钮,该按钮将从url加载新数据,但它始终返回已加载的数据。仅当我转到片段B a然后又回到片段A时,数据才会更新。

这是我的装载者

class EventLoader(上下文:上下文,私有值url:字符串?,私有值httpRequestBuilder:HttpRequestBuilder):AsyncTaskLoader>(上下文){

override fun loadInBackground(): ArrayList<Event>? {

    return url?.let { httpRequestBuilder.fetchCountlyData(it) }
}

override fun onStartLoading() {
    forceLoad()
}

}

这是我的片段

class EventFragment : Fragment(), LoaderManager.LoaderCallbacks<ArrayList<Event>> {

private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private var viewManager = LinearLayoutManager(activity)
private lateinit var stateText: TextView
private lateinit var loadingIndicator: ProgressBar
private lateinit var refreshIcon: ImageButton
private var initList = arrayListOf<Event>()
private val url = "https://appanalytics-test.wirecard.com/o?method=user_details&uid=b37ed2fcb1647fd94fa3a835b9c34ed0c52c7438&api_key=15fc695e630e404206b5ce7db37d3029&app_id=5b153d2d0b2f2605440f8a5f"
private val httpRequestBuilder = HttpRequestBuilder()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {

    val view = inflater.inflate(R.layout.fragment_event, container, false)
    refreshIcon = activity!!.findViewById(R.id.refresh_toolbar_button)
    stateText = view.findViewById(R.id.empty_view)
    viewAdapter = ItemAdapter(initList, activity!!.baseContext)
    loadingIndicator = view.findViewById(R.id.loading_indicator)

    val connectManager = activity!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val networkInfo = connectManager.activeNetworkInfo

    if (networkInfo != null && networkInfo.isConnected) {
        loaderManager.initLoader(1, null, this as LoaderManager.LoaderCallbacks<ArrayList<Event>>)
    } else {
        loadingIndicator.visibility = View.GONE
        stateText.text = getString(R.string.no_internet)
    }

    refreshIcon.setOnClickListener {
        loaderManager.restartLoader(1, null, this as LoaderManager.LoaderCallbacks<ArrayList<Event>>)
    }

    implementRecyclerView(view)

    return view
}

override fun onCreateLoader(id: Int, args: Bundle?): Loader<ArrayList<Event>> {
    initList.clear()
    viewAdapter.notifyDataSetChanged()
    loadingIndicator.visibility = View.VISIBLE
    stateText.visibility = View.GONE

    return EventLoader(context!!, url, httpRequestBuilder)
}

override fun onLoadFinished(loader: Loader<ArrayList<Event>>, events: ArrayList<Event>?) {
    stateText.text = getString(R.string.empty_list)
    loadingIndicator.visibility = View.GONE

    if (events != null && !events.isEmpty()) {
        initList.addAll(events)
        viewAdapter.notifyDataSetChanged()
        stateText.visibility = View.GONE
    } else {
        stateText.visibility = View.VISIBLE
    }
}

override fun onLoaderReset(loader: Loader<ArrayList<Event>>) {

}

private fun implementRecyclerView(view: View) {
    recyclerView = view.findViewById<RecyclerView>(R.id.recycler_view).apply {
        layoutManager = viewManager
        adapter = viewAdapter
    }
}

}

0 个答案:

没有答案