daily pastebin goal
9%
SHARE
TWEET

OneFragment

rama_astadipati Jun 24th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fragment/OneFragment
  2.  
  3. package info.androidboss.retrofit.fragment;
  4.  
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.support.v4.app.Fragment;
  8. import android.support.v7.widget.DefaultItemAnimator;
  9. import android.support.v7.widget.DividerItemDecoration;
  10. import android.support.v7.widget.LinearLayoutManager;
  11. import android.support.v7.widget.RecyclerView;
  12. import android.util.Log;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.Toast;
  17.  
  18.  
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. import info.androidboss.retrofit.model.Movie;
  23. import info.androidboss.retrofit.adapter.MovieAdapter;
  24. import info.androidboss.retrofit.R;
  25. import info.androidboss.retrofit.utils.RetroFitApiClient;
  26. import info.androidboss.retrofit.appinterface.RetroFitApiInterface;
  27. import retrofit2.Call;
  28. import retrofit2.Callback;
  29. import retrofit2.Response;
  30.  
  31. public class OneFragment extends Fragment {
  32.  
  33.     MovieAdapter movieAdapter;
  34.     RecyclerView recyclerView;
  35.     List<Movie> movieList = new ArrayList<Movie>();
  36.     private OnFragmentInteractionListener listener;
  37.     public static OneFragment newInstance() {
  38.         return new OneFragment();
  39.     }
  40.  
  41.     @Override
  42.     public void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.     }
  45.  
  46.     @Override
  47.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  48.                              Bundle savedInstanceState) {
  49.  
  50.         View view = inflater.inflate(R.layout.fragment_one, container, false);
  51.         recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
  52.  
  53.         movieAdapter = new MovieAdapter(movieList, getActivity());
  54.         RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
  55.         recyclerView.setLayoutManager(mLayoutManager);
  56.         recyclerView.setItemAnimator(new DefaultItemAnimator());
  57.         recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
  58.         recyclerView.setAdapter(movieAdapter);
  59.         getMovieList();
  60.         return view;
  61.     }
  62.  
  63.     @Override
  64.     public void onAttach(Context context) {
  65.         super.onAttach(context);
  66.         if (context instanceof OnFragmentInteractionListener) {
  67.             listener = (OnFragmentInteractionListener) context;
  68.         } else {
  69.             throw new RuntimeException(context.toString()
  70.                     + " must implement OnFragmentInteractionListener");
  71.         }
  72.     }
  73.  
  74.     @Override
  75.     public void onDetach() {
  76.         super.onDetach();
  77.         listener = null;
  78.     }
  79.  
  80.     public void getMovieList() {
  81.         RetroFitApiInterface apiInterface = RetroFitApiClient.getClient().create(RetroFitApiInterface.class);
  82.         Call<List<Movie>> call = apiInterface.getMovie();
  83.         call.enqueue(new Callback<List<Movie>>() {
  84.             @Override
  85.             public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
  86.  
  87.                 if (response==null){
  88.                     Toast.makeText(getActivity(), "Somthing Went Wrong...!!", Toast.LENGTH_SHORT).show();
  89.                 }else{
  90.                     for (Movie movie:response.body()){
  91.                         movieList.add(movie);
  92.                     }
  93.                     Log.i("RESPONSE: ", ""+response.toString());
  94.                 }
  95.                 movieAdapter.notifyDataSetChanged();
  96.             }
  97.  
  98.             @Override
  99.             public void onFailure(Call<List<Movie>> call, Throwable t) {
  100.                 Toast.makeText(getActivity(), "Unable to fetch json: " + t.getMessage(), Toast.LENGTH_LONG).show();
  101.                 Log.e("ERROR: ", t.getMessage());
  102.             }
  103.         });
  104.     }
  105.  
  106.     public interface OnFragmentInteractionListener {
  107.     }
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top