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>

4

2 に答える 2

9

「無限」要素を持つようにわずかに変更された aListViewおよび anを使用しますAdapter

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エンドレスリスト

CommonsWare には、この動作もサポートするコンポーネントがあります: https://github.com/commonsguy/cwac-endless

于 2013-10-19T17:51:47.047 に答える
5

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
    }

    ...
}

このようにして、不要なビューに余分なメモリを割り当てることなく、カルーセルのようにリストが回転します。

于 2014-01-18T05:03:09.710 に答える