在RecyclerView中调用Google Places API [英] Calling Google Places API in a RecyclerView

查看:42
本文介绍了在RecyclerView中调用Google Places API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView,其适配器使用Google Places API获取有关位置ID的详细信息.然后,适配器在RecyclerView的每个元素中显示这些详细信息(例如,地点地址).

I have a RecyclerView whose adapter fetches details about a place ID using the Google Places API. The adapter then displays those details (eg. place address) in each element of the RecyclerView.

是否存在建议的异步方式?在我当前的解决方案中,当用户向上或向下滚动时,地方地址有时会显示在错误的RecyclerView元素中.同样,在点击RecyclerView元素以进入详细信息屏幕,然后返回到原始屏幕时,RecyclerView中缺少一些地方地址(大概在从Places API提取详细信息时会有延迟)

Is there a recommended way to achieve this asynchronously? In my current solution, the place addresses are sometimes displayed in the incorrect RecyclerView element when the user scrolls up or down. Also when tapping on a RecyclerView element to go to the detail screen, then going back to the original screen, some of the place addresses are missing in the RecyclerView (presumably there is a delay when the details are still being fetched from the Places API).

public void onBindViewHolder(final ViewHolder v, int position) {
    Places.GeoDataApi.getPlaceById(googleApiClient, placeID)
            .setResultCallback(new ResultCallback<PlaceBuffer>() {
                @Override
                public void onResult(PlaceBuffer places) {
                    Place place = places.get(0);

                    // Display address in the RecyclerView element
                    v.address.setText(place.getName());
                }
     }

推荐答案

不要在 onbindviewholder 方法中点击api.因为 onbindviewholder 方法每次滚动 recyclerview 时都会执行.

Dont hit api in onbindviewholder method. Because onbindviewholder method execute every time when you scroll recyclerview.

如果您先获取活动或片段中的数据,然后将其存储在arraylist中并传递给 recyclerview 适配器,则效果会更好.

This will be better if you fetch data in activity or fragment and then , store that data in arraylist and pass to recyclerview adapter.

并且拖移项目位置的问题是由于 recyclerview 回收滚动条上的每个项目.

And problem of shuffling item position is due to recyclerview recycle every item on scroll.

因此,在您的 onbindviewholder 方法中,将此属性设置为 viewHolder

So in your onbindviewholder method, set this property to viewHolder

viewHolder.setIsRecyclable(false);

这篇关于在RecyclerView中调用Google Places API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆