如何实现在Android的无尽的画廊? [英] How to implement an endless gallery in Android?

查看:716
本文介绍了如何实现在Android的无尽的画廊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是画廊布局在我的应用程序。当用户从左到右移动时,在库中的照片,它正在(它是无限的,这意味着元件被再次重复)。但是,当用户从右向左移动并到达第一元件,它没有。之后再没有元素都来了。但我要重复从这个方面也元素。你能给我一些建议吗?

I am using a gallery layout in my application. It is working when the user moves the pictures in the gallery from left to right (it is infinite that means elements are repeated again). But when the user moves from right to left and reaches the first element, it doesn't. After then no elements are coming. But I want to repeat elements from this side also. Can you give me some suggestions?

 Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setFocusable(true);
        g.setSelection((int)(Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)% mImageIds.length);        
        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
            {
                try {
                    imageid=position;
                    ((ImageView)findViewById(R.id.ImageViewlarge)).setImageResource(mImageIds[position]);
                    ((TextView)findViewById(R.id.TextViewImageName)).setText(imgNames[position]);
                     mp = MediaPlayer.create(SeaSpell.this,audioTrack[position]);

                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                }
            });


        }

我怎样才能让我的相册查看循环?我能够从左至右这样做是为了纠正无限,但是当我从从右向左拖动它显示了终点。

How can I make my gallery view circular? I am able to do it from left to right infinitely, but when I drag from right to left it is showing the end point.

推荐答案

在getView:

if(position>21){
    position=0;
}

,因为它应该由ch​​eckPosition功能处理该应被删除。

this should be removed as it should be handled by the checkPosition function.

在checkPosition:

In checkPosition:

有关模运算符(),因为 A%B 如果 0℃ ; =&一个所述; b 那么结果将是 A 。对于 B&LT; = A LT; 2 * B 那么结果将是 AB ,所以如果 B == A ,那么结果是 0 。这继续为任何正整数,所以检查应该是:

For the modulus operator (%), given a % b if 0 <= a < b then the result will be a. For b <= a < 2*b then the result will be a-b, so if b == a, then the result is 0. This continues for any positive integer, so the check should be:

if (position > 0)
    position = position % mImageIds.length;

现在,你是从这个缺少什么办案其中位置&LT; 0

Now, what you are missing from this is handling the case where position < 0.

a    a%3    a    a%3    f(a)
0    0      0    0      0
1    1     -1   -1      2
2    2     -2   -2      1
3    0     -3    0      0
4    1     -4   -1      2
5    2     -5   -2      1
6    0     -6    0      0

我们希望在这种情况下,什么是它回到身边换到列表的末尾 - 的 F(一)在上表中

What we want in this case is for it to wrap back around to the end of the list -- the f(a) in the table above.

由于可以在表中可以看出上述情况,如果 A 为负,那么 -b&LT; A&LT; = 0 。此外,如果我们让 F(A)=(A%B)+ B 我们得到我们想要的结果。

As can be seen in the table above, if a is negative then -b < a <= 0. Also, if we make f(a) = (a % b) + b we get our desired result.

这意味着,在checkPosition逻辑就变成了:

This means that the logic in checkPosition becomes:

position = position % mImageIds.length;
if (position < 0)
    position = position + mImageIds.length;

这应该为位置所有值不论值 mImageIds.length 的。

这篇关于如何实现在Android的无尽的画廊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆