Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. /**
  2. * Author: CodeBoy722
  3. *
  4. * this fragment handles the browsing of all images in an ArrayList of pictureFacer passed in the constructor
  5. * the images are loaded in a ViewPager an a RecyclerView is used as a pager indicator for
  6. * each image in the ViewPager
  7. */
  8. public class pictureBrowserFragment extends Fragment implements imageIndicatorListener {
  9.  
  10. private ArrayList<pictureFacer> allImages = new ArrayList<>();
  11. private int position;
  12. private Context animeContx;
  13. private ImageView image;
  14. private ViewPager imagePager;
  15. private RecyclerView indicatorRecycler;
  16. private int viewVisibilityController;
  17. private int viewVisibilitylooper;
  18. private ImagesPagerAdapter pagingImages;
  19. private int previousSelected = -1;
  20.  
  21. public pictureBrowserFragment(){
  22.  
  23. }
  24.  
  25. public pictureBrowserFragment(ArrayList<pictureFacer> allImages, int imagePosition, Context anim) {
  26. this.allImages = allImages;
  27. this.position = imagePosition;
  28. this.animeContx = anim;
  29. }
  30.  
  31. public static pictureBrowserFragment newInstance(ArrayList<pictureFacer> allImages, int imagePosition, Context anim) {
  32. pictureBrowserFragment fragment = new pictureBrowserFragment(allImages,imagePosition,anim);
  33. return fragment;
  34. }
  35.  
  36.  
  37. @Nullable
  38. @Override
  39. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  40. return inflater.inflate(R.layout.picture_browser, container, false);
  41.  
  42. }
  43.  
  44. @SuppressLint("ClickableViewAccessibility")
  45. @Override
  46. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  47. super.onViewCreated(view, savedInstanceState);
  48.  
  49. /**
  50. * initialisation of the recyclerView visibility control integers
  51. */
  52. viewVisibilityController = 0;
  53. viewVisibilitylooper = 0;
  54.  
  55. /**
  56. * setting up the viewPager with images
  57. */
  58. imagePager = view.findViewById(R.id.imagePager);
  59. pagingImages = new ImagesPagerAdapter();
  60. imagePager.setAdapter(pagingImages);
  61. imagePager.setOffscreenPageLimit(3);
  62. imagePager.setCurrentItem(position);//displaying the image at the current position passed by the ImageDisplay Activity
  63.  
  64.  
  65. /**
  66. * setting up the recycler view indicator for the viewPager
  67. */
  68. indicatorRecycler = view.findViewById(R.id.indicatorRecycler);
  69. indicatorRecycler.hasFixedSize();
  70. indicatorRecycler.setLayoutManager(new GridLayoutManager(getContext(),1,RecyclerView.HORIZONTAL,false));
  71. RecyclerView.Adapter indicatorAdapter = new recyclerViewPagerImageIndicator(allImages,getContext(),this);
  72. indicatorRecycler.setAdapter(indicatorAdapter);
  73.  
  74. //adjusting the recyclerView indicator to the current position of the viewPager, also highlights the image in recyclerView with respect to the
  75. //viewPager's position
  76. allImages.get(position).setSelected(true);
  77. previousSelected = position;
  78. indicatorAdapter.notifyDataSetChanged();
  79. indicatorRecycler.scrollToPosition(position);
  80.  
  81.  
  82. /**
  83. * this listener controls the visibility of the recyclerView
  84. * indication and it current position in respect to the image ViewPager
  85. */
  86. imagePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  87. @Override
  88. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  89.  
  90. }
  91.  
  92. @Override
  93. public void onPageSelected(int position) {
  94.  
  95. if(previousSelected != -1){
  96. allImages.get(previousSelected).setSelected(false);
  97. previousSelected = position;
  98. allImages.get(position).setSelected(true);
  99. indicatorRecycler.getAdapter().notifyDataSetChanged();
  100. indicatorRecycler.scrollToPosition(position);
  101. }else{
  102. previousSelected = position;
  103. allImages.get(position).setSelected(true);
  104. indicatorRecycler.getAdapter().notifyDataSetChanged();
  105. indicatorRecycler.scrollToPosition(position);
  106. }
  107.  
  108. }
  109.  
  110. @Override
  111. public void onPageScrollStateChanged(int state) {
  112.  
  113. }
  114. });
  115.  
  116.  
  117. indicatorRecycler.setOnTouchListener(new View.OnTouchListener() {
  118. @Override
  119. public boolean onTouch(View v, MotionEvent event) {
  120. /**
  121. * uncomment the below condition to control recyclerView visibility automatically
  122. * when image is clicked also uncomment the condition set on the image's onClickListener in the ImagesPagerAdapter adapter
  123. */
  124. /*if(viewVisibilityController == 0){
  125. indicatorRecycler.setVisibility(View.VISIBLE);
  126. visibiling();
  127. }else{
  128. viewVisibilitylooper++;
  129. }*/
  130. return false;
  131. }
  132. });
  133.  
  134. }
  135.  
  136.  
  137. /**
  138. * this method of the imageIndicatorListerner interface helps in communication between the fragment and the recyclerView Adapter
  139. * each time an iten in the adapter is clicked the position of that item is communicated in the fragment and the position of the
  140. * viewPager is adjusted as follows
  141. * @param ImagePosition The position of an image item in the RecyclerView Adapter
  142. */
  143. @Override
  144. public void onImageIndicatorClicked(int ImagePosition) {
  145.  
  146. //the below lines of code highlights the currently select image in the indicatorRecycler with respect to the viewPager position
  147. if(previousSelected != -1){
  148. allImages.get(previousSelected).setSelected(false);
  149. previousSelected = ImagePosition;
  150. indicatorRecycler.getAdapter().notifyDataSetChanged();
  151. }else{
  152. previousSelected = ImagePosition;
  153. }
  154.  
  155. imagePager.setCurrentItem(ImagePosition);
  156. }
  157.  
  158. /**
  159. * the imageViewPager's adapter
  160. */
  161. private class ImagesPagerAdapter extends PagerAdapter {
  162.  
  163. @Override
  164. public int getCount() {
  165. return allImages.size();
  166. }
  167.  
  168. @NonNull
  169. @Override
  170. public Object instantiateItem(@NonNull ViewGroup containerCollection, int position) {
  171. LayoutInflater layoutinflater = (LayoutInflater) containerCollection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  172.  
  173. View view = layoutinflater.inflate(R.layout.picture_browser_pager,null);
  174. image = view.findViewById(R.id.image);
  175.  
  176. setTransitionName(image, String.valueOf(position)+"picture");
  177.  
  178. pictureFacer pic = allImages.get(position);
  179. Glide.with(animeContx)
  180. .load(pic.getPicturePath())
  181. .apply(new RequestOptions().fitCenter())
  182. .into(image);
  183.  
  184. image.setOnClickListener(new View.OnClickListener() {
  185. @Override
  186. public void onClick(View v) {
  187.  
  188. if(indicatorRecycler.getVisibility() == View.GONE){
  189. indicatorRecycler.setVisibility(View.VISIBLE);
  190. }else{
  191. indicatorRecycler.setVisibility(View.GONE);
  192. }
  193.  
  194. /**
  195. * uncomment the below condition and comment the one above to control recyclerView visibility automatically
  196. * when image is clicked
  197. */
  198. /*if(viewVisibilityController == 0){
  199. indicatorRecycler.setVisibility(View.VISIBLE);
  200. visibiling();
  201. }else{
  202. viewVisibilitylooper++;
  203. }*/
  204.  
  205. }
  206. });
  207.  
  208.  
  209. ((ViewPager) containerCollection).addView(view);
  210. return view;
  211. }
  212.  
  213. @Override
  214. public void destroyItem(ViewGroup containerCollection, int position, Object view) {
  215. ((ViewPager) containerCollection).removeView((View) view);
  216. }
  217.  
  218. @Override
  219. public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
  220. return view == ((View) object);
  221. }
  222. }
  223.  
  224. /**
  225. * method for controlling the visibility of the recyclerView indicator
  226. */
  227. private void visibiling(){
  228. viewVisibilityController = 1;
  229. final int checker = viewVisibilitylooper;
  230. new Handler().postDelayed(new Runnable() {
  231. @Override
  232. public void run() {
  233. if(viewVisibilitylooper > checker){
  234. visibiling();
  235. }else{
  236. indicatorRecycler.setVisibility(View.GONE);
  237. viewVisibilityController = 0;
  238. viewVisibilitylooper = 0;
  239. }
  240. }
  241. }, 4000);
  242. }
  243.  
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement