[英]android:how to make infinite scrollview with endless scrolling
我想要一個代碼來讓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>
使用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
然后當你去獲得一個項目使用mod來獲得序列中的正確項目來欺騙它。
有些人已經提出了不同的解決方案。
請參閱此處: Android無盡列表
和CommonsWare也有一個支持這種行為的組件: https : //github.com/commonsguy/cwac-endless
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
}
...
}
這樣,列表就像旋轉木馬一樣旋轉,沒有額外的內存分配,用於不需要的視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.