简体   繁体   English

如何在Android中实现无尽的画廊?

[英]How to implement an endless gallery in 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();
                            }

                }
            });


        }

截图Frontgallery

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. 我能够无限地从左到右进行,但是当我从右向左拖动时,它显示了终点。

In getView: 在getView中:

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

this should be removed as it should be handled by the checkPosition function. 这应该被删除,因为它应该由ch​​eckPosition函数处理。

In checkPosition: 在checkPosition中:

For the modulus operator ( % ), given a % b if 0 <= a < b then the result will be a . 对于模数运算符( % ),给定a % b如果0 <= a < b则结果为a For b <= a < 2*b then the result will be ab , so if b == a , then the result is 0 . 对于b <= a < 2*b则结果将为ab ,因此如果b == a ,则结果为0 This continues for any positive integer, so the check should be: 对于任何正整数,这都会继续,因此检查应该是:

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

Now, what you are missing from this is handling the case where position < 0 . 现在,你在这里缺少的是处理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

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. 在这种情况下我们想要的是它回绕到列表的末尾 - 上表中的f(a)

As can be seen in the table above, if a is negative then -b < a <= 0 . 从上表中可以看出,如果a为负,则-b < a <= 0 Also, if we make f(a) = (a % b) + b we get our desired result. 此外,如果我们使f(a) = (a % b) + b我们得到我们想要的结果。

This means that the logic in checkPosition becomes: 这意味着checkPosition中的逻辑变为:

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

which should work for all values of position irrespective of the value of mImageIds.length . 这应该适用于所有position值,而不管mImageIds.length的值。

If anyone wanted to make it also go backwards, you can implement this. 如果有人想让它也倒退,你可以实现这一点。 All it really does it is start the gallery in the middle. 所有这一切真的是在中间开始画廊。

GalleryName.setSelection((int)( Integer.MAX_VALUE / 2 ) - ( Integer.MAX_VALUE / 2 ) % mImageIds.length);

the Adapter code for reece's explanation can be found here: android circular gallery? reece解释的适配器代码可以在这里找到: android圆形图库?

just be sure to use gogothee's suggestion to move the initial selection to the mid-point of the range. 只需确保使用gogothee的建议将初始选择移动到范围的中点。 (so that you can scroll left and right almost forever). (这样你几乎可以永远向左和向右滚动)。 For Example, I did it like this: 例如,我这样做:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    //get references to UI components
    mGallery = (Gallery) findViewById(R.id.filter_gallery);

    //connect Galleries with adapters
    mGallery.setAdapter(new GalleryAdapter(this));

    //auto-select first image (position 1073741820)
    mGallery.setSelection((int)(Integer.MAX_VALUE/2) - ( (Integer.MAX_VALUE/2) % mImageBuffer.getCount() ) );
}

To make first element as center of available array: 要将第一个元素作为可用数组的中心:

your_gallery_obj.setSelection((int)( Integer.MAX_VALUE / 2 ) - ( Integer.MAX_VALUE / 2 ) % your_array_size); your_gallery_obj.setSelection((int)(Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)%your_array_size);

To make middle element as center of available array : 要将中间元素作为可用数组的中心:

your_gallery_obj.setSelection((int)(Integer.MAX_VALUE/2) + (Integer.MAX_VALUE/2) % your_array_size); your_gallery_obj.setSelection((int)(Integer.MAX_VALUE / 2)+(Integer.MAX_VALUE / 2)%your_array_size);

A shameless self plug, just wrote an Infinite Scrolling Gallery Tutorial: 一个无耻的自我插件,只写了一个无限滚动画廊教程:

http://blog.blundell-apps.com/infinite-scrolling-gallery/ http://blog.blundell-apps.com/infinite-scrolling-gallery/

I use the same modulas maths as @reece, source code can also be downloaded. 我使用与@reece相同的modulas数学,也可以下载源代码。

You can use images on your SD card or images in your /resources/drawable directory 您可以使用SD卡上的图像或/ resources / drawable目录中的图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM