Cześć,
Probuje ściągnąc za pomocą retrofita i wyświetlic używającGlide randomowy obraz z lorem.picsumw recyclerView. Ustawiam base URL, dodaje annotacje jako rozmiar obrazu (200). Nie wiem gdzie jest błąd. Dla statycznej tablicy ze stringami w postaci linkow dziala, takze zakladam, ze adapter jest dobrze napisany. Bede wdzięczny za każdy hint.
Link do repo: https://github.com/LightingTT/RecycleViewPictures
ApiService:
public interface ApiService {
@GET("200/")
Call<List<Pictures>> getFile();
ApiClient:
public class ApiClient {
public static String BASE_URL = "https://picsum.photos/";
private static Retrofit retrofit;
public static Retrofit getClient(){
if(retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Object class:
public class Pictures {
@SerializedName("picture")
private String picturesUrl;
public Pictures (String picturesUrl)
{
this.picturesUrl = picturesUrl;
}
public String getImageUrl() {
return picturesUrl;
}
}
Adapter:
public class MyRecycleAdapter extends RecyclerView.Adapter<MyRecycleAdapter.ViewHolderClass> {
private static final String TAG = "MainActivity";
private Context context;
private List<Pictures> imageList;
//Constructor
public MyRecycleAdapter(Context context, List<Pictures> imageList)
{
this.context = context;
this.imageList = imageList;
}
//
public void setMyRecycleAdapter(List<Pictures> imageList)
{
this.imageList = imageList;
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolderClass onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.single_picture_view, parent, false);
ViewHolderClass linearViewHolderClass = new ViewHolderClass(view);
return linearViewHolderClass;
}
@Override
public void onBindViewHolder(@NonNull ViewHolderClass holder, int position) {
Glide
.with(context)
.load(imageList.get(position).getImageUrl())
.into(holder.imageView);
Log.d(TAG, "onBindViewHolder: ------>called<-----");
}
@Override
public int getItemCount() {
return imageList.size();
}
public class ViewHolderClass extends RecyclerView.ViewHolder{
private ImageView imageView;
//Constructor
public ViewHolderClass(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.single_picture_id);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
MyRecycleAdapter recyclerAdapter;
List<Pictures> imageList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageList = new ArrayList<>();
//Create RecycleView object and pin view
RecyclerView recycleView = findViewById(R.id.linear_layout_with_recycleView_ID);
GridLayoutManager linearLayoutManager = new GridLayoutManager (this, 2);
//Setup Adapter
recycleView.setLayoutManager(linearLayoutManager);
recyclerAdapter = new MyRecycleAdapter(MainActivity.this, imageList);
recycleView.setAdapter(recyclerAdapter);
Log.d(TAG, "onCreate: ------>called<-----");
//Creating reference for MyService and receiving deserialized data.
ApiService apiClient = ApiClient.getClient().create(ApiService.class);
Call<List<Pictures>> call = apiClient.getFile();
call.enqueue(new Callback<List<Pictures>>() {
@Override
public void onResponse(Call<List<Pictures>> call, Response<List<Pictures>> response) {
imageList = response.body();
Log.d(TAG, "onResponse: ------>called<-----");
recyclerAdapter.setMyRecycleAdapter(imageList);
}
@Override
public void onFailure(Call<List<Pictures>> call, Throwable t) {
Log.d("TAG","onFailure = ------>called<----- "+t.toString());
}
});
}
}