Android View水平滑动与分页加载方案

来源:互联网 发布:医院网络客服工资高吗 编辑:程序博客网 时间:2024/06/01 07:42

需求:实现自定义组合控件(imageView+TextView)水平滑动

实现:

1、使用Gallery,本省自带内存管理,不用考虑View太多导致内存泄露,为了让Gallery 实现无限循环滑动+初始化居中的问题,在Adapter中,getCount中返回 Integer的最大值。

2、但是,Gallery很不好控制,不是左边对齐

3、上网查资料,左边对齐了,NND,右边又没对齐,查资料,一直没解决

Google怪不得不推荐使用Gallery呢!

于是,换思路重来,使用 HorizonScrollView + LinearLayout 实现,而且可以精细控制,而且可以解决一个坑爹的问题:

比如:要一次性加载上百张图片,异步网络请求怎么搞?内存可能泄露?

所以,我可以实现精细的控制,分页加载,监听到HorizonScrollView 滑到最右边或者最左边的事件,滑动到最右边的时候,再次发网络请求,再次加载图片,

每次只加载固定个数的图片,形成缓冲池,类似于ListView下拉加载更多

private static void handleScreenShot(ArrayList<Screenshot> screenshots) {if (null == screenshots || screenshots.size() == 0) {return;}shotView.removeAllViews();for (Screenshot screenshot : screenshots) {View child = (View) LayoutInflater.from(mContext).inflate(R.layout.app_screenshots_item, null);LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(  LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  layout.setMargins(15, 0, 0, 0);  child.setLayoutParams(layout);  shotView.addView(child);//child.setTag(resourceInfo);//child.setOnClickListener(this);ImageView imageView = (ImageView) child.findViewById(R.id.screenshot);AsyncImageTask task = new AsyncImageTask(imageView, Constants.HOST+screenshot.getUrl());task.setDefaultImage(R.drawable.loading);task.setFailImage(R.drawable.loading);TaskQueue.getInstance().addTask(task);}}

 <HorizontalScrollView                        android:id="@+id/screenshots_hScrollview"                        android:layout_width="fill_parent"                        android:layout_height="wrap_content"                        android:layout_marginBottom="10dp"                        android:layout_marginLeft="15dp"                        android:layout_marginRight="5dp"                        android:layout_marginTop="15dp"                        android:scrollbarAlwaysDrawHorizontalTrack="false"                        android:scrollbars="none" >                        <LinearLayout                            android:id="@+id/gallery_shot"                            android:layout_width="wrap_content"                            android:layout_height="wrap_content"                            android:orientation="horizontal" >                        </LinearLayout>                    </HorizontalScrollView>


0 0