use*_*395 7 android scrollview android-scrollview
我想要一个代码来使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>
Run Code Online (Sandbox Code Playgroud)
使用a ListView
和a Adapter
略微修改以具有"无限"元素
以下是您Adapter
支持此行为的更改:
@Override
public int getCount()
{
return Integer.MAX_VALUE;
}
@Override
public ImageItem getItem(int position)
{
return mItems.get(position % mItems.size());
}
Run Code Online (Sandbox Code Playgroud)
基本上你通过告诉它计数是巧妙的MAX_INT
,然后当你去获得一个项目时使用mod来获得序列中的正确项目.
有些人已经提出了不同的解决方案.
请参阅此处:Android无尽列表
和CommonsWare也有一个支持这种行为的组件:https://github.com/commonsguy/cwac-endless
小智 5
FoamGuy的回答是正确的.但是为了使列表向后移动,如在无限转盘中,我还通过调用以下方法将默认元素设置为Integer.MAX_VALUE/2来欺骗系统:
listView.setSelection( Integer.MAX_VALUE/2 );
Run Code Online (Sandbox Code Playgroud)
用户很可能不会向后滚动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
}
...
}
Run Code Online (Sandbox Code Playgroud)
这样,列表就像旋转木马一样旋转,没有额外的内存分配,用于不需要的视图.
归档时间: |
|
查看次数: |
16003 次 |
最近记录: |