gpt4 book ai didi

android - 为 RecyclerView 中的其他项目重复图片

转载 作者:行者123 更新时间:2023-11-30 00:15:41 25 4
gpt4 key购买 nike

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

我有一个包含 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();
}}

我的 fragment :

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);

}}

最佳答案

你应该使用整数类型的图像 -> 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 {
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);

}
}

你的 fragment

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

关于android - 为 RecyclerView 中的其他项目重复图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47346146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com