如何创建一个封闭的(圆形)的ListView?创建一个、圆形、ListView

2023-09-11 11:03:37 作者:致命的执着

我想创建一个自定义的ListView(或类似),这将表现得像个封闭的(圆形)之一:

向下滚动 - 在最后一个项目是达到第一开始(..,n-1个中,n,1,2,..) 向上滚动 - 之后的第一个项目是到达最后开始(..,2,1,N,N-1,..)

这听起来很简单,但在概念上,显然,有没有简单的方法来做到这一点。 任何人都可以点我正确的解决方案? 谢谢!

我已经收到(在Android的开发者谷歌团体街道波士顿)的答案,但它听起来莫名其妙地丑:) -

  

我创造我自己这样做   清单适配器(从子类   BaseAdapter)。

     

I codeD我自己的名单适配器在这样的   的方式,它的getCount将()方法返回   一个HUUUUGE号码。

     

如果项目'X'被选中,那么这   项对应到适配器   位置='adapter.getCount()/ 2 + X'

     

和我的适配器的方法   的getItem(INT位置),我看在我   阵列备份适配器   抓取索引的项目:       (位置getCount将()/ 2)%myDataItems.length

     

您需要做一些更特殊   的东西,使一切工作正常,   但你的想法。

     为什么CAD选择一个圆进行都会提示边界不封闭

在原则上,它仍然是可能的   到年底或将开始   列表中,但如果你设置getCount将()来   围绕一个亿左右,这是很难   要做到: - )

解决方案

我的同事乔,我相信我们已经找到了一个简单的方法来解决同样的问题。在我们的解决方案,虽然不是扩大BaseAdapter我们扩展ArrayAdapter。

在code是为休耕:

 
公共类CircularArrayAdapter扩展ArrayAdapter
{

        公共静态最终诠释HALF_MAX_VALUE = Integer.MAX_VALUE的/ 2;
        公众最终诠释中部;
        私人T []的对象;

        公共CircularArrayAdapter(上下文的背景下,INT textViewResourceId,T []对象)
        {
            超(背景下,textViewResourceId,对象);
            this.objects =物体;
            MIDDLE = HALF_MAX_VALUE  -  HALF_MAX_VALUE%objects.length;
        }

        @覆盖
        公众诠释getCount将()
        {
            返回Integer.MAX_VALUE的;
        }

        @覆盖
        公共牛逼的getItem(INT位置)
        {
            返回对象[位置%objects.length]。
        }
 }
 

因此​​,这将创建一个类称为CircularArrayAdapter内搭的对象类型T(可以是任何东西),并使用它来创建一个数组列表。 T是常用的字符串虽然可以是任何东西。

的构造是相同的为ArrayAdapter虽然初始化一个叫做中间恒定。这是列表的中间。无论什么可用于阵列MIDDLE的长度居中的ListView在列表的中间。

getCount将被overrided返回一个巨大的价值如上面建立一个巨大的名单进行。

的getItem被overrided返回阵列上的假位置。从而填充列表时该列表充满中循环方式的对象。

在这一点上CircularArrayAdapter只是替换ArrayAdapter创建的ListView的文件中。

要居中的ListView休耕线必须插在你的文件创建的ListView ListView的对象已经初始化之后:

listViewObject.setSelectionFromTop(nameOfAdapterObject.MIDDLE,0);

和使用MIDDLE恒定$ P $为视图是在屏幕的顶部居中的列表的顶部项目列表pviously初始化

:)〜干杯,我希望这个解决方案是非常有用的。

I want to create a customized ListView (or similar) which will behave like a closed (circular) one:

scrolling down - after the last item was reached the first begins (.., n-1, n, 1, 2, ..) scrolling upward - after the first item was reached the last begins (.., 2, 1, n, n-1, ..)

It sounds simple conceptually but, apparently, there is no straightforward approach to do this. Can anyone point me to the right solution ? Thank you !

I have already received an answer (from Streets Of Boston on Android-Developers google groups), but it sounds somehow ugly :) -

I did this by creating my own list-adapter (subclassed from BaseAdapter).

I coded my own list-adapter in such a way that its getCount() method returns a HUUUUGE number.

And if item 'x' is selected, then this item corresponds to adapter position='adapter.getCount()/2+x'

And for my adapter's method getItem(int position), i look in my array that backs up the adapter and fetch the item on index: (position-getCount()/2) % myDataItems.length

You need to do some more 'special' stuff to make it all work correctly, but you get the idea.

In principle, it is still possible to reach the end or the beginning of the list, but if you set getCount() to around a million or so, this is hard to do :-)

解决方案

My colleague Joe, and I believe we have found a simpler way to solve the same problem. In our solution though instead of extending BaseAdapter we extend ArrayAdapter.

The code is as fallows :


public class CircularArrayAdapter extends ArrayAdapter
{   

        public static final int HALF_MAX_VALUE = Integer.MAX_VALUE/2;
        public final int MIDDLE;
        private T[] objects;

        public CircularArrayAdapter(Context context, int textViewResourceId, T[] objects)
        {
            super(context, textViewResourceId, objects);
            this.objects = objects;
            MIDDLE = HALF_MAX_VALUE - HALF_MAX_VALUE % objects.length;
        }

        @Override
        public int getCount()
        {
            return Integer.MAX_VALUE;
        }

        @Override
        public T getItem(int position) 
        {
            return objects[position % objects.length];
        }
 }

So this creates a class called CircularArrayAdapter which take an object type T (which may be anything) and uses it to create an array list. T is commonly a string though may be anything.

The constructor is the same as is for ArrayAdapter though initializes a constant called middle. This is the middle of the list. No matter what the length of the array MIDDLE can be used to center the ListView in the mid of the list.

getCount is overrided to return a huge value as is done above creating a huge list.

getItem is overrided to return the fake position on the array. Thus when filling the list the list is filled with objects in a looping manner.

At this point CircularArrayAdapter simply replaces ArrayAdapter in the file creating the ListView.

To center the ListView the fallowing line must be inserted in your file creating the ListView after the ListView object has been initialized:

listViewObject.setSelectionFromTop(nameOfAdapterObject.MIDDLE, 0);

and using the MIDDLE constant previously initialized for the list the view is centered with the top item of the list at the top of the screen.

: ) ~ Cheers, I hope this solution is useful.

 
精彩推荐
图片推荐