Answer the question
In order to leave comments, you need to log in
Hello! How to add recyclerView to fragment? In the onResponce method?
public class HomeFragment extends Fragment {
RecyclerView recyclerView;
DelpapaAdapter delpapaAdapter;
listdata;
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
class public class DelpapaAdapter extends RecyclerView.Adapter {
Context context;
listdata;
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);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question