View pager for Android gallery effects multiple images
First, let's look at the following results:
From the above picture, we can see that when multiple images are added, they can be squashed into a gallery. We pull the images left and right to see the images we added. Is the effect much better? Let's see how it works!
The above effect is similar to the ViewPage effect in Android, but it is different from ViewPager. ViewPager can only display one image at a time.
In fact, we have used the clipChildren attribute of View. Here we want to set ViewPager and its parent form to false, as shown below:
Android: clipChildren = "false"
If the clipChildren attribute is set to true, it indicates that children is to be dropped to clip. That is to say, all parts beyond the current view will be dropped for the child element, set it to false here, which indicates that the part beyond the view is displayed. Do not cut it off.
Xml code:
Android: id = "@ + id/container"
Android: layout_width = "match_parent"
Android: layout_height = "100dp"
Android: clipChildren = "false"
Android: gravity = "center_horizontal"
Android: layerType = "software"
Android: orientation = "horizontal">
Android: id = "@ + id/viewpager"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: layout_marginLeft = "110dp"
Android: layout_marginRight = "110dp"
Android: clipChildren = "false">
Java code:
// 1. Set the number of cached items
MViewPager. setOffscreenPageLimit (3 );
// 2. Set the spacing between pages
MViewPager. setPageMargin (10 );
// 3. Distribute the touch events of the parent class to viewPgaer. Otherwise, only one view object in the middle can be moved.
Container. setOnTouchListener (new View. OnTouchListener (){
@ Override
Public boolean onTouch (View v, MotionEvent event ){
Return mViewPager. dispatchTouchEvent (event );
}
});