如何创建一个封闭的(圆形)ListView? [英] How to create a closed (circular) ListView?

查看:25
本文介绍了如何创建一个封闭的(圆形)ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义的 ListView(或类似的),它的行为就像一个封闭的(圆形):

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

  1. 向下滚动 - 在到达最后一个项目后,第一个开始(.., n-1, n, 1, 2, ..)
  2. 向上滚动 - 到达第一项后,最后一项开始 (.., 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 !

我已经收到了一个答案(来自 Streets Of Boston 的 Android-Developers google 群组),但听起来有点难看 :) -

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).

我用这样的方式编写了自己的列表适配器其 getCount() 方法返回的方式HUUUUGE 号码.

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

如果项目 'x' 被选中,那么这个item对应适配器position='adapter.getCount()/2+x'

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

对于我的适配器方法getItem(int position),我看看我的备份适配器的阵列和获取索引上的项目:(position-getCount()/2) % myDataItems.length

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.

原则上还是可以的到达终点或起点列表,但是如果您将 getCount() 设置为大约一百万左右,这很难要做:-)

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 :-)

推荐答案

我的同事 Joe 和我相信我们已经找到了一种更简单的方法来解决同样的问题.在我们的解决方案中,我们扩展了 ArrayAdapter,而不是扩展 BaseAdapter.

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.

代码如下:

public class CircularArrayAdapter< T > extends ArrayAdapter< T >
{   

        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];
        }
 }

因此,这创建了一个名为 CircularArrayAdapter 的类,它采用对象类型 T(可以是任何类型)并使用它来创建数组列表.T 通常是一个字符串,但可以是任何东西.

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.

构造函数与 ArrayAdapter 相同,但初始化了一个名为 middle 的常量.这是列表的中间部分.无论数组 MIDDLE 的长度如何,都可以将 ListView 置于列表中间.

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() 被覆盖以返回一个巨大的值,就像上面创建一个巨大的列表一样.

getCount() is overrides to return a huge value as is done above creating a huge list.

getItem() 被覆盖以返回数组上的假位置.因此,当填充列表时,列表以循环方式填充对象.

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

此时 CircularArrayAdapter 只是替换了创建 ListView 的文件中的 ArrayAdapter.

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

要使 ListView 居中,必须在 ListView 对象初始化后创建 ListView 的文件中插入空闲线:

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

listViewObject.setSelectionFromTop(nameOfAdapterObject.MIDDLE, 0);

并使用之前为列表初始化的 MIDDLE 常量,视图以屏幕顶部的列表顶部项目居中.

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.

这篇关于如何创建一个封闭的(圆形)ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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