Advertisement
Mihail_Atnsv

ForumAvtivity

Mar 15th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. package com.roehamptonuniversity.activities;
  2.  
  3. import androidx.fragment.app.Fragment;
  4. import androidx.recyclerview.widget.GridLayoutManager;
  5. import androidx.recyclerview.widget.RecyclerView;
  6.  
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12.  
  13. import com.roehamptonuniversity.R;
  14.  
  15. public class ForumActivity extends Fragment {
  16.  
  17.     public View onCreate(LayoutInflater inflater, ViewGroup container,
  18.                          Bundle savedInstanceState) {
  19.         RecyclerView recyclerView = (RecyclerView) inflater.inflate(
  20.                 R.layout.fragment_categories, container, false);
  21.         String[] categoryNames = new String[Categories.category.length];
  22.         for (int i = 0; i < categoryNames.length; i++) {
  23.             categoryNames[i] = Categories.category[i].getName();
  24.         }
  25.  
  26.         int[] categoryImages = new int[Categories.category.length];
  27.         for (int i = 0; i < categoryImages.length; i++) {
  28.             categoryImages[i] = Categories.category[i].getImageResourceId();
  29.         }
  30.         CaptionedImageAdaptor adapter = new CaptionedImageAdaptor(categoryNames, categoryImages);
  31.         recyclerView.setAdapter(adapter);
  32.         GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
  33.         recyclerView.setLayoutManager(layoutManager);
  34.  
  35.         adapter.setListener(new CaptionedImageAdaptor.Listener() {
  36.             @Override
  37.             public void onClick(int position) {
  38.                 Intent intent = new Intent(getActivity(), CategoryDetail.class);
  39.                 intent.putExtra(CategoryDetail.EXTRA_CATEGORY_ID, position);
  40.                 getActivity().startActivity(intent);
  41.             }
  42.         });
  43.         return recyclerView;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement