LinuxFragment.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package club.thepenguins.android.fragments;
  2. import android.os.Bundle;
  3. import androidx.fragment.app.Fragment;
  4. import androidx.recyclerview.widget.LinearLayoutManager;
  5. import androidx.recyclerview.widget.RecyclerView;
  6. import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import club.thepenguins.android.R;
  14. import club.thepenguins.android.adapters.PostRecyclerAdapter;
  15. import club.thepenguins.android.api.APIService;
  16. import club.thepenguins.android.data.Model;
  17. import club.thepenguins.android.data.Posts;
  18. import club.thepenguins.android.utils.Constants;
  19. import retrofit2.Call;
  20. import retrofit2.Callback;
  21. import retrofit2.Response;
  22. import retrofit2.Retrofit;
  23. import retrofit2.converter.gson.GsonConverterFactory;
  24. /**
  25. * A simple {@link Fragment} subclass.
  26. * Use the {@link LinuxFragment#newInstance} factory method to
  27. * create an instance of this fragment.
  28. */
  29. public class LinuxFragment extends Fragment {
  30. // TODO: Rename parameter arguments, choose names that match
  31. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  32. private static final String ARG_PARAM1 = "param1";
  33. private static final String ARG_PARAM2 = "param2";
  34. // TODO: Rename and change types of parameters
  35. private String mParam1;
  36. private String mParam2;
  37. private RecyclerView recyclerView;
  38. private LinearLayoutManager LayoutManager;
  39. private ArrayList<Model> list;
  40. private PostRecyclerAdapter adapter;
  41. public static List<Posts> mListPost;
  42. private SwipeRefreshLayout swipeContainer;
  43. public LinuxFragment() {
  44. // Required empty public constructor
  45. }
  46. /**
  47. * Use this factory method to create a new instance of
  48. * this fragment using the provided parameters.
  49. *
  50. * @param param1 Parameter 1.
  51. * @param param2 Parameter 2.
  52. * @return A new instance of fragment LinuxFragment.
  53. */
  54. // TODO: Rename and change types and number of parameters
  55. public static LinuxFragment newInstance(String param1, String param2) {
  56. LinuxFragment fragment = new LinuxFragment();
  57. Bundle args = new Bundle();
  58. args.putString(ARG_PARAM1, param1);
  59. args.putString(ARG_PARAM2, param2);
  60. fragment.setArguments(args);
  61. return fragment;
  62. }
  63. @Override
  64. public void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. if (getArguments() != null) {
  67. mParam1 = getArguments().getString(ARG_PARAM1);
  68. mParam2 = getArguments().getString(ARG_PARAM2);
  69. }
  70. }
  71. @Override
  72. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  73. Bundle savedInstanceState) {
  74. View rootView = inflater.inflate(R.layout.fragment_linux, container, false);
  75. getActivity().setTitle("Choose Fragment");
  76. recyclerView = rootView.findViewById(R.id.recycler_view);
  77. LayoutManager = new LinearLayoutManager(rootView.getContext(), LinearLayoutManager.VERTICAL, false);
  78. recyclerView.setLayoutManager(LayoutManager);
  79. swipeContainer = rootView.findViewById(R.id.swiperefresh);
  80. list = new ArrayList<>();
  81. getRetrofit();
  82. adapter = new PostRecyclerAdapter(list, rootView.getContext());
  83. recyclerView.setAdapter(adapter);
  84. swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  85. @Override
  86. public void onRefresh() {
  87. adapter.clear();
  88. getRetrofit();
  89. }
  90. });
  91. swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
  92. android.R.color.holo_green_light,
  93. android.R.color.holo_orange_light,
  94. android.R.color.holo_red_light);
  95. return rootView;
  96. }
  97. private void getRetrofit() {
  98. swipeContainer.setRefreshing(true);
  99. Retrofit retrofit = new Retrofit.Builder()
  100. .baseUrl(Constants.BaseUrl)
  101. .addConverterFactory(GsonConverterFactory.create())
  102. .build();
  103. APIService service = retrofit.create(APIService.class);
  104. Call<List<Posts>> call = service.getCategoryPosts("4");
  105. call.enqueue(new Callback<List<Posts>>() {
  106. @Override
  107. public void onResponse(Call<List<Posts>> call, Response<List<Posts>> response) {
  108. mListPost = response.body();
  109. for (int i = 0; i < response.body().size(); i++) {
  110. list.add(new Model(response.body().get(i).getTitle().getRendered(), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getContent().getRendered()));
  111. //Log.d("Linux", "onResponse: " + response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl());
  112. }
  113. adapter.notifyDataSetChanged();
  114. swipeContainer.setRefreshing(false);
  115. }
  116. @Override
  117. public void onFailure(Call<List<Posts>> call, Throwable t) {
  118. Log.d("Linux", "onFailure: ", t);
  119. }
  120. });
  121. }
  122. }