android进度条在gridview中

问题描述

android进度条在gridview中

我的gridview种有个进度条和开始及结束两个按钮,我点击开始,我希望他可以一直按照我的赋值更新进度,当我按下结束按钮时他结束掉。这个怎么实现呢?求大师指点迷津!

解决方案

创建一个服务,这个服务可以读取你的赋值并广播进度,你的界面可以收听这个服务的广播并自己更新进度条。这是通用的做法。

解决方案二:

thread + handler
thread负责赋值的变化,然后发消息
handler负责更新进度条
通过btn的点击响应来控制thread的运行

解决方案三:


myadapter.java(自定义适配器)
tock_on.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    if (tock_flag) {
                    } else {
                        tock_flag = true;
                        tock_on.setBackgroundResource(0);
                        tock_on.setBackgroundResource(R.drawable.tock_on_btn);
                        tock_off.setBackgroundResource(0);
                        tock_off.setBackgroundResource(R.drawable.tock_off_btn);
                        search_voice.setBackgroundResource(0);
                        search_voice.setBackgroundResource(R.drawable.vocie_press);

                        //计时器
                        if (null != timer) {
                            task.cancel();
                            task = null;
                            timer.cancel(); // Cancel timer
                            timer.purge();
                            timer = null;
                            handler.removeMessages(msg.what);
                            mlCount = 0;
                            if (SETTING_SECOND_ID == settingTimerUnitFlg) {
                                // second
                                tvTime.setText(R.string.init_time_second);
                            } else if (SETTING_100MILLISECOND_ID == settingTimerUnitFlg) {
                                // 100 millisecond
                                tvTime.setText(R.string.init_time_100millisecond);
                            }
                        }
                        task = new TimerTask() {
                            public void run() {
                                if (null == msg) {
                                    msg = new Message();
                                } else {
                                    msg = Message.obtain();
                                }
                                msg.what = 1;
                                handler.sendMessage(msg);
                            }

                        };
                        timer = new Timer(true);
                        timer.schedule(task, mlTimerUnit, mlTimerUnit); // set timer duration

                    }
                }

            });
                         tock_off.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    if(tock_flag){//只对说话按钮负责
                        tock_off.setBackgroundResource(0);
                        tock_off.setBackgroundResource(R.drawable.tock_off);
                        tock_on.setBackgroundResource(0);
                        tock_on.setBackgroundResource(R.drawable.tock_on);
                        search_voice.setBackgroundResource(0);
                        search_voice.setBackgroundResource(R.drawable.vocie);

                        tock_flag = false;
                        voice_flag = true;

                        try {
                            task.cancel();
                            timer.cancel(); // Cancel timer
                            timer.purge();
                            handler.removeMessages(msg.what);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }
            });

final Handler mHandler =new Handler(){
                public void handleMessage(Message msg) {
                    if(msg.what==0x111){
                        volum.setProgress(volum_value); //更新进度
                    }else{
                        volum.setVisibility(View.GONE); //设置进度条不显示,并且不占用空间
                    }
                }
            };
  new Thread(new Runnable() {
                    public void run() {
                        while (true) {
                            volum_value = doWork(); //获取耗时操作完成的百分比
                            Message m=new Message();
                            if(tock_flag){
                                m.what=0x111;
                                mHandler.sendMessage(m);    //发送信息
                            }else{
                                m.what=0x110;
                                mHandler.sendMessage(m);    //发送消息
                                break;
                            }
                        }
                    }
                    //模拟一个耗时操作
                    private byte doWork() {
                        volum_value+=Math.random()*10;  //改变完成进度
                        try {
                            Thread.sleep(200);      //线程休眠200毫秒
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        return volum_value; //返回新的进度
                    }
                }).start(); //开启一个线程

解决方案四:


myadapter.java(自定义适配器)
tock_on.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    if (tock_flag) {
                    } else {
                        tock_flag = true;
                        tock_on.setBackgroundResource(0);
                        tock_on.setBackgroundResource(R.drawable.tock_on_btn);
                        tock_off.setBackgroundResource(0);
                        tock_off.setBackgroundResource(R.drawable.tock_off_btn);
                        search_voice.setBackgroundResource(0);
                        search_voice.setBackgroundResource(R.drawable.vocie_press);

                        //计时器
                        if (null != timer) {
                            task.cancel();
                            task = null;
                            timer.cancel(); // Cancel timer
                            timer.purge();
                            timer = null;
                            handler.removeMessages(msg.what);
                            mlCount = 0;
                            if (SETTING_SECOND_ID == settingTimerUnitFlg) {
                                // second
                                tvTime.setText(R.string.init_time_second);
                            } else if (SETTING_100MILLISECOND_ID == settingTimerUnitFlg) {
                                // 100 millisecond
                                tvTime.setText(R.string.init_time_100millisecond);
                            }
                        }
                        task = new TimerTask() {
                            public void run() {
                                if (null == msg) {
                                    msg = new Message();
                                } else {
                                    msg = Message.obtain();
                                }
                                msg.what = 1;
                                handler.sendMessage(msg);
                            }

                        };
                        timer = new Timer(true);
                        timer.schedule(task, mlTimerUnit, mlTimerUnit); // set timer duration

                    }
                }

            });
                         tock_off.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    if(tock_flag){//只对说话按钮负责
                        tock_off.setBackgroundResource(0);
                        tock_off.setBackgroundResource(R.drawable.tock_off);
                        tock_on.setBackgroundResource(0);
                        tock_on.setBackgroundResource(R.drawable.tock_on);
                        search_voice.setBackgroundResource(0);
                        search_voice.setBackgroundResource(R.drawable.vocie);

                        tock_flag = false;
                        voice_flag = true;

                        try {
                            task.cancel();
                            timer.cancel(); // Cancel timer
                            timer.purge();
                            handler.removeMessages(msg.what);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }
            });

final Handler mHandler =new Handler(){
                public void handleMessage(Message msg) {
                    if(msg.what==0x111){
                        volum.setProgress(volum_value); //更新进度
                    }else{
                        volum.setVisibility(View.GONE); //设置进度条不显示,并且不占用空间
                    }
                }
            };
  new Thread(new Runnable() {
                    public void run() {
                        while (true) {
                            volum_value = doWork(); //获取耗时操作完成的百分比
                            Message m=new Message();
                            if(tock_flag){
                                m.what=0x111;
                                mHandler.sendMessage(m);    //发送信息
                            }else{
                                m.what=0x110;
                                mHandler.sendMessage(m);    //发送消息
                                break;
                            }
                        }
                    }
                    //模拟一个耗时操作
                    private byte doWork() {
                        volum_value+=Math.random()*10;  //改变完成进度
                        try {
                            Thread.sleep(200);      //线程休眠200毫秒
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        return volum_value; //返回新的进度
                    }
                }).start(); //开启一个线程

解决方案五:

06-04 10:34:30.390 424-591/? E/Watchdog﹕ !@Sync 12144
06-04 10:34:34.164 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:34.164 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:39.159 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:39.169 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:44.194 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:44.194 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:44.744 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:44.744 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:49.779 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:49.779 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:54.794 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:54.794 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:59.809 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:59.809 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:00.389 424-591/? E/Watchdog﹕ !@Sync 12145
06-04 10:35:04.824 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:04.824 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:09.849 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:09.849 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:14.834 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:14.834 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:15.394 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:15.394 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:20.429 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:20.429 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:25.434 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:25.434 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:30.389 424-591/? E/Watchdog﹕ !@Sync 12146
06-04 10:35:30.439 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:30.439 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:35.444 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:35.444 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:40.459 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:40.459 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:45.463 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:45.463 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:50.478 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:50.478 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:55.493 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:55.493 10502-10502/? E/MtpService﹕ battPlugged Type : 2
这些算么

时间: 2024-09-15 21:26:38

android进度条在gridview中的相关文章

有关android进度条上文件大小的单位的问题

问题描述 有关android进度条上文件大小的单位的问题 我想做一个进度条用来显示文件下载的进度.下面代码中setMax()的参数是整数,大小是文件的字节数,我在上面以单位M显示文件的大小,不知道怎么实现额.还有最后一行setProgress()设置当前下载量显示的也是整型的字节数,怎么兆为单位来显示呢,如文件大小12.5M,当前进度2.3M.求大神指教一下!谢谢! pd.setMax(conn.getContentLength()); //设置成文件的大小 InputStream is = c

Android 进度条使用详解及示例代码_Android

在这里,总结一下loading进度条的使用简单总结一下. 一.说起进度条,必须说说条形进度条,经常都会使用到嘛,特别是下载文件进度等等,还有像腾讯QQ安装进度条一样,有个进度总给人良好的用户体验. 先来找图看看,做这个图完成不用图片就可以做到了. 看下xml布局文件,其实就是直接用xml写的在加两个属性设置一下就好了,一个style,另一个是background. <ProgressBar android:id="@+id/pb_progressbar" style="

各位大神求赐教-struts2 上传视频进度条实现过程中 request对象问题

问题描述 struts2 上传视频进度条实现过程中 request对象问题 List formList = upload.parseRequest(request); 为了实现这条代码,在web.xml中配置了 changeRequest com.dlyk.framework.util.filter.GetServletRequest changeRequest*.action 这个配置就是为了request对象不被struts2 封装,不被封装的requestparseRequest(requ

Android实现九宫格(GridView中各项平分空间)的方法_Android

本文实例讲述了Android实现九宫格(GridView中各项平分空间)的方法.分享给大家供大家参考.具体如下: 项目需要做一个九宫格(也不一定是9的,4宫格.16宫格.4x3宫格...),封了 一个宫格,它能够根据为它分配的空间来自动的调节宫中各项的尺寸. 从TableLayout集成来的,因此如果你直接在设计器上使用该封装的话需要把它自动加进去的那几个TableRow删除一下. 类名为AdvancedGridView,代码如下: import android.content.Context;

android 进度条组件ProgressBar_Android

首先是main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" androi

Android实现九宫格(GridView中各项平分空间)的方法

本文实例讲述了Android实现九宫格(GridView中各项平分空间)的方法.分享给大家供大家参考.具体如下: 项目需要做一个九宫格(也不一定是9的,4宫格.16宫格.4x3宫格...),封了 一个宫格,它能够根据为它分配的空间来自动的调节宫中各项的尺寸. 从TableLayout集成来的,因此如果你直接在设计器上使用该封装的话需要把它自动加进去的那几个TableRow删除一下. 类名为AdvancedGridView,代码如下: import android.content.Context;

Android 进度条显示在标题栏的实现方法

好吧,先给大家展示效果图: xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <Butt

android 进度条更新-Android关于下载进度条更新问题

问题描述 Android关于下载进度条更新问题 @Override protected Bitmap doInBackground(String... params) { Bitmap bitmap=null; ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); InputStream inputStream=null; try { HttpClient httpClient=new DefaultHttpClient(

Android进度条控件progressbar使用方法详解

一.简介 二.方法 1)进度条ProgressBar使用方法 1.在layout布局文件中创建ProgressBar控件 <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:progress="30&