安卓:如何制作具有无限滚动的无限滚动视图

7
我希望你能提供一段代码,让 ScrollView 可以以无限循环的方式显示相同的图片。

这对于布局非常有用,我想知道添加到具有无限滚动的ScrollView所需的代码是什么。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="match_parent"

    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
           <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
           <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />


    </LinearLayout>
</HorizontalScrollView>


可能是Android无限列表的重复问题。 - Jim G.
2个回答

10

使用一个ListView和一个稍加修改以具有“无限”元素的Adapter

以下是支持此行为的Adapter的更改:

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

@Override
public ImageItem getItem(int position) 
{
    return mItems.get(position % mItems.size());
}

基本上,您可以通过告诉它计数为MAX_INT来欺骗它,然后在获取项目时使用模运算以获取序列中的正确项目。

已经有几个人提出了不同的解决方案。

请参见此处:Android无限列表

而且CommonsWare也有一个支持此行为的组件:https://github.com/commonsguy/cwac-endless


啊啊,好古老的模数运算!我发现这是无限选择器或列表视图最好的解决方案! - longi
但是如果我不想要ListView,我想要无限滚动的ScrollView怎么办? - M. Usman Khan
@usman scrollview本身是无限的。但是,如果您想要这种行为,它内部包含的任何内容也必须是无限的。 - FoamyGuy
@FoamyGuy 我的意思是如何在ScrollView中创建LinearLayout,使得滚动不受LinearLayout中项目数量的限制。即LinearLayout中的最后一个项目与第一个项目相连。抱歉,我可能没有表达清楚。 - M. Usman Khan

5
FoamGuy 的回答是正确的。 但是为了让列表向后滚动,就像无限旋转木马一样,我也通过调用以下内容将默认元素设置为 Integer.MAX_VALUE/2 来欺骗系统:
listView.setSelection( Integer.MAX_VALUE/2 );

用户很少会向后滚动十亿个元素,这使得无限循环效果在两个方向上都具有影响。

我还对BaseAdapter自定义实现进行了一些修改:

@Override
public Object getItem(int position) {
    if ( model.getSize()==0 ) {
        return null;
    }

    // mod the list index to the actual element count
    return model.getElementAtPosition( position%model.getSize() );
}

@Override
public long getItemId(int position) {
    if ( model.getSize()==0 ) {
        return -1;
    }

    // same as above
    return model.getElementAtPosition( position%model.getSize() ).id;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if ( model.getSize()==0 ) {
        return null;
    }

    // also make sure to point to the appropriate element in your model otherwise you 
    // would end up building millions of views.
    position%=model.getSize();

    View front= convertView;

    if ( convertView==null ) {
        // inflate/build your list item view
    }

    ...
}

这样列表将像旋转木马一样旋转,无需为不需要的视图进行额外的内存分配。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接