为 RecyclerView 中的其他项目重复图片
Repeating Pictures for other items in the RecyclerView
首先,请原谅我,我的英语不好。
我有一个RecyclerView,里面有9个item,但是从第四个item到下一个,其他item重复了前4个item的图片!
但其他字段对于 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);
}}
在Gradle中:
compile 'com.github.bumptech.glide:glide:3.5.2'
适配器:
Glide.with(context).load(imageurl).transform(new RoundImageTransform(context)).skipMemoryCache(true).into(ImageView);
注意:删除 RoundImageTransform -- 它是为了使图像视图成为圆形
你应该使用整数类型的图像 -> 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);
}
}
你的片段
locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", R.drawable.pic1));
首先,请原谅我,我的英语不好。
我有一个RecyclerView,里面有9个item,但是从第四个item到下一个,其他item重复了前4个item的图片! 但其他字段对于 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);
}}
在Gradle中:
compile 'com.github.bumptech.glide:glide:3.5.2'
适配器:
Glide.with(context).load(imageurl).transform(new RoundImageTransform(context)).skipMemoryCache(true).into(ImageView);
注意:删除 RoundImageTransform -- 它是为了使图像视图成为圆形
你应该使用整数类型的图像 -> 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);
}
}
你的片段
locationsListModels.add(new LocationsListModel("Hotel 1","120 reviews","200$", R.drawable.pic1));