Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 启动时不会显示RecyclerView中的图像_Android_Android Recyclerview_Android Volley_Adapter_Android Viewholder - Fatal编程技术网

Android 启动时不会显示RecyclerView中的图像

Android 启动时不会显示RecyclerView中的图像,android,android-recyclerview,android-volley,adapter,android-viewholder,Android,Android Recyclerview,Android Volley,Adapter,Android Viewholder,我有一个RecyclerView,里面有一张卡片列表,每张卡片上都有一张图片。当我开始向下滚动时,屏幕外开始的卡在屏幕上出现时会加载图像,但屏幕上开始的卡只有在移离屏幕并返回屏幕时才会加载图像 它似乎也有点随机性。用于上下文的Gif: gif中的最后一个示例(前3个餐厅图像不加载)是最常见的 视图持有者: public class LocationsViewHolder extends RecyclerView.ViewHolder{ private ImageView locationIma

我有一个
RecyclerView
,里面有一张卡片列表,每张卡片上都有一张图片。当我开始向下滚动时,屏幕外开始的卡在屏幕上出现时会加载图像,但屏幕上开始的卡只有在移离屏幕并返回屏幕时才会加载图像

它似乎也有点随机性。用于上下文的Gif:

gif中的最后一个示例(前3个餐厅图像不加载)是最常见的

视图持有者:

public class LocationsViewHolder extends RecyclerView.ViewHolder{

private ImageView locationImage;
private TextView locationName;
private TextView locationAddress;

public LocationsViewHolder(View itemView) {
    super(itemView);

    locationName = (TextView)itemView.findViewById(R.id.location_name);
    locationAddress = (TextView)itemView.findViewById(R.id.location_address);
    locationImage = (ImageView) itemView.findViewById(R.id.location_image);
}

public void updateUI(Place place){
    locationName.setText(place.getName());
    if(place.getPhotoID() != null){
        // Log.v("IMAGE", "photoID for " + place.getName() + ": " + place.getPhotoID());
        locationImage.setImageBitmap(place.getImage());
    } else {
        locationImage.setImageResource(R.drawable.no_image);
    }
}

}
public class LocationsAdapter extends RecyclerView.Adapter<LocationsViewHolder>{

ArrayList<Place> locations;

Context context;

GetNearbyPlacesData data;

public LocationsAdapter(ArrayList<Place> locations, Context context) {
    this.locations = locations;
    this.context = context;
    data = GetNearbyPlacesData.getInstance(context);
}

@Override
public LocationsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View card = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_location, parent, false);
    return new LocationsViewHolder(card);
}

@Override
public void onBindViewHolder(LocationsViewHolder holder, int position) {
    Place location = locations.get(position);
    holder.updateUI(location);
}

@Override
public int getItemCount() {
    return locations.size();
}

}
    Bitmap bitmap;

public void downloadPlaceImage(String imageKey, final Place place){
    String url = buildImgURL(imageKey);

    ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            bitmap = response;
            place.setImage(bitmap);
            Log.v("IMG", "downloaded image");
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    requestQueue.add(imageRequest);
}
}
ArrayList<Place> placesList;

public ArrayList<Place> downloadPlacesData(Bundle bundle){
    String url = buildPlaceURL(bundle);

    placesList = new ArrayList<>();

    //TODO make more requests for the next pages of results since each page is limited to 20 places

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray results = response.getJSONArray("results");
                for(int i = 0; i < results.length(); i++){
                    JSONObject main = results.getJSONObject(i);
                    JSONObject geo = main.getJSONObject("geometry");
                    JSONObject loc = geo.getJSONObject("location");
                    String placeID = main.getString("place_id");
                    String name = main.getString("name");
                    String lat = loc.getString("lat");
                    String lng = loc.getString("lng");
                    String photoID = null;

                    try {
                        if (main.getJSONArray("photos") != null) {
                            JSONArray photos = main.getJSONArray("photos");
                            photoID = photos.getJSONObject(0).getString("photo_reference");
                        }
                    }catch (JSONException e){
                        Log.v("JSON4", e.getLocalizedMessage());
                    }

                    Place place = new Place(photoID, name, placeID, Double.parseDouble(lat), Double.parseDouble(lng), context);
                    Log.v("NAME", name);

                    placesList.add(place);

                }
                onPlacesLoadedListener.placesLoaded(placesList);

            }catch (JSONException e){
                Log.v("JSON", e.getLocalizedMessage());
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    requestQueue.add(jsonObjectRequest);

    return placesList;
}
@Override
public void placesLoaded(ArrayList<Place> list) {
    Log.v("PLACES", "adding places");
    MarkerOptions markerOptions = new MarkerOptions();

    // build a resource path using the type of place
    String type = URLInfo.getString("type");
    String resourcePath = "drawable/pin_" + type;

    // create a resource path out of the String created
    int resource = getResources().getIdentifier(resourcePath, null, this.getPackageName());

    // set the marker icon according to the place type
    markerOptions.icon(BitmapDescriptorFactory.fromResource(resource));

    for(int i = 0; i < list.size(); i++){
        // get the latitude and longitude
        double lat = list.get(i).getLat();
        double lng = list.get(i).getLng();
        // set the marker position and title
        markerOptions.position(new LatLng(lat, lng));
        markerOptions.title(list.get(i).getName());
        mMap.addMarker(markerOptions);
    }
    createLocationsListFragment();

}

private void createLocationsListFragment() {
    FragmentManager fm = getSupportFragmentManager();
    LocationsListFragment fragment = LocationsListFragment.newInstance();
    fm.beginTransaction().add(R.id.container_locations, fragment).commit();
}
适配器:

public class LocationsViewHolder extends RecyclerView.ViewHolder{

private ImageView locationImage;
private TextView locationName;
private TextView locationAddress;

public LocationsViewHolder(View itemView) {
    super(itemView);

    locationName = (TextView)itemView.findViewById(R.id.location_name);
    locationAddress = (TextView)itemView.findViewById(R.id.location_address);
    locationImage = (ImageView) itemView.findViewById(R.id.location_image);
}

public void updateUI(Place place){
    locationName.setText(place.getName());
    if(place.getPhotoID() != null){
        // Log.v("IMAGE", "photoID for " + place.getName() + ": " + place.getPhotoID());
        locationImage.setImageBitmap(place.getImage());
    } else {
        locationImage.setImageResource(R.drawable.no_image);
    }
}

}
public class LocationsAdapter extends RecyclerView.Adapter<LocationsViewHolder>{

ArrayList<Place> locations;

Context context;

GetNearbyPlacesData data;

public LocationsAdapter(ArrayList<Place> locations, Context context) {
    this.locations = locations;
    this.context = context;
    data = GetNearbyPlacesData.getInstance(context);
}

@Override
public LocationsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View card = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_location, parent, false);
    return new LocationsViewHolder(card);
}

@Override
public void onBindViewHolder(LocationsViewHolder holder, int position) {
    Place location = locations.get(position);
    holder.updateUI(location);
}

@Override
public int getItemCount() {
    return locations.size();
}

}
    Bitmap bitmap;

public void downloadPlaceImage(String imageKey, final Place place){
    String url = buildImgURL(imageKey);

    ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            bitmap = response;
            place.setImage(bitmap);
            Log.v("IMG", "downloaded image");
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    requestQueue.add(imageRequest);
}
}
ArrayList<Place> placesList;

public ArrayList<Place> downloadPlacesData(Bundle bundle){
    String url = buildPlaceURL(bundle);

    placesList = new ArrayList<>();

    //TODO make more requests for the next pages of results since each page is limited to 20 places

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray results = response.getJSONArray("results");
                for(int i = 0; i < results.length(); i++){
                    JSONObject main = results.getJSONObject(i);
                    JSONObject geo = main.getJSONObject("geometry");
                    JSONObject loc = geo.getJSONObject("location");
                    String placeID = main.getString("place_id");
                    String name = main.getString("name");
                    String lat = loc.getString("lat");
                    String lng = loc.getString("lng");
                    String photoID = null;

                    try {
                        if (main.getJSONArray("photos") != null) {
                            JSONArray photos = main.getJSONArray("photos");
                            photoID = photos.getJSONObject(0).getString("photo_reference");
                        }
                    }catch (JSONException e){
                        Log.v("JSON4", e.getLocalizedMessage());
                    }

                    Place place = new Place(photoID, name, placeID, Double.parseDouble(lat), Double.parseDouble(lng), context);
                    Log.v("NAME", name);

                    placesList.add(place);

                }
                onPlacesLoadedListener.placesLoaded(placesList);

            }catch (JSONException e){
                Log.v("JSON", e.getLocalizedMessage());
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    requestQueue.add(jsonObjectRequest);

    return placesList;
}
@Override
public void placesLoaded(ArrayList<Place> list) {
    Log.v("PLACES", "adding places");
    MarkerOptions markerOptions = new MarkerOptions();

    // build a resource path using the type of place
    String type = URLInfo.getString("type");
    String resourcePath = "drawable/pin_" + type;

    // create a resource path out of the String created
    int resource = getResources().getIdentifier(resourcePath, null, this.getPackageName());

    // set the marker icon according to the place type
    markerOptions.icon(BitmapDescriptorFactory.fromResource(resource));

    for(int i = 0; i < list.size(); i++){
        // get the latitude and longitude
        double lat = list.get(i).getLat();
        double lng = list.get(i).getLng();
        // set the marker position and title
        markerOptions.position(new LatLng(lat, lng));
        markerOptions.title(list.get(i).getName());
        mMap.addMarker(markerOptions);
    }
    createLocationsListFragment();

}

private void createLocationsListFragment() {
    FragmentManager fm = getSupportFragmentManager();
    LocationsListFragment fragment = LocationsListFragment.newInstance();
    fm.beginTransaction().add(R.id.container_locations, fragment).commit();
}
下载PlacesData()函数:

public class LocationsViewHolder extends RecyclerView.ViewHolder{

private ImageView locationImage;
private TextView locationName;
private TextView locationAddress;

public LocationsViewHolder(View itemView) {
    super(itemView);

    locationName = (TextView)itemView.findViewById(R.id.location_name);
    locationAddress = (TextView)itemView.findViewById(R.id.location_address);
    locationImage = (ImageView) itemView.findViewById(R.id.location_image);
}

public void updateUI(Place place){
    locationName.setText(place.getName());
    if(place.getPhotoID() != null){
        // Log.v("IMAGE", "photoID for " + place.getName() + ": " + place.getPhotoID());
        locationImage.setImageBitmap(place.getImage());
    } else {
        locationImage.setImageResource(R.drawable.no_image);
    }
}

}
public class LocationsAdapter extends RecyclerView.Adapter<LocationsViewHolder>{

ArrayList<Place> locations;

Context context;

GetNearbyPlacesData data;

public LocationsAdapter(ArrayList<Place> locations, Context context) {
    this.locations = locations;
    this.context = context;
    data = GetNearbyPlacesData.getInstance(context);
}

@Override
public LocationsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View card = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_location, parent, false);
    return new LocationsViewHolder(card);
}

@Override
public void onBindViewHolder(LocationsViewHolder holder, int position) {
    Place location = locations.get(position);
    holder.updateUI(location);
}

@Override
public int getItemCount() {
    return locations.size();
}

}
    Bitmap bitmap;

public void downloadPlaceImage(String imageKey, final Place place){
    String url = buildImgURL(imageKey);

    ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            bitmap = response;
            place.setImage(bitmap);
            Log.v("IMG", "downloaded image");
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    requestQueue.add(imageRequest);
}
}
ArrayList<Place> placesList;

public ArrayList<Place> downloadPlacesData(Bundle bundle){
    String url = buildPlaceURL(bundle);

    placesList = new ArrayList<>();

    //TODO make more requests for the next pages of results since each page is limited to 20 places

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray results = response.getJSONArray("results");
                for(int i = 0; i < results.length(); i++){
                    JSONObject main = results.getJSONObject(i);
                    JSONObject geo = main.getJSONObject("geometry");
                    JSONObject loc = geo.getJSONObject("location");
                    String placeID = main.getString("place_id");
                    String name = main.getString("name");
                    String lat = loc.getString("lat");
                    String lng = loc.getString("lng");
                    String photoID = null;

                    try {
                        if (main.getJSONArray("photos") != null) {
                            JSONArray photos = main.getJSONArray("photos");
                            photoID = photos.getJSONObject(0).getString("photo_reference");
                        }
                    }catch (JSONException e){
                        Log.v("JSON4", e.getLocalizedMessage());
                    }

                    Place place = new Place(photoID, name, placeID, Double.parseDouble(lat), Double.parseDouble(lng), context);
                    Log.v("NAME", name);

                    placesList.add(place);

                }
                onPlacesLoadedListener.placesLoaded(placesList);

            }catch (JSONException e){
                Log.v("JSON", e.getLocalizedMessage());
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    requestQueue.add(jsonObjectRequest);

    return placesList;
}
@Override
public void placesLoaded(ArrayList<Place> list) {
    Log.v("PLACES", "adding places");
    MarkerOptions markerOptions = new MarkerOptions();

    // build a resource path using the type of place
    String type = URLInfo.getString("type");
    String resourcePath = "drawable/pin_" + type;

    // create a resource path out of the String created
    int resource = getResources().getIdentifier(resourcePath, null, this.getPackageName());

    // set the marker icon according to the place type
    markerOptions.icon(BitmapDescriptorFactory.fromResource(resource));

    for(int i = 0; i < list.size(); i++){
        // get the latitude and longitude
        double lat = list.get(i).getLat();
        double lng = list.get(i).getLng();
        // set the marker position and title
        markerOptions.position(new LatLng(lat, lng));
        markerOptions.title(list.get(i).getName());
        mMap.addMarker(markerOptions);
    }
    createLocationsListFragment();

}

private void createLocationsListFragment() {
    FragmentManager fm = getSupportFragmentManager();
    LocationsListFragment fragment = LocationsListFragment.newInstance();
    fm.beginTransaction().add(R.id.container_locations, fragment).commit();
}
最后是实际片段(onCreate和onCreateView):


如果您有图像的url或位图,则使用glide,因为它将缓存您的图像。如果您发送url并加载它,则进行网络调用



在RecyclerView中,您必须确保为每个项目返回唯一的Id,或者如果没有稳定的Id,则应返回RecyclerView.no_Id(-1)。请避免返回不稳定的值。(稳定值是唯一的值,即使数据集的位置发生变化也不会改变)

在RecyclerView适配器中添加getItemId回调

例如:


(在您的示例视频中)在餐厅和药店之间切换时执行什么代码?例如,适配器的数据是如何更新的?当选择其中一个项目时,它会下载附近的位置数据,类似于图像下载(JSON和截图)编辑:我会将此添加到主帖子中