1

まず、私を許してください、私の英語は良くありません。RecyclerViewの他の項目の画像を繰り返します

私はRecyclerViewに9個のアイテムがありますが、4番目のアイテムから次のアイテムには、最初の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(); 
}} 

マイ・フラグメント:

Gradleので
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); 

}} 
+0

画像をimageviewに読み込むには、Glideのようなライブラリを使用しましたか? –

+0

いいえ...ライブラリを使用していませんでした。 – hirad

+0

グライドを試してみてください。 –

答えて

1

ラウンドとしてImageViewのを作るためです整数型 - > R.drawable.p2

、アプリ/ 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)); 
0

compile 'com.github.bumptech.glide:glide:3.5.2' 

アダプタ:

Glide.with(context).load(imageurl).transform(new RoundImageTransform(context)).skipMemoryCache(true).into(ImageView); 

注:RoundImageTransformを削除する - それはあなたが画像を使うべき形状

+0

ご指導ありがとうございます.... – hirad

関連する問題