我正在获取有关某些神奇宝贝的数据,并且还希望获取它们的图像并在同一活动中滑行。无法直接获取图像,我必须从神奇宝贝url对象获取图像的url。

我的意思是,我必须先从响应中获取https://pokeapi.co/api/v2/limit?=10的宠物小精灵,然后才能通过id https://pokeapi.co/api/v2/pokemon/1获取每个宠物小精灵的图片网址

我能够在适配器中成功完成此操作,但想知道这是否是最佳实践,因为如果数量更大,我担心它会滞后

适配器在下方

class ApiAdapter (internal var activity: MainActivity):RecyclerView.Adapter<ApiAdapter.ApiViewHolder>(){

    private val queue = Volley.newRequestQueue(activity)


    init{

        loadData()
    }
    val dataInstance = ArrayList<DataClass>()
    private fun loadData(){

        val url = "https://pokeapi.co/api/v2/pokemon?limit=5"

        val jsonObjectRequest = JsonObjectRequest(
            Request.Method.GET, url, null,
            Response.Listener { response ->

                try {
                    val jsonArray = response.getJSONArray("results")
                    lateinit var name:String
                    lateinit var pokeUrl: String
                    lateinit var jsonCities: JSONArray
                    for(i in 0 until jsonArray.length()){
                        val jsonObject = jsonArray.getJSONObject(i)
                        name = jsonObject.getString("name")
                        pokeUrl = jsonObject.getString("url")


                        dataInstance.add(DataClass(name, pokeUrl))

                    }


                    notifyDataSetChanged()

                }
                catch (error: JSONException){
                    error.printStackTrace()
                }


            },
            Response.ErrorListener { error ->
                // TODO: Handle error

                Log.e("Api", "error: $error")

            }

        )
        // Add the request to the RequestQueue.
        queue.add(jsonObjectRequest)
    }




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

    override fun onBindViewHolder(holder:ApiViewHolder, position:Int){

        val current = dataInstance.get(position)

        holder.name.text = current.name


        fun loadImageData(){

            val url = current.url

            val jsonObjectRequest = JsonObjectRequest(
                Request.Method.GET, url, null,
                Response.Listener { response ->

                    try {
                        val jsonResponse = response.getJSONObject("sprites")
                        val picUrl = jsonResponse.optString("front_default")

                        Glide.with(activity).load(picUrl).into(holder.image)

//                        notifyDataSetChanged()
                        Toast.makeText(activity, "res: $picUrl", Toast.LENGTH_LONG).show()
                    }
                    catch (error: JSONException){
                        error.printStackTrace()
                    }

                },
                Response.ErrorListener { error ->


                    Log.e("Api", "error: $error")

                }

            )
            // Add the request to the RequestQueue.
            queue.add(jsonObjectRequest)
        }

        loadImageData()


    }

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

    class ApiViewHolder(dataView: View): RecyclerView.ViewHolder(dataView){


        internal var name = dataView.findViewById<TextView>(R.id.name)
        internal var image = dataView.findViewById<ImageView>(R.id.pokemon_image)

    }
}


也许有更好的方法来解决此问题。您的建议将不胜感激

最佳答案

一些一般注意事项和建议:


您正在从onBindViewHolder触发API调用。回收后每次绑定视图时,都会触发该事件。您将需要将其从适配器中拉出,或实施一些缓存。通常,不要在没有缓存的情况下将API放在适配器中,这是灾难的根源。
您正在从适配器内调用notifyDatasetChanged()。这通常也被认为是不良做法。工作流应该是适配器仅接收视图状态数据,然后通过外部notifyDatasetChanged将视图状态数据应用于其子级。


但是,根据您的具体情况,您实际上根本不需要额外的API调用来获得神奇宝贝的形象。 PokeAPI中的所有神奇宝贝都使用相同的图像URL格式,例如:
https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/4.png

如果仅用神奇宝贝的ID替换该URL中的数字4,则可以避免进行额外的API调用,而只需将该手动URL直接传递给适配器即可。然后在完整详细信息活动或片段中获取完整的神奇宝贝对象。
 只是一个想法。

10-07 19:30
查看更多