Android 重复RecyclerView中其他项目的图片

Android 重复RecyclerView中其他项目的图片,android,android-fragments,android-recyclerview,android-adapter,android-adapterview,Android,Android Fragments,Android Recyclerview,Android Adapter,Android Adapterview,首先,请原谅,我的英语不好 我有一个包含9个项目的RecyclerView,但是从第四个项目到下一个项目,前4个项目的图片会在其他项目中重复! 但RecyclerView中所有项目的其他字段已正确完成 如何解决这个问题 这是我的代码: 我的型号: public class LocationsListModel { final String DRAWABLE = "drawable/"; private String name,imageUrl, review, price; public L

首先,请原谅,我的英语不好

我有一个包含9个项目的RecyclerView,但是从第四个项目到下一个项目,前4个项目的图片会在其他项目中重复! 但RecyclerView中所有项目的其他字段已正确完成

如何解决这个问题

这是我的代码:

我的型号:

public class LocationsListModel {

final String DRAWABLE = "drawable/";
private String name,imageUrl, review, price;

public LocationsListModel(String name, String review, String price, String imageUrl) {
    this.name = name;
    this.review = review;
    this.price = price;
    this.imageUrl = imageUrl;
}

public String getName() {
    return name;
}
public String getReview() {
    return review;
}
public String getPrice() {
    return price;
}
public String getImageUrl() {
    return DRAWABLE + imageUrl;
}}
public class LocationsListAdapter extends RecyclerView.Adapter<LocationsListAdapter.LocationsListView> {

private static ArrayList<LocationsListModel> locationList;
private Context context;
private ImageView locationImage;


public void setItems(ArrayList<LocationsListModel> items) {
    this.locationList = items;
}


public class LocationsListView extends RecyclerView.ViewHolder {

    public CardView cardView;
    public TextView locationName;
    public TextView review;
    public TextView locationPrice;


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

        cardView = itemView.findViewById(R.id.cardView);

        locationName = itemView.findViewById(R.id.location_name);
        review = itemView.findViewById(R.id.review);
        locationPrice = itemView.findViewById(R.id.price);
        locationImage = itemView.findViewById(R.id.imageView2);
    }
}

@Override
public LocationsListAdapter.LocationsListView onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.locations_list_item, parent, false);

    context = parent.getContext();

    return new LocationsListView(view);
}

@Override
public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {
    final LocationsListModel locationsListModel = locationList.get(position);


    String uri = locationsListModel.getImageUrl();
    int resource = locationImage.getResources().getIdentifier(uri, null, locationImage.getContext().getPackageName());
    locationImage.setImageResource(resource);

    holder.locationName.setText(locationsListModel.getName());
    holder.review.setText(locationsListModel.getReview());
    holder.locationPrice.setText(locationsListModel.getPrice());

}

@Override
public int getItemCount() {
    return locationList.size();
}}
public class LocationsListFragment extends Fragment {

private final LocationsListAdapter locationsListAdapter = new LocationsListAdapter();
private RecyclerView locationsList;
SimpleRatingBar ratingBar;
ArrayList<LocationsListModel> locationsListModels = new ArrayList<>();


public LocationsListFragment() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static LocationsListFragment newInstance() {
    LocationsListFragment fragment = new LocationsListFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_locations_list, container, false);

    ratingBar = view.findViewById(R.id.ratingBar);
    locationsList = view.findViewById(R.id.locations_lis);

    setRecyclerViewItems();

    return view;
}

private void setRecyclerViewItems() {

    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(locationsList);

    locationsList.setLayoutManager(new LinearLayoutManager(getContext(),
            LinearLayoutManager.HORIZONTAL, true));

    locationsList.setAdapter(locationsListAdapter);


    locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", "pic1"));
    locationsListModels.add(new LocationsListModel("Hotel 2","102 reviews","120$", "pic2"));
    locationsListModels.add(new LocationsListModel("Hotel 3","84 reviews","240$", "pic3"));
    locationsListModels.add(new LocationsListModel("Hotel 4","113 reviews","220$", "pic4"));
    locationsListModels.add(new LocationsListModel("Hotel 5","52 reviews","140$", "pic5"));
    locationsListModels.add(new LocationsListModel("Hotel 6","57 reviews","190$", "pic6"));
    locationsListModels.add(new LocationsListModel("Hotel 7","230 reviews","300$", "pic7"));
    locationsListModels.add(new LocationsListModel("Hotel 8","131 reviews","90$", "pic8"));
    locationsListModels.add(new LocationsListModel("Hotel 9","32 reviews","110$", "pic9"));

    locationsListAdapter.setItems(locationsListModels);
    locationsList.setHasFixedSize(true);

}}
compile 'com.github.bumptech.glide:glide:3.5.2'
Glide.with(context).load(imageurl).transform(new RoundImageTransform(context)).skipMemoryCache(true).into(ImageView);
 public class LocationsListModel {
    public String name, review, price;
    public int imageResource;

    public LocationsListModel(String name, String review, String price, int imageResource) {
        this.name = name;
        this.review = review;
        this.price = price;
        this.imageResource = imageResource;
    }
}
@Override
    public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {

        LocationsListModel locationsListModel = locationList.get(position);

        GlideApp.with(context).load(locationsListModel.imageResource)
                              .into(holder.imageView);

        holder.locationName.setText(locationsListModel.name);
        holder.review.setText(locationsListModel.review);
        holder.locationPrice.setText(locationsListModel.price);

    }
}
我的适配器:

public class LocationsListModel {

final String DRAWABLE = "drawable/";
private String name,imageUrl, review, price;

public LocationsListModel(String name, String review, String price, String imageUrl) {
    this.name = name;
    this.review = review;
    this.price = price;
    this.imageUrl = imageUrl;
}

public String getName() {
    return name;
}
public String getReview() {
    return review;
}
public String getPrice() {
    return price;
}
public String getImageUrl() {
    return DRAWABLE + imageUrl;
}}
public class LocationsListAdapter extends RecyclerView.Adapter<LocationsListAdapter.LocationsListView> {

private static ArrayList<LocationsListModel> locationList;
private Context context;
private ImageView locationImage;


public void setItems(ArrayList<LocationsListModel> items) {
    this.locationList = items;
}


public class LocationsListView extends RecyclerView.ViewHolder {

    public CardView cardView;
    public TextView locationName;
    public TextView review;
    public TextView locationPrice;


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

        cardView = itemView.findViewById(R.id.cardView);

        locationName = itemView.findViewById(R.id.location_name);
        review = itemView.findViewById(R.id.review);
        locationPrice = itemView.findViewById(R.id.price);
        locationImage = itemView.findViewById(R.id.imageView2);
    }
}

@Override
public LocationsListAdapter.LocationsListView onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.locations_list_item, parent, false);

    context = parent.getContext();

    return new LocationsListView(view);
}

@Override
public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {
    final LocationsListModel locationsListModel = locationList.get(position);


    String uri = locationsListModel.getImageUrl();
    int resource = locationImage.getResources().getIdentifier(uri, null, locationImage.getContext().getPackageName());
    locationImage.setImageResource(resource);

    holder.locationName.setText(locationsListModel.getName());
    holder.review.setText(locationsListModel.getReview());
    holder.locationPrice.setText(locationsListModel.getPrice());

}

@Override
public int getItemCount() {
    return locationList.size();
}}
public class LocationsListFragment extends Fragment {

private final LocationsListAdapter locationsListAdapter = new LocationsListAdapter();
private RecyclerView locationsList;
SimpleRatingBar ratingBar;
ArrayList<LocationsListModel> locationsListModels = new ArrayList<>();


public LocationsListFragment() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static LocationsListFragment newInstance() {
    LocationsListFragment fragment = new LocationsListFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_locations_list, container, false);

    ratingBar = view.findViewById(R.id.ratingBar);
    locationsList = view.findViewById(R.id.locations_lis);

    setRecyclerViewItems();

    return view;
}

private void setRecyclerViewItems() {

    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(locationsList);

    locationsList.setLayoutManager(new LinearLayoutManager(getContext(),
            LinearLayoutManager.HORIZONTAL, true));

    locationsList.setAdapter(locationsListAdapter);


    locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", "pic1"));
    locationsListModels.add(new LocationsListModel("Hotel 2","102 reviews","120$", "pic2"));
    locationsListModels.add(new LocationsListModel("Hotel 3","84 reviews","240$", "pic3"));
    locationsListModels.add(new LocationsListModel("Hotel 4","113 reviews","220$", "pic4"));
    locationsListModels.add(new LocationsListModel("Hotel 5","52 reviews","140$", "pic5"));
    locationsListModels.add(new LocationsListModel("Hotel 6","57 reviews","190$", "pic6"));
    locationsListModels.add(new LocationsListModel("Hotel 7","230 reviews","300$", "pic7"));
    locationsListModels.add(new LocationsListModel("Hotel 8","131 reviews","90$", "pic8"));
    locationsListModels.add(new LocationsListModel("Hotel 9","32 reviews","110$", "pic9"));

    locationsListAdapter.setItems(locationsListModels);
    locationsList.setHasFixedSize(true);

}}
compile 'com.github.bumptech.glide:glide:3.5.2'
Glide.with(context).load(imageurl).transform(new RoundImageTransform(context)).skipMemoryCache(true).into(ImageView);
 public class LocationsListModel {
    public String name, review, price;
    public int imageResource;

    public LocationsListModel(String name, String review, String price, int imageResource) {
        this.name = name;
        this.review = review;
        this.price = price;
        this.imageResource = imageResource;
    }
}
@Override
    public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {

        LocationsListModel locationsListModel = locationList.get(position);

        GlideApp.with(context).load(locationsListModel.imageResource)
                              .into(holder.imageView);

        holder.locationName.setText(locationsListModel.name);
        holder.review.setText(locationsListModel.review);
        holder.locationPrice.setText(locationsListModel.price);

    }
}
public class LocationsListAdapter扩展了RecyclerView.Adapter{
私有静态ArrayList locationList;
私人语境;
私有图像查看位置图像;
公共无效集合项(ArrayList项){
this.locationList=项目;
}
公共类LocationsListView扩展了RecyclerView.ViewHolder{
公共卡视图卡视图;
公共文本视图位置名称;
公共文本视图审查;
公共文本视图位置价格;
公共位置列表视图(视图项视图){
超级(项目视图);
cardView=itemView.findViewById(R.id.cardView);
locationName=itemView.findViewById(R.id.location\u name);
review=itemView.findViewById(R.id.review);
locationPrice=itemView.findViewById(R.id.price);
locationImage=itemView.findViewById(R.id.imageView2);
}
}
@凌驾
public LocationsListAdapter.LocationsListView onCreateViewHolder(视图组父级,int-viewType){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.locations\u list\u项,parent,false);
context=parent.getContext();
返回新位置列表视图(视图);
}
@凌驾
BindViewHolder上的公共无效(LocationsListAdapter.LocationsListView holder,内部位置){
最终位置slistmodel LocationsListModel=locationList.get(位置);
字符串uri=locationsListModel.getImageUrl();
int resource=locationImage.getResources().getIdentifier(uri,null,locationImage.getContext().getPackageName());
locationImage.setImageResource(资源);
holder.locationName.setText(locationsListModel.getName());
holder.review.setText(locationsListModel.getReview());
holder.locationPrice.setText(locationsListModel.getPrice());
}
@凌驾
public int getItemCount(){
返回locationList.size();
}}
我的片段:

public class LocationsListModel {

final String DRAWABLE = "drawable/";
private String name,imageUrl, review, price;

public LocationsListModel(String name, String review, String price, String imageUrl) {
    this.name = name;
    this.review = review;
    this.price = price;
    this.imageUrl = imageUrl;
}

public String getName() {
    return name;
}
public String getReview() {
    return review;
}
public String getPrice() {
    return price;
}
public String getImageUrl() {
    return DRAWABLE + imageUrl;
}}
public class LocationsListAdapter extends RecyclerView.Adapter<LocationsListAdapter.LocationsListView> {

private static ArrayList<LocationsListModel> locationList;
private Context context;
private ImageView locationImage;


public void setItems(ArrayList<LocationsListModel> items) {
    this.locationList = items;
}


public class LocationsListView extends RecyclerView.ViewHolder {

    public CardView cardView;
    public TextView locationName;
    public TextView review;
    public TextView locationPrice;


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

        cardView = itemView.findViewById(R.id.cardView);

        locationName = itemView.findViewById(R.id.location_name);
        review = itemView.findViewById(R.id.review);
        locationPrice = itemView.findViewById(R.id.price);
        locationImage = itemView.findViewById(R.id.imageView2);
    }
}

@Override
public LocationsListAdapter.LocationsListView onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.locations_list_item, parent, false);

    context = parent.getContext();

    return new LocationsListView(view);
}

@Override
public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {
    final LocationsListModel locationsListModel = locationList.get(position);


    String uri = locationsListModel.getImageUrl();
    int resource = locationImage.getResources().getIdentifier(uri, null, locationImage.getContext().getPackageName());
    locationImage.setImageResource(resource);

    holder.locationName.setText(locationsListModel.getName());
    holder.review.setText(locationsListModel.getReview());
    holder.locationPrice.setText(locationsListModel.getPrice());

}

@Override
public int getItemCount() {
    return locationList.size();
}}
public class LocationsListFragment extends Fragment {

private final LocationsListAdapter locationsListAdapter = new LocationsListAdapter();
private RecyclerView locationsList;
SimpleRatingBar ratingBar;
ArrayList<LocationsListModel> locationsListModels = new ArrayList<>();


public LocationsListFragment() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static LocationsListFragment newInstance() {
    LocationsListFragment fragment = new LocationsListFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_locations_list, container, false);

    ratingBar = view.findViewById(R.id.ratingBar);
    locationsList = view.findViewById(R.id.locations_lis);

    setRecyclerViewItems();

    return view;
}

private void setRecyclerViewItems() {

    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(locationsList);

    locationsList.setLayoutManager(new LinearLayoutManager(getContext(),
            LinearLayoutManager.HORIZONTAL, true));

    locationsList.setAdapter(locationsListAdapter);


    locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", "pic1"));
    locationsListModels.add(new LocationsListModel("Hotel 2","102 reviews","120$", "pic2"));
    locationsListModels.add(new LocationsListModel("Hotel 3","84 reviews","240$", "pic3"));
    locationsListModels.add(new LocationsListModel("Hotel 4","113 reviews","220$", "pic4"));
    locationsListModels.add(new LocationsListModel("Hotel 5","52 reviews","140$", "pic5"));
    locationsListModels.add(new LocationsListModel("Hotel 6","57 reviews","190$", "pic6"));
    locationsListModels.add(new LocationsListModel("Hotel 7","230 reviews","300$", "pic7"));
    locationsListModels.add(new LocationsListModel("Hotel 8","131 reviews","90$", "pic8"));
    locationsListModels.add(new LocationsListModel("Hotel 9","32 reviews","110$", "pic9"));

    locationsListAdapter.setItems(locationsListModels);
    locationsList.setHasFixedSize(true);

}}
compile 'com.github.bumptech.glide:glide:3.5.2'
Glide.with(context).load(imageurl).transform(new RoundImageTransform(context)).skipMemoryCache(true).into(ImageView);
 public class LocationsListModel {
    public String name, review, price;
    public int imageResource;

    public LocationsListModel(String name, String review, String price, int imageResource) {
        this.name = name;
        this.review = review;
        this.price = price;
        this.imageResource = imageResource;
    }
}
@Override
    public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {

        LocationsListModel locationsListModel = locationList.get(position);

        GlideApp.with(context).load(locationsListModel.imageResource)
                              .into(holder.imageView);

        holder.locationName.setText(locationsListModel.name);
        holder.review.setText(locationsListModel.review);
        holder.locationPrice.setText(locationsListModel.price);

    }
}
公共类LocationsListFragment扩展了片段{
专用最终位置SlistAdapter位置SlistAdapter=新位置SlistAdapter();
私人回收站查看位置列表;
简化杆比杆;
ArrayList locationsListModels=新的ArrayList();
公共位置列表片段(){
//必需的空公共构造函数
}
//TODO:重命名和更改参数的类型和数量
public static LocationsListFragment newInstance(){
LocationsListFragment=新LocationsListFragment();
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
最终视图=充气机。充气(R.layout.fragment\u locations\u list,container,false);
ratingBar=view.findViewById(R.id.ratingBar);
locationsList=view.findViewById(R.id.locations\u-lis);
setRecycleServiceItems();
返回视图;
}
私有void setRecycleServiceItems(){
SnapHelper SnapHelper=新的LinearSnapHelper();
snapHelper.attachToRecyclerView(位置列表);
locationsList.setLayoutManager(新的LinearLayoutManager(getContext()),
LinearLayoutManager.HORIZONTAL,true));
位置列表设置适配器(位置列表适配器);
locationsListModels.add(新LocationsListModel(“酒店1”、“120评论”、“200美元”、“pic1”));
locationsListModels.add(新LocationsListModel(“酒店2”、“102评论”、“120美元”、“pic2”));
locationsListModels.add(新LocationsListModel(“酒店3”、“84评论”、“240美元”、“pic3”));
locationsListModels.add(新LocationsListModel(“酒店4”、“113评论”、“220美元”、“pic4”));
locationsListModels.add(新LocationsListModel(“酒店5”、“52评论”、“140美元”、“pic5”));
locationsListModels.add(新LocationsListModel(“酒店6”、“57评论”、“190美元”、“pic6”));
locationsListModels.add(新LocationsListModel(“酒店7”、“230评论”、“300美元”、“pic7”));
locationsListModels.add(新LocationsListModel(“Hotel 8”、“131评论”、“90美元”、“pic8”));
locationsListModels.add(新的LocationsListModel(“酒店9”、“32评论”、“110美元”、“pic9”));
locationsListAdapter.setItems(locationsListModels);
locationsList.setHasFixedSize(真);
}}

在渐变中:

public class LocationsListModel {

final String DRAWABLE = "drawable/";
private String name,imageUrl, review, price;

public LocationsListModel(String name, String review, String price, String imageUrl) {
    this.name = name;
    this.review = review;
    this.price = price;
    this.imageUrl = imageUrl;
}

public String getName() {
    return name;
}
public String getReview() {
    return review;
}
public String getPrice() {
    return price;
}
public String getImageUrl() {
    return DRAWABLE + imageUrl;
}}
public class LocationsListAdapter extends RecyclerView.Adapter<LocationsListAdapter.LocationsListView> {

private static ArrayList<LocationsListModel> locationList;
private Context context;
private ImageView locationImage;


public void setItems(ArrayList<LocationsListModel> items) {
    this.locationList = items;
}


public class LocationsListView extends RecyclerView.ViewHolder {

    public CardView cardView;
    public TextView locationName;
    public TextView review;
    public TextView locationPrice;


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

        cardView = itemView.findViewById(R.id.cardView);

        locationName = itemView.findViewById(R.id.location_name);
        review = itemView.findViewById(R.id.review);
        locationPrice = itemView.findViewById(R.id.price);
        locationImage = itemView.findViewById(R.id.imageView2);
    }
}

@Override
public LocationsListAdapter.LocationsListView onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.locations_list_item, parent, false);

    context = parent.getContext();

    return new LocationsListView(view);
}

@Override
public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {
    final LocationsListModel locationsListModel = locationList.get(position);


    String uri = locationsListModel.getImageUrl();
    int resource = locationImage.getResources().getIdentifier(uri, null, locationImage.getContext().getPackageName());
    locationImage.setImageResource(resource);

    holder.locationName.setText(locationsListModel.getName());
    holder.review.setText(locationsListModel.getReview());
    holder.locationPrice.setText(locationsListModel.getPrice());

}

@Override
public int getItemCount() {
    return locationList.size();
}}
public class LocationsListFragment extends Fragment {

private final LocationsListAdapter locationsListAdapter = new LocationsListAdapter();
private RecyclerView locationsList;
SimpleRatingBar ratingBar;
ArrayList<LocationsListModel> locationsListModels = new ArrayList<>();


public LocationsListFragment() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static LocationsListFragment newInstance() {
    LocationsListFragment fragment = new LocationsListFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_locations_list, container, false);

    ratingBar = view.findViewById(R.id.ratingBar);
    locationsList = view.findViewById(R.id.locations_lis);

    setRecyclerViewItems();

    return view;
}

private void setRecyclerViewItems() {

    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(locationsList);

    locationsList.setLayoutManager(new LinearLayoutManager(getContext(),
            LinearLayoutManager.HORIZONTAL, true));

    locationsList.setAdapter(locationsListAdapter);


    locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", "pic1"));
    locationsListModels.add(new LocationsListModel("Hotel 2","102 reviews","120$", "pic2"));
    locationsListModels.add(new LocationsListModel("Hotel 3","84 reviews","240$", "pic3"));
    locationsListModels.add(new LocationsListModel("Hotel 4","113 reviews","220$", "pic4"));
    locationsListModels.add(new LocationsListModel("Hotel 5","52 reviews","140$", "pic5"));
    locationsListModels.add(new LocationsListModel("Hotel 6","57 reviews","190$", "pic6"));
    locationsListModels.add(new LocationsListModel("Hotel 7","230 reviews","300$", "pic7"));
    locationsListModels.add(new LocationsListModel("Hotel 8","131 reviews","90$", "pic8"));
    locationsListModels.add(new LocationsListModel("Hotel 9","32 reviews","110$", "pic9"));

    locationsListAdapter.setItems(locationsListModels);
    locationsList.setHasFixedSize(true);

}}
compile 'com.github.bumptech.glide:glide:3.5.2'
Glide.with(context).load(imageurl).transform(new RoundImageTransform(context)).skipMemoryCache(true).into(ImageView);
 public class LocationsListModel {
    public String name, review, price;
    public int imageResource;

    public LocationsListModel(String name, String review, String price, int imageResource) {
        this.name = name;
        this.review = review;
        this.price = price;
        this.imageResource = imageResource;
    }
}
@Override
    public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {

        LocationsListModel locationsListModel = locationList.get(position);

        GlideApp.with(context).load(locationsListModel.imageResource)
                              .into(holder.imageView);

        holder.locationName.setText(locationsListModel.name);
        holder.review.setText(locationsListModel.review);
        holder.locationPrice.setText(locationsListModel.price);

    }
}
适配器:

public class LocationsListModel {

final String DRAWABLE = "drawable/";
private String name,imageUrl, review, price;

public LocationsListModel(String name, String review, String price, String imageUrl) {
    this.name = name;
    this.review = review;
    this.price = price;
    this.imageUrl = imageUrl;
}

public String getName() {
    return name;
}
public String getReview() {
    return review;
}
public String getPrice() {
    return price;
}
public String getImageUrl() {
    return DRAWABLE + imageUrl;
}}
public class LocationsListAdapter extends RecyclerView.Adapter<LocationsListAdapter.LocationsListView> {

private static ArrayList<LocationsListModel> locationList;
private Context context;
private ImageView locationImage;


public void setItems(ArrayList<LocationsListModel> items) {
    this.locationList = items;
}


public class LocationsListView extends RecyclerView.ViewHolder {

    public CardView cardView;
    public TextView locationName;
    public TextView review;
    public TextView locationPrice;


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

        cardView = itemView.findViewById(R.id.cardView);

        locationName = itemView.findViewById(R.id.location_name);
        review = itemView.findViewById(R.id.review);
        locationPrice = itemView.findViewById(R.id.price);
        locationImage = itemView.findViewById(R.id.imageView2);
    }
}

@Override
public LocationsListAdapter.LocationsListView onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.locations_list_item, parent, false);

    context = parent.getContext();

    return new LocationsListView(view);
}

@Override
public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {
    final LocationsListModel locationsListModel = locationList.get(position);


    String uri = locationsListModel.getImageUrl();
    int resource = locationImage.getResources().getIdentifier(uri, null, locationImage.getContext().getPackageName());
    locationImage.setImageResource(resource);

    holder.locationName.setText(locationsListModel.getName());
    holder.review.setText(locationsListModel.getReview());
    holder.locationPrice.setText(locationsListModel.getPrice());

}

@Override
public int getItemCount() {
    return locationList.size();
}}
public class LocationsListFragment extends Fragment {

private final LocationsListAdapter locationsListAdapter = new LocationsListAdapter();
private RecyclerView locationsList;
SimpleRatingBar ratingBar;
ArrayList<LocationsListModel> locationsListModels = new ArrayList<>();


public LocationsListFragment() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static LocationsListFragment newInstance() {
    LocationsListFragment fragment = new LocationsListFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_locations_list, container, false);

    ratingBar = view.findViewById(R.id.ratingBar);
    locationsList = view.findViewById(R.id.locations_lis);

    setRecyclerViewItems();

    return view;
}

private void setRecyclerViewItems() {

    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(locationsList);

    locationsList.setLayoutManager(new LinearLayoutManager(getContext(),
            LinearLayoutManager.HORIZONTAL, true));

    locationsList.setAdapter(locationsListAdapter);


    locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", "pic1"));
    locationsListModels.add(new LocationsListModel("Hotel 2","102 reviews","120$", "pic2"));
    locationsListModels.add(new LocationsListModel("Hotel 3","84 reviews","240$", "pic3"));
    locationsListModels.add(new LocationsListModel("Hotel 4","113 reviews","220$", "pic4"));
    locationsListModels.add(new LocationsListModel("Hotel 5","52 reviews","140$", "pic5"));
    locationsListModels.add(new LocationsListModel("Hotel 6","57 reviews","190$", "pic6"));
    locationsListModels.add(new LocationsListModel("Hotel 7","230 reviews","300$", "pic7"));
    locationsListModels.add(new LocationsListModel("Hotel 8","131 reviews","90$", "pic8"));
    locationsListModels.add(new LocationsListModel("Hotel 9","32 reviews","110$", "pic9"));

    locationsListAdapter.setItems(locationsListModels);
    locationsList.setHasFixedSize(true);

}}
compile 'com.github.bumptech.glide:glide:3.5.2'
Glide.with(context).load(imageurl).transform(new RoundImageTransform(context)).skipMemoryCache(true).into(ImageView);
 public class LocationsListModel {
    public String name, review, price;
    public int imageResource;

    public LocationsListModel(String name, String review, String price, int imageResource) {
        this.name = name;
        this.review = review;
        this.price = price;
        this.imageResource = imageResource;
    }
}
@Override
    public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {

        LocationsListModel locationsListModel = locationList.get(position);

        GlideApp.with(context).load(locationsListModel.imageResource)
                              .into(holder.imageView);

        holder.locationName.setText(locationsListModel.name);
        holder.review.setText(locationsListModel.review);
        holder.locationPrice.setText(locationsListModel.price);

    }
}

注意:删除RoundImageTransform——它用于将imageview制作成圆形

您应该在integer type->R.drawable.p2中使用图像

和你的app/build.gradle添加以下内容:

compile 'com.github.bumptech.glide:glide:4.3.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
您的型号:

public class LocationsListModel {

final String DRAWABLE = "drawable/";
private String name,imageUrl, review, price;

public LocationsListModel(String name, String review, String price, String imageUrl) {
    this.name = name;
    this.review = review;
    this.price = price;
    this.imageUrl = imageUrl;
}

public String getName() {
    return name;
}
public String getReview() {
    return review;
}
public String getPrice() {
    return price;
}
public String getImageUrl() {
    return DRAWABLE + imageUrl;
}}
public class LocationsListAdapter extends RecyclerView.Adapter<LocationsListAdapter.LocationsListView> {

private static ArrayList<LocationsListModel> locationList;
private Context context;
private ImageView locationImage;


public void setItems(ArrayList<LocationsListModel> items) {
    this.locationList = items;
}


public class LocationsListView extends RecyclerView.ViewHolder {

    public CardView cardView;
    public TextView locationName;
    public TextView review;
    public TextView locationPrice;


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

        cardView = itemView.findViewById(R.id.cardView);

        locationName = itemView.findViewById(R.id.location_name);
        review = itemView.findViewById(R.id.review);
        locationPrice = itemView.findViewById(R.id.price);
        locationImage = itemView.findViewById(R.id.imageView2);
    }
}

@Override
public LocationsListAdapter.LocationsListView onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.locations_list_item, parent, false);

    context = parent.getContext();

    return new LocationsListView(view);
}

@Override
public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {
    final LocationsListModel locationsListModel = locationList.get(position);


    String uri = locationsListModel.getImageUrl();
    int resource = locationImage.getResources().getIdentifier(uri, null, locationImage.getContext().getPackageName());
    locationImage.setImageResource(resource);

    holder.locationName.setText(locationsListModel.getName());
    holder.review.setText(locationsListModel.getReview());
    holder.locationPrice.setText(locationsListModel.getPrice());

}

@Override
public int getItemCount() {
    return locationList.size();
}}
public class LocationsListFragment extends Fragment {

private final LocationsListAdapter locationsListAdapter = new LocationsListAdapter();
private RecyclerView locationsList;
SimpleRatingBar ratingBar;
ArrayList<LocationsListModel> locationsListModels = new ArrayList<>();


public LocationsListFragment() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static LocationsListFragment newInstance() {
    LocationsListFragment fragment = new LocationsListFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_locations_list, container, false);

    ratingBar = view.findViewById(R.id.ratingBar);
    locationsList = view.findViewById(R.id.locations_lis);

    setRecyclerViewItems();

    return view;
}

private void setRecyclerViewItems() {

    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(locationsList);

    locationsList.setLayoutManager(new LinearLayoutManager(getContext(),
            LinearLayoutManager.HORIZONTAL, true));

    locationsList.setAdapter(locationsListAdapter);


    locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", "pic1"));
    locationsListModels.add(new LocationsListModel("Hotel 2","102 reviews","120$", "pic2"));
    locationsListModels.add(new LocationsListModel("Hotel 3","84 reviews","240$", "pic3"));
    locationsListModels.add(new LocationsListModel("Hotel 4","113 reviews","220$", "pic4"));
    locationsListModels.add(new LocationsListModel("Hotel 5","52 reviews","140$", "pic5"));
    locationsListModels.add(new LocationsListModel("Hotel 6","57 reviews","190$", "pic6"));
    locationsListModels.add(new LocationsListModel("Hotel 7","230 reviews","300$", "pic7"));
    locationsListModels.add(new LocationsListModel("Hotel 8","131 reviews","90$", "pic8"));
    locationsListModels.add(new LocationsListModel("Hotel 9","32 reviews","110$", "pic9"));

    locationsListAdapter.setItems(locationsListModels);
    locationsList.setHasFixedSize(true);

}}
compile 'com.github.bumptech.glide:glide:3.5.2'
Glide.with(context).load(imageurl).transform(new RoundImageTransform(context)).skipMemoryCache(true).into(ImageView);
 public class LocationsListModel {
    public String name, review, price;
    public int imageResource;

    public LocationsListModel(String name, String review, String price, int imageResource) {
        this.name = name;
        this.review = review;
        this.price = price;
        this.imageResource = imageResource;
    }
}
@Override
    public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {

        LocationsListModel locationsListModel = locationList.get(position);

        GlideApp.with(context).load(locationsListModel.imageResource)
                              .into(holder.imageView);

        holder.locationName.setText(locationsListModel.name);
        holder.review.setText(locationsListModel.review);
        holder.locationPrice.setText(locationsListModel.price);

    }
}
您的适配器:

public class LocationsListModel {

final String DRAWABLE = "drawable/";
private String name,imageUrl, review, price;

public LocationsListModel(String name, String review, String price, String imageUrl) {
    this.name = name;
    this.review = review;
    this.price = price;
    this.imageUrl = imageUrl;
}

public String getName() {
    return name;
}
public String getReview() {
    return review;
}
public String getPrice() {
    return price;
}
public String getImageUrl() {
    return DRAWABLE + imageUrl;
}}
public class LocationsListAdapter extends RecyclerView.Adapter<LocationsListAdapter.LocationsListView> {

private static ArrayList<LocationsListModel> locationList;
private Context context;
private ImageView locationImage;


public void setItems(ArrayList<LocationsListModel> items) {
    this.locationList = items;
}


public class LocationsListView extends RecyclerView.ViewHolder {

    public CardView cardView;
    public TextView locationName;
    public TextView review;
    public TextView locationPrice;


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

        cardView = itemView.findViewById(R.id.cardView);

        locationName = itemView.findViewById(R.id.location_name);
        review = itemView.findViewById(R.id.review);
        locationPrice = itemView.findViewById(R.id.price);
        locationImage = itemView.findViewById(R.id.imageView2);
    }
}

@Override
public LocationsListAdapter.LocationsListView onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.locations_list_item, parent, false);

    context = parent.getContext();

    return new LocationsListView(view);
}

@Override
public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {
    final LocationsListModel locationsListModel = locationList.get(position);


    String uri = locationsListModel.getImageUrl();
    int resource = locationImage.getResources().getIdentifier(uri, null, locationImage.getContext().getPackageName());
    locationImage.setImageResource(resource);

    holder.locationName.setText(locationsListModel.getName());
    holder.review.setText(locationsListModel.getReview());
    holder.locationPrice.setText(locationsListModel.getPrice());

}

@Override
public int getItemCount() {
    return locationList.size();
}}
public class LocationsListFragment extends Fragment {

private final LocationsListAdapter locationsListAdapter = new LocationsListAdapter();
private RecyclerView locationsList;
SimpleRatingBar ratingBar;
ArrayList<LocationsListModel> locationsListModels = new ArrayList<>();


public LocationsListFragment() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static LocationsListFragment newInstance() {
    LocationsListFragment fragment = new LocationsListFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_locations_list, container, false);

    ratingBar = view.findViewById(R.id.ratingBar);
    locationsList = view.findViewById(R.id.locations_lis);

    setRecyclerViewItems();

    return view;
}

private void setRecyclerViewItems() {

    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(locationsList);

    locationsList.setLayoutManager(new LinearLayoutManager(getContext(),
            LinearLayoutManager.HORIZONTAL, true));

    locationsList.setAdapter(locationsListAdapter);


    locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", "pic1"));
    locationsListModels.add(new LocationsListModel("Hotel 2","102 reviews","120$", "pic2"));
    locationsListModels.add(new LocationsListModel("Hotel 3","84 reviews","240$", "pic3"));
    locationsListModels.add(new LocationsListModel("Hotel 4","113 reviews","220$", "pic4"));
    locationsListModels.add(new LocationsListModel("Hotel 5","52 reviews","140$", "pic5"));
    locationsListModels.add(new LocationsListModel("Hotel 6","57 reviews","190$", "pic6"));
    locationsListModels.add(new LocationsListModel("Hotel 7","230 reviews","300$", "pic7"));
    locationsListModels.add(new LocationsListModel("Hotel 8","131 reviews","90$", "pic8"));
    locationsListModels.add(new LocationsListModel("Hotel 9","32 reviews","110$", "pic9"));

    locationsListAdapter.setItems(locationsListModels);
    locationsList.setHasFixedSize(true);

}}
compile 'com.github.bumptech.glide:glide:3.5.2'
Glide.with(context).load(imageurl).transform(new RoundImageTransform(context)).skipMemoryCache(true).into(ImageView);
 public class LocationsListModel {
    public String name, review, price;
    public int imageResource;

    public LocationsListModel(String name, String review, String price, int imageResource) {
        this.name = name;
        this.review = review;
        this.price = price;
        this.imageResource = imageResource;
    }
}
@Override
    public void onBindViewHolder(LocationsListAdapter.LocationsListView holder, int position) {

        LocationsListModel locationsListModel = locationList.get(position);

        GlideApp.with(context).load(locationsListModel.imageResource)
                              .into(holder.imageView);

        holder.locationName.setText(locationsListModel.name);
        holder.review.setText(locationsListModel.review);
        holder.locationPrice.setText(locationsListModel.price);

    }
}
你的片段

locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", R.drawable.pic1));

在将图像加载到imageview时,是否使用了Glide之类的库?否。。。我没有使用任何库…尝试使用Glide。你能试试我的答案吗?你应该换R.drawable.p2yes。。。它起作用了。。。