public class HomeFragment extends Fragment {
RecyclerView recyclerView;
DelpapaAdapter delpapaAdapter;
List data;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
Call call = APIclient.apIinterface().getDelpapa();
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
recyclerView.setVisibility(View.VISIBLE);
delpapaAdapter = new DelpapaAdapter(HomeFragment.this,response.body().data,getContext());
recyclerView.setAdapter(delpapaAdapter);
Log.d("resultOk", response.body() + "");
Toast.makeText(getActivity().getApplicationContext(), "Ok everything ", Toast.LENGTH_LONG).show();
} else {
recyclerView.setVisibility(View.VISIBLE);
Toast.makeText(getActivity().getApplicationContext(), "An error Occured ", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call call, Throwable t) {
recyclerView.setVisibility(View.VISIBLE);
Toast.makeText(getActivity().getApplicationContext(), "An error Occured " + t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
return recyclerView;
}
}
Класс DelpapaAdapter
public class DelpapaAdapter extends RecyclerView.Adapter {
Context context;
List data;
public DelpapaAdapter(Context context, List data) {
this.context = context;
this.data = data;
}
public DelpapaAdapter(HomeFragment homeFragment, List data, Context context) {
}
@NonNull
@Override
public DelpapaViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.fragment_home, parent, false);
return new DelpapaViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DelpapaViewHolder holder, int position) {
Delpapa delpapa = data.get(position);
Glide.with(context)
.load(delpapa.getLink())
.into(holder.imageView);
}
@Override
public int getItemCount() {
return data.size();
}
public class DelpapaViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public DelpapaViewHolder(@NonNull View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
}
}
}
Вопрос задан
более трёх лет назад
233 просмотра