6.6 程序锁界面

1. 在针布局中创建unlockListView和lockedListView 

2. 在lockActivity中, 分明填充两个listView,  点击未加锁textView时,lockedListView 不可见

3. 创建已加锁程序数据库,并创建dao层, 操作该数据库

4. 分别给listView中的条目设置点击事件, 点击unlockListView条目对象, 把该对象从unlockList集合总移除,并添加到lockedList集合中, 且调用dao中方法将该条目对应程序的包名添加到已加锁程序数据库中

5. 给点击的listview中的item设置移动动画

 

 


public
  class   AppLockActivity   extends   Activity   implements   OnClickListener {

       private   static   final   String   TAG   =   "AppLockActivity" ;
       private   TextView   tv_unlock ;
       private   TextView   tv_locked ;
       private   ListView   lv_unlock ;
       private   ListView   lv_locked ;
       private   LinearLayout    lock_loading ;
      
       private   AppInfoBean   appInfoBean ;
       private   List<AppInfoBean>   allAppInfos ;
       private   List<AppInfoBean>   unLockAppInfos ;
       private   List<AppInfoBean>   LockedAppInfos ;
      
       private   MyAdapter    unlock_adapter ;
       private   MyAdapter    locked_adapter ;
      
       private   LinearLayout    ll_unlock ;
       private   LinearLayout      ll_locked ;
       private   AppLockDao    dao ;
       private   TextView     tv_unlock_count ;
       private   TextView     tv_locked_count ;

      
       /**
       * 消息处理机制
       */
       private   Handler    handler =   new   Hand ler(){
               @Override
               public   void   handleMessage(Message msg) {
                  super .handleMessage(msg);
                  lock_loading .setVisibility(View.   GONE );     //移除进度条
             
                  //未添加锁程序界面适配器
                   unlock_adapter =   new   MyAdapter(   true );
                   lv_unlock .setAdapter(   unlock_adapter );
                
                   //已添加锁程序界面适配器
                   locked_adapter =   new   MyAdapter(   false );
                   lv_locked .setAdapter(   locked_adapter );
              
               
            }
            
      };
      
      
       @Override
       protected   void   onCreate(Bundle savedInstanceState) {
          super .onCreate(savedInstanceState);
            setContentView(R.layout.   activity_app_lock );
            
               allAppInfos =   new   ArrayList<AppInfoBean>();
               dao =   new   AppLockDao(getApplicationContext());
               //找到到关心的控件
               tv_unlock =(TextView) findViewById(R.id.   tv_unlock );
               tv_locked =(TextView) findViewById(R.id.   tv_locked );
               lv_unlock =(ListView) findViewById(R.id.   lv_unlock );
               lv_locked =(ListView) findViewById(R.id.   lv_locked );
            
               lock_loading =(LinearLayout) findViewById(R.id. lock_loading   );
               tv_unlock_count =(TextView) findViewById(R.id. tv_unlock_count   );
               tv_locked_count =(TextView) findViewById(R.id. tv_locked_count   );
            
               ll_unlock =(LinearLayout) findViewById(R.id.   ll_unlock );
               ll_locked =(LinearLayout) findViewById(R.id.   ll_locked );
            
               tv_locked .setOnClickListener(   this );
             tv_unlock .setOnClickListener(   this );
          
          
             //给未加锁程序的 lv设置点击事件     添加到已枷锁集合
             lv_unlock .setOnItemClickListener(   new   OnItemClickListener() {

                     @Override
                     public   void   onItemClick(AdapterView<?> parent,   final   View view,
                                 final   int   position,   long   id) {
                        
                         
                             //创建移动动画
                           Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim. tran_lock_right );
                           view.startAnimation(animation);
                        
                        
                           //在子线程中开启动画,在主线程中更新 ui
                           new   Thread(){
                                 public   void   run() {
                                  try   {
                                    Thread. sleep(200);
                                      
                                         //更新ui
                                       runOnUiThread(   new   Runnable() {
                                          
                                             @Override
                                             public   void   run() {
                                                   appInfoBean =   unLockAppInfos .get(position);     //初始化appbean
                                                
                                                   //添加程序到已加锁
                                                   LockedAppInfos .add(   appInfoBean );
                                                   dao .add(   appInfoBean .getPackName());
                                                
                                                   //在未加锁中删除程序
                                                   unLockAppInfos .remove(position);
                                                
                                                   //通知界面更新
                                               unlock_adapter .notifyDataSetChanged();
                                               locked_adapter .notifyDataSetChanged();
                                          
                                                
                                          }
                                    });
                                    
                                    
                              }   catch   (InterruptedException e) {
                                    
                                    e.printStackTrace();
                              }
                                    
                                    
                              };
                              
                              
                        }.start();
                        
                        
                        
                        
                        
                    
                  }

                    
            });
          
          
             //给加锁的lv设置条目点击事件    添加到未加锁集合
             lv_locked .setOnItemClickListener(   new   OnItemClickListener() {

                     @Override
                     public   void   onItemClick(AdapterView<?> parent,   final   View view,
                                 final   int   position,   long   id) {
                        
                        
                            //创建移动动画
                           Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim. tran_lock_left );
                           view.startAnimation(animation);
                        
                        
                           //在子线程中开启动画,在主线程中更新 ui
                           new   Thread(){
                                 public   void   run() {
                                  try   {
                                    Thread. sleep(200);
                                      
                                         //更新ui
                                       runOnUiThread(   new   Runnable() {
                                          
                                             @Override
                                             public   void   run() {
                                                
                                                   appInfoBean =   LockedAppInfos .get(position);   //初始化已加锁集合中 appbean
                                                      
                                                         //添加程序到未加锁
                                                         unLockAppInfos .add(   appInfoBean );
                                                      
                                                      
                                                         //在已加锁中删除程序
                                                         LockedAppInfos .remove(position);
                                                         dao .delete(   appInfoBean .getAppName());
                                                      
                                                      
                                                      
                                                         //通知界面更新
                                                     unlock_adapter .notifyDataSetChanged();
                                                     locked_adapter .notifyDataSetChanged();
                                                  
                                                
                                                      
                                          }
                                    });
                                    
                                    
                              }   catch   (InterruptedException e) {
                                    
                                    e.printStackTrace();
                              }
                                    
                                    
                              };
                              
                              
                        }.start();
                        
                        
                        
             
                  }
            });
          
          
          
          
          
          
            
            
               //填充数据到集合列表
             fillDateToList();
            
            
            
            
      }

  
       /*
       * 填充数据
       */
       private   void   fillDateToList() {
               lock_loading .setVisibility(View.   VISIBLE );
      
               new   Thread(){
                     public   void   run() {
                           allAppInfos = AppManager.getAppInfo(getApplicationContext());
                     
                           //初始化集合
                           unLockAppInfos =   new   ArrayList<AppInfoBean>();
                           LockedAppInfos =   new   ArrayList<AppInfoBean>();
                        for   (AppInfoBean appinfo :   allAppInfos ) {
                                 if (   dao .find(appinfo.getPackName())){
                                       LockedAppInfos .add(appinfo);
                              }   else {
                                       unLockAppInfos .add(appinfo);
                              }
                        }
                     
                     
                           handler .sendEmptyMessage(0);
                        
                  };
            }.start();
                  
            
            
      }
      
      
      
       /**
       * 创建适配器
       */
       public   class   MyAdapter   extends   BaseAdapter{
               private   boolean     unlocked =   false ;

      
                     @Override
                     public   int   getCount() {
                           if (   unlocked ){
                                 tv_unlock_count .setText(   "未加锁" + unLockAppInfos   .size()+ "个"   );
                                 return   unLockAppInfos .size();
                        }   else {
                                 tv_locked_count .setText(   "已加锁" + LockedAppInfos   .size()+ "个"   );
                                 return   LockedAppInfos .size();
                              
                        }
                        
                  }
                  
                     @Override
                     public   View getView(   int   position, View convertView, ViewGroup parent) {
                        View view=   null ;
                        ViewHolder holder ;
                           if (convertView!=   null &&convertView   instanceof   LinearLayout){
                               view=convertView;
                        holder=(ViewHolder) view.getTag();     //复用孩子的id

                              
                        }   else {
                              view=View. inflate(getApplicationContext(), R.layout. list_app_locked ,   null   );
                              holder=   new   ViewHolder();
                              holder.   iv_icon =(ImageView) view.findViewById(R.id. iv_icon   );
                              holder.   tv_name =(TextView) view.findViewById(R.id. tv_name   );
                              holder.   iv_status =(ImageView)view.findViewById(R.id.   iv_status );
                              
                              view.setTag(holder);
                        }
                        
                        
                           //填充未加锁和已加锁界面
                           if (   unlocked ){
                              
                              AppInfoBean unLockAppInfo= unLockAppInfos .get(position);
                              
                                 //给关心的控件设置值
                              holder.   iv_icon .setImageDrawable(unLockAppInfo.getIcon());
                              holder.   tv_name .setText(unLockAppInfo.getAppName());
                              holder.   iv_status .setImageResource(R.drawable.   unlock );
                        }   else {
                              
                              AppInfoBean LockAppInfo= LockedAppInfos .get(position);
                              
                                 //给关心的控件设置值
                              holder.   iv_icon .setImageDrawable(LockAppInfo.getIcon());
                              holder.   tv_name .setText(LockAppInfo.getAppName());
                              holder.   iv_status .setImageResource(R.drawable.   lock );
                              
                        }
                        
                           return   view;
                  }
                  
                  
                     @Override
                     public   Object getItem(   int   position) {
                        
                           return   null ;
                  }


                     @Override
                     public   long   getItemId(   int   position) {
                           //   TODO   Auto-generated method stub
                           return   0;
                  }

            
                  

                     public   MyAdapter(   boolean   unlocked) {
                                 super ();
                                 this .   unlocked   = unlocked;
                            }



      }
      
      
       static   class   ViewHolder{
               private   ImageView    iv_icon ;
               private   TextView    tv_name ;
               private   ImageView    iv_status ;
            
      }


      
       @Override
       public   void   onClick(View v) {
               switch   (v.getId()) {
               case   R.id.   tv_unlock :
                     tv_unlock .setBackgroundResource(R.drawable.   tab_left_pressed );
                     tv_locked .setBackgroundResource(R.drawable.   tab_right_default );
                     ll_locked .setVisibility(View.   INVISIBLE );
                     ll_unlock .setVisibility(View.   VISIBLE );
                     break ;

               case   R.id.   tv_locked :
                     tv_unlock .setBackgroundResource(R.drawable.   tab_left_default );
                     tv_locked .setBackgroundResource(R.drawable.   tab_right_pressed );
                     ll_locked .setVisibility(View.   VISIBLE );
                     ll_unlock .setVisibility(View.   INVISIBLE );
                     break ;
            }
            
      }
      
      
      
      
}



 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值