Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Author: CodeBoy722
- *
- * this fragment handles the browsing of all images in an ArrayList of pictureFacer passed in the constructor
- * the images are loaded in a ViewPager an a RecyclerView is used as a pager indicator for
- * each image in the ViewPager
- */
- public class pictureBrowserFragment extends Fragment implements imageIndicatorListener {
- private ArrayList<pictureFacer> allImages = new ArrayList<>();
- private int position;
- private Context animeContx;
- private ImageView image;
- private ViewPager imagePager;
- private RecyclerView indicatorRecycler;
- private int viewVisibilityController;
- private int viewVisibilitylooper;
- private ImagesPagerAdapter pagingImages;
- private int previousSelected = -1;
- public pictureBrowserFragment(){
- }
- public pictureBrowserFragment(ArrayList<pictureFacer> allImages, int imagePosition, Context anim) {
- this.allImages = allImages;
- this.position = imagePosition;
- this.animeContx = anim;
- }
- public static pictureBrowserFragment newInstance(ArrayList<pictureFacer> allImages, int imagePosition, Context anim) {
- pictureBrowserFragment fragment = new pictureBrowserFragment(allImages,imagePosition,anim);
- return fragment;
- }
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.picture_browser, container, false);
- }
- @SuppressLint("ClickableViewAccessibility")
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- /**
- * initialisation of the recyclerView visibility control integers
- */
- viewVisibilityController = 0;
- viewVisibilitylooper = 0;
- /**
- * setting up the viewPager with images
- */
- imagePager = view.findViewById(R.id.imagePager);
- pagingImages = new ImagesPagerAdapter();
- imagePager.setAdapter(pagingImages);
- imagePager.setOffscreenPageLimit(3);
- imagePager.setCurrentItem(position);//displaying the image at the current position passed by the ImageDisplay Activity
- /**
- * setting up the recycler view indicator for the viewPager
- */
- indicatorRecycler = view.findViewById(R.id.indicatorRecycler);
- indicatorRecycler.hasFixedSize();
- indicatorRecycler.setLayoutManager(new GridLayoutManager(getContext(),1,RecyclerView.HORIZONTAL,false));
- RecyclerView.Adapter indicatorAdapter = new recyclerViewPagerImageIndicator(allImages,getContext(),this);
- indicatorRecycler.setAdapter(indicatorAdapter);
- //adjusting the recyclerView indicator to the current position of the viewPager, also highlights the image in recyclerView with respect to the
- //viewPager's position
- allImages.get(position).setSelected(true);
- previousSelected = position;
- indicatorAdapter.notifyDataSetChanged();
- indicatorRecycler.scrollToPosition(position);
- /**
- * this listener controls the visibility of the recyclerView
- * indication and it current position in respect to the image ViewPager
- */
- imagePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
- @Override
- public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
- }
- @Override
- public void onPageSelected(int position) {
- if(previousSelected != -1){
- allImages.get(previousSelected).setSelected(false);
- previousSelected = position;
- allImages.get(position).setSelected(true);
- indicatorRecycler.getAdapter().notifyDataSetChanged();
- indicatorRecycler.scrollToPosition(position);
- }else{
- previousSelected = position;
- allImages.get(position).setSelected(true);
- indicatorRecycler.getAdapter().notifyDataSetChanged();
- indicatorRecycler.scrollToPosition(position);
- }
- }
- @Override
- public void onPageScrollStateChanged(int state) {
- }
- });
- indicatorRecycler.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- /**
- * uncomment the below condition to control recyclerView visibility automatically
- * when image is clicked also uncomment the condition set on the image's onClickListener in the ImagesPagerAdapter adapter
- */
- /*if(viewVisibilityController == 0){
- indicatorRecycler.setVisibility(View.VISIBLE);
- visibiling();
- }else{
- viewVisibilitylooper++;
- }*/
- return false;
- }
- });
- }
- /**
- * this method of the imageIndicatorListerner interface helps in communication between the fragment and the recyclerView Adapter
- * each time an iten in the adapter is clicked the position of that item is communicated in the fragment and the position of the
- * viewPager is adjusted as follows
- * @param ImagePosition The position of an image item in the RecyclerView Adapter
- */
- @Override
- public void onImageIndicatorClicked(int ImagePosition) {
- //the below lines of code highlights the currently select image in the indicatorRecycler with respect to the viewPager position
- if(previousSelected != -1){
- allImages.get(previousSelected).setSelected(false);
- previousSelected = ImagePosition;
- indicatorRecycler.getAdapter().notifyDataSetChanged();
- }else{
- previousSelected = ImagePosition;
- }
- imagePager.setCurrentItem(ImagePosition);
- }
- /**
- * the imageViewPager's adapter
- */
- private class ImagesPagerAdapter extends PagerAdapter {
- @Override
- public int getCount() {
- return allImages.size();
- }
- @NonNull
- @Override
- public Object instantiateItem(@NonNull ViewGroup containerCollection, int position) {
- LayoutInflater layoutinflater = (LayoutInflater) containerCollection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = layoutinflater.inflate(R.layout.picture_browser_pager,null);
- image = view.findViewById(R.id.image);
- setTransitionName(image, String.valueOf(position)+"picture");
- pictureFacer pic = allImages.get(position);
- Glide.with(animeContx)
- .load(pic.getPicturePath())
- .apply(new RequestOptions().fitCenter())
- .into(image);
- image.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if(indicatorRecycler.getVisibility() == View.GONE){
- indicatorRecycler.setVisibility(View.VISIBLE);
- }else{
- indicatorRecycler.setVisibility(View.GONE);
- }
- /**
- * uncomment the below condition and comment the one above to control recyclerView visibility automatically
- * when image is clicked
- */
- /*if(viewVisibilityController == 0){
- indicatorRecycler.setVisibility(View.VISIBLE);
- visibiling();
- }else{
- viewVisibilitylooper++;
- }*/
- }
- });
- ((ViewPager) containerCollection).addView(view);
- return view;
- }
- @Override
- public void destroyItem(ViewGroup containerCollection, int position, Object view) {
- ((ViewPager) containerCollection).removeView((View) view);
- }
- @Override
- public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
- return view == ((View) object);
- }
- }
- /**
- * method for controlling the visibility of the recyclerView indicator
- */
- private void visibiling(){
- viewVisibilityController = 1;
- final int checker = viewVisibilitylooper;
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- if(viewVisibilitylooper > checker){
- visibiling();
- }else{
- indicatorRecycler.setVisibility(View.GONE);
- viewVisibilityController = 0;
- viewVisibilitylooper = 0;
- }
- }
- }, 4000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement