Gallery default item selected is in center [duplicate]

孤人 提交于 2019-12-18 12:38:46

问题


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 which 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?


回答1:


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)




回答2:


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!




回答3:


it works for me with this :

mlp.setMargins((int) -(metrics.widthPixels/2.5), mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);


来源:https://stackoverflow.com/questions/5404590/gallery-default-item-selected-is-in-center

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