【问题标题】:android:how to make infinite scrollview with endless scrollingandroid:如何通过无限滚动制作无限滚动视图
【发布时间】:2013-10-28 10:36:23
【问题描述】:

我想要一个代码来让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 scrollview android-scrollview


【解决方案1】:

使用 ListViewAdapter 稍作修改以具有“无限”元素

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

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

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

基本上你通过告诉它计数是MAX_INT 来欺骗它,然后当你去获取一个项目时,使用 mod 来获取序列中的正确项目。

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

请看这里:Android Endless List

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

【讨论】:

  • 啊啊,好老模!!这是我发现的无限选择器或列表视图的最佳解决方案!
  • 但是如果我不想要列表视图,我想要无尽的滚动视图呢?
  • @usman 滚动视图本身是无穷无尽的。不管你在里面有什么,但如果你想要那种行为,它也必须是无穷无尽的。
  • @FoamyGuy 我的意思是,如何在滚动视图中创建 LinearLayout,以便滚动不受 LinearLayout 中项目数的限制。即,LinearLayout 中的最后一项似乎与第一项相连。对不起,我可能没有让自己足够清楚。
【解决方案2】:

FoamGuy 的回答是正确的。 但是为了使列表倒退,就像在无限轮播中一样,我还通过调用以下方法将默认元素设置为 Integer.MAX_VALUE/2 来欺骗系统:

listView.setSelection( Integer.MAX_VALUE/2 );

用户不太可能向后滚动 10 亿个元素,这会产生双向无限轮播的效果。

我还对 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
    }

    ...
}

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

【讨论】:

    猜你喜欢
    • 2020-06-22
    • 2014-12-12
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    相关资源
    最近更新 更多