加ProgressBar以及实现延时操作

用GridView实现Gallery的效果再做一个处理,假设图片加载是通过服务器端的,那么在没有加载完毕的时候界面上要有个ProgressBar显示,当加载完毕后,取消ProgressBar。

image

这里是示意性的代码,图片是一上来就加载了,实际情况应该是只看到ProgressBar。这里需要使用FrameLayout,它可以让多个视图重叠在一起。

看布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="200dp">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="最近访问人物" />
    <FrameLayout android:layout_width="fill_parent"
        android:layout_height="match_parent">
        <HorizontalScrollView android:layout_width="fill_parent"
            android:layout_height="160dp">
            <FrameLayout android:layout_width="fill_parent"
                android:layout_height="match_parent">
                <LinearLayout android:layout_width="1100dp"
                    android:layout_height="match_parent" android:orientation="horizontal">
                    <FrameLayout android:layout_width="fill_parent"
                        android:layout_height="match_parent">
                        <GridView android:id="@+id/grid" android:layout_width="fill_parent"
                            android:gravity="center" android:layout_height="fill_parent"
                            android:horizontalSpacing="1.0dip" android:verticalSpacing="1.0dip"
                            android:stretchMode="spacingWidthUniform" android:numColumns="auto_fit"
                            android:columnWidth="80dip">
                        </GridView>
                    </FrameLayout>
                </LinearLayout>
            </FrameLayout>
        </HorizontalScrollView>
        <ProgressBar android:id="@+id/p1" android:layout_gravity="center"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </FrameLayout>
</LinearLayout>

在代码中调用:

bar.setVisibility(ProgressBar.GONE);

即可让ProgressBar在界面中消失。

这里因为是模拟正式环境,做了个延时取消ProgressBar。使用了Android Handler和Message机制。其实自己写个Timer或者线程啥的都可以完成任务。但不是最佳实践。

这是在Activity的onCreate方法中实现的延时关闭ProgressBar的代码片段:

Message message=new Message();
message.obj=(ProgressBar) headerView.findViewById(R.id.p1);;
new Handler(){
    public void handleMessage(Message msg) {
        ProgressBar bar=(ProgressBar) msg.obj;
        bar.setVisibility(ProgressBar.GONE);
    }
}.sendMessageDelayed(message,1000*10);

 

全部源代码:

http://easymorse.googlecode.com/svn/tags/grid.demo-0.2.0/

PDF格式創作    发送文章为PDF   

这篇文章上的评论的 RSS feed TrackBack URI

Leave a Reply