Search code examples
androidandroid-gallery

Gallery type scroll view in android


I am trying to implement a scroller like show in the image below. enter image description here

I have tried using viewpager but it only shows one item at a time. And I need to show 5 of them and of different sizes. The one in middle needs to be bigger.

Each Item is a frameLayout that contains an ImageView and a TexView, I dont have any problem implementing that part. The problem is it needs to be a scroller and have many items in scroller e.g upto 15 maybe. But should have only 5 items visible at any one time just like shown below. I have tried many implementations. Please some one give me a working example as I have already tried many examples none of them works perfectly. I have waisted more than a week on this one.


Solution

  • You can control it by overriding getPageWidth() in the PagerFragmentAdapter:

    @Override 
    public float getPageWidth(int position) { 
        return(0.4f); 
    } 
    

    and making sure the size of your images is not too large, so that the page width fits multiple images.

    enter image description here

    Here are all the steps to set this up:

    1) Add a fragment container to your activity layout, where you will load the PhotoPagerFragment:

    <!-- PHOTO PAGER FRAGMENT -->
    <FrameLayout
        android:id="@+id/photoPagerFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:tag="sticky" 
        android:layout_gravity="center_horizontal" >
    </FrameLayout>
    

    2) Inject the PhotoPagerFragment in your activity's onCreate():

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_layout);
    
            //Insert the fragment
            FragmentManager fm = getSupportFragmentManager();
            Fragment fragment = fm.findFragmentById(R.id.photoPagerFragmentContainer);
    
            if (fragment == null) {
                fragment = new PhotoPagerFragment();
                fm.beginTransaction()
                    .add(R.id.photoPagerFragmentContainer, fragment)
                    .commit();
            }
        }
    

    3) Create a layout for your PhotoPagerFragment:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black"
        android:orientation="vertical" >
    
        <android.support.v4.view.ViewPager
            android:id="@+id/photoPager"
            android:layout_width="fill_parent"
            android:layout_height="120dp"
            android:layout_marginTop="2dp"/>
    
    </LinearLayout>
    

    4) Create your PhotoPagerFragment:

    public class PhotoPagerFragment extends Fragment {
    
        private ViewPager       mPhotoPager;
        private PagerAdapter    mPhotoAdapter;
    
        public static final String TAG = "PhotoPagerFragment";
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_photo_pager, container, false);
    
            mPhotoAdapter = new PhotoPagerFragmentAdapter(getActivity().getSupportFragmentManager());
    
            mPhotoPager = (ViewPager) view.findViewById(R.id.photoPager);
            mPhotoPager.setAdapter(mPhotoAdapter);
    
            return view;
        }
    }
    

    5) And the adapter:

    public class PhotoPagerFragmentAdapter extends FragmentPagerAdapter {
    
        private int[] Images = new int[] { 
                R.drawable.photo_1, R.drawable.photo_2,
                R.drawable.photo_3, R.drawable.photo_4,
                R.drawable.photo_5, R.drawable.photo_6
        };
    
        private int mCount = Images.length;
    
    
        public PhotoPagerFragmentAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            return PhotoDetailsFragment.newInstance(Images[position]);
        }
    
        @Override
        public int getCount() {
            return mCount;
        }
    
        @Override 
        public float getPageWidth(int position) { 
            return(0.4f); 
        } 
    
        public void setCount(int count) {
            if (count > 0 && count <= 10) {
                mCount = count;
                notifyDataSetChanged();
            }
        }
    }
    

    6) And finally, your PhotoDetailsFragment that will show each image:

    public final class PhotoDetailsFragment extends Fragment {
    
        private int photoResId;
    
        private static final String TAG = "PhotoDetailsFragment";
        public  static final String EXTRA_PHOTO_ID = "com.sample.photo_res_id";
    
    
        public static PhotoDetailsFragment newInstance(int photoResId) {
            Bundle args = new Bundle();
            args.putSerializable(EXTRA_PHOTO_ID, photoResId);
    
            PhotoDetailsFragment fragment = new PhotoDetailsFragment();
            fragment.setArguments(args);
    
            return fragment;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            photoResId = (Integer)getArguments().getSerializable(EXTRA_PHOTO_ID);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            final ImageView image = new ImageView(getActivity());
            image.setImageResource(photoResId);
    
            // Hook up the clicks on the thumbnail views
            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ...
                }
            });
    
            LinearLayout layout = new LinearLayout(getActivity());
            layout.setLayoutParams(new LayoutParams(-1, -1));
    
            layout.setGravity(Gravity.CENTER);
            layout.addView(image);
    
            return layout;
        }
    }