开发者

How do I get the number of visible Views for a Gallery?

I have a Gallery and want to know how many items are visible on the screen. Each item takes up th开发者_StackOverflowe full screen, so I'm really interested in knowing when two items are visible as a result of being mid-scroll. Thanks.


OK here we go.! Extend your Gallery Get access to the

protected boolean getChildStaticTransformation(View child, Transformation t) {}

There you have access to each view displayed at your gallery. From now on 2 things you can do.

  1. Count all the unique childs (getChildStaticTransformation can be called multiple times eg on touch events etc. that's why checking for unique childs is must). Notice this method returns the childs that are populated by the gallery. Gallery populated top-1 and bottom+1 childs for performance issues. Sometimes is +2 -2 or +n -n (depends of the child width , cause is horizontal scrolling widget). So that way maybe not what you need cause is not always accurate on the childs displayed to the gallery.
  2. For each child check if the .getLeft and .getRight is inside the gallery bounds.So if the getLeft of a child is inside the gallery and the getRight of another child is in gallery too you are at mid-scroll .This is prolly what you need.
  3. If you have fill_parent to the child (display only one at a time) then you can see if the gallery is scrolling flinging.Then you are at the mid-scroll until the gallery "launch" an setOnItemSelectedListener(). For this to work you have to setCallbackDuringFling(true);


The best I can think of is to set an AnimationListener on your Gallery like this:

gallery.setLayoutAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
        //schedule timer to execute halfway through animation here

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub

    }
});

And set the animation duration with gallery.setAnimationDuration(durationInMilliseconds);

Now you know when the animation starts and you know how long it will take. With that information you can schedule a timer to execute your code halfway through the animation, which should be when your gallery has two views showing at the same time.

This seems like a very difficult way to do it though, so give someone else time to give a better suggestion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜