Gallery default item selected is in center [duplicate]
Possible Duplicate:
android gallery image position problem
I'm using Gallery view within my app. Now when I run the code. Gallery has default selected item is no 1 w开发者_如何学JAVAhich is in center and left side is blank. Instead I want no 1 item should be at left and selected. Also clicking on the any gallery item should not bring that item in the center.
I tired hunting it lot on the groups but not found any solution. Is this possible or not ? If yes then how come?
hey, I kinda got around the very same issue with adjusting the left margin like so:
DisplayMetrics metrics = new DisplayMetrics();
ctx.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Gallery gallery = (Gallery) ctx.findViewById(R.id.gallery);
MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
mlp.setMargins(-(metrics.widthPixels/2),
mlp.topMargin,
mlp.rightMargin,
mlp.bottomMargin
);
which goes almost to the left edge, but there is a bit of slack (half image width?), which I actually liked;
if you replace
-(metrics.widthPixels/2)
with
-(metrics.widthPixels/2 + YOUR_IMAGE_WIDTH)
it goes all the way to left edge of the screen (provided this gallery is displaying images of same size, which was my case)
Gallery
does not have the customization hooks to support doing what you want without a custom implementation. What you should take a look at is the source code for the Gallery widget:
Gallery.java
Use this as a starting point for a custom widget that does exactly what you want. In particular, pay attention to the method setSelectionToCenterChild()
which is part of how the current Gallery does it's selection. Many of the methods are not private, so you may be able to get away with subclassing and overriding methods...but most likely you will need to take this source and create a new class.
Hope that Helps!
it works for me with this :
mlp.setMargins((int) -(metrics.widthPixels/2.5), mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);
精彩评论