2017-10-16 164 views
0

我有一個recyclerview,我想在用戶刷新刷新時進行更新。刷卡刷新以更新數據

這是我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當用戶刷卡刷新()函數,它不是更新數據。它甚至不會調用獲取函數來更新數據。

我的代碼有什麼問題?

+0

你打電話initLoader,它會運行一次,嘗試將其更改爲restartLoader,所以它會每次運行時調用 –

回答

0

只需使用

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

,而不是

loaderManager.initLoader(mCoinLoaderId, null, this)