Gallery default item selected is in center [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-30 07:26:50

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);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!