滑动以刷新以更新数据

时间:2017-10-16 16:54:20

标签: android kotlin

我有一个recyclelerview,我想在用户滑动刷新时更新。

这是我的MainActity.kt

class MainActivity : AppCompatActivity(), LoaderManager.LoaderCallbacks<List<Coin>> {

private val mCoinLoaderId = 1
private val mCoinEndpointUrl = "http://coincap.io/front"
private var swipeRefreshLayout: SwipeRefreshLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    swipeRefreshLayout=  findViewById<SwipeRefreshLayout>(R.id.swipe_refresh_layout) as SwipeRefreshLayout

    rv_coins.layoutManager = LinearLayoutManager(this)
    rv_coins.setHasFixedSize(true)
    runLoaders()


    swipeRefreshLayout?.setOnRefreshListener{


                // This method performs the actual data-refresh operation.
                // The method calls setRefreshing(false) when it's finished.
                runLoaders()
            }



}

private fun runLoaders() {

    val connManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val networkInfo = connManager.activeNetworkInfo
    if (networkInfo != null && networkInfo.isConnected) {
        loaderManager.initLoader(mCoinLoaderId, null , this)
    } else {
        Toast.makeText(this, "No Internet connection", Toast.LENGTH_SHORT).show()
    }
}


override fun onCreateLoader(id: Int, args: Bundle?): Loader<List<Coin>> {
    val baseUri: Uri = Uri.parse(mCoinEndpointUrl)


    return object : AsyncTaskLoader<List<Coin>>(this) {
        override fun onStartLoading() {
            forceLoad()
        }

        override fun loadInBackground(): List<Coin>? {
            return QueryUtils.fetchCoinData(baseUri.toString())
        }
    }
}

override fun onLoadFinished(loader: Loader<List<Coin>>?, data: List<Coin>?) {
    if (data != null && !data.isEmpty()) {

        progressBar1.isEnabled = false
        if(swipeRefreshLayout?.isRefreshing!!)  {
            swipeRefreshLayout?.isRefreshing = false
        }

        rv_coins.adapter = CoinAdapter(data)
    }



}

override fun onLoaderReset(loader: Loader<List<Coin>>?) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

我有以下适配器

class CoinAdapter(private val mCoinData: List<Coin>): RecyclerView.Adapter<CoinAdapter.CoinViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): CoinViewHolder {
    val inflater = LayoutInflater.from(parent?.context)
    return CoinViewHolder(inflater.inflate(R.layout.list_item, parent, false))
}

override fun onBindViewHolder(holder: CoinViewHolder?, position: Int) {
    holder?.mCoinShortNameTV?.text = mCoinData[position].short
    holder?.mCoinLongNameTV?.text = mCoinData[position].longname
    holder?.mCoinPercentageTV?.text = """${mCoinData[position].perc.toString()}%"""
    if (mCoinData[position].perc < 0.0) {
        holder?.mCoinPercentageTV?.setTextColor(Color.parseColor("#F73757"))
    } else {
        holder?.mCoinPercentageTV?.setTextColor(Color.parseColor("#21BD2E"))
    }
}

override fun getItemCount(): Int {
    return mCoinData.size
}


inner class CoinViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
    val mCoinLongNameTV: TextView = itemView.findViewById<TextView>(R.id.long_name) as TextView
    val mCoinPercentageTV: TextView = itemView.findViewById<TextView>(R.id.percentage) as TextView
    val mCoinShortNameTV: TextView = itemView.findViewById<TextView>(R.id.short_name) as TextView
}

但即使我在用户滑动刷新时调用runLoaders()函数,它也不会更新数据。它甚至没有调用fetch函数来更新数据。

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

只需使用

loaderManager.initLoader(mCoinLoaderId, null, this).forceLoad()

而不是

loaderManager.initLoader(mCoinLoaderId, null, this)