Android 分享机顶盒项目的封装类《GridView》(三)(转)

简介: Android 分享机顶盒项目的封装类《GridView》(三)(转)
     public   void  setOnLongPressListener(onLongPressExt longPress) {
         this .onLongPress  =  longPress;
    }
     public   void  NotifyDataChange() {
        removeAllViews();
    }
     void  bindView() {
        removeAllViews();
         int  count  =  adapter.getCount();
        TableCell[] cell  =   null ;
         int  j  =   0 ;
        LinearLayout layout;
        tableRowsList  =   new  ArrayList < HashMap < String, Object >> ();
         for  ( int  i  =   0 ; i  <  count; i ++ ) {
            j ++ ;
            final  int  position  =  i;
             if  (j  >  getColumnCount()  ||  i  ==   0 ) {
                cell  =   new  TableCell[getColumnCount()];
            }
            final View view  =  adapter.getView(i,  null ,  null );
            view.setOnTouchListener( new  OnTouchListener() {
                @Override
                 public  boolean onTouch(View v, MotionEvent  event ) {
                     //  TODO Auto-generated method stub
                    unCheckPressed();
                    checkRowID  =   - 1 ;
                    checkColumnID  =   - 1 ;
                     if  (onItemClickEvent  !=   null ) {
                        onItemClickEvent.onItemClick(position,  event , view);
                    }
                     return   false ;
                }
            });
            view.setOnLongClickListener( new  OnLongClickListener() {
                @Override
                 public  boolean onLongClick(View v) {
                     if  (onLongPress  !=   null ) {
                        onLongPress.onLongPress(v);
                    }
                     return   true ;
                }
            });
            cell[j  -   1 ]  =   new  TableCell(view);
             if  (j  ==  getColumnCount()) {
                lastRowCount  =  j;
                j  =   0 ;
                HashMap < String, Object >  map  =   new  HashMap < String, Object > ();
                TableRow tr  =   new  TableRow(cell);
                map.put( " tableRow " , tr);
                tableRowsList.add(map);
                layout  =   new  LinearLayout(getContext());
                addLayout(layout, cell, tr.getSize(), tr);
            }  else   if  (i  >=  count  -   1   &&  j  >   0 ) {
                lastRowCount  =  j;
                HashMap < String, Object >  map  =   new  HashMap < String, Object > ();
                TableRow tr  =   new  TableRow(cell);
                map.put( " tableRow " , tr);
                tableRowsList.add(map);
                layout  =   new  LinearLayout(getContext());
                addLayout(layout, cell, j, tr);
            }
        }
    }
     private   void  addLayout(LinearLayout layout, TableCell[] cell,  int  size,
            TableRow tr) {
        LinearLayout.LayoutParams  params   =   new  LinearLayout.LayoutParams( 130 ,
                 110 );
        layout.setGravity(Gravity.LEFT);
        layout.setOrientation(LinearLayout.HORIZONTAL);
         for  ( int  k  =   0 ; k  <  size; k ++ ) {
            View remoteView  =  (View) tr.getCellValue(k).getValue();
            layout.addView(remoteView, k,  params );
        }
        LinearLayout.LayoutParams firstParams  =   new  LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        firstParams.leftMargin  =   60 ;
        addView(layout, firstParams);
    }
     public   void  setAdapter(AppsAdapter appsAdapter) {
         this .adapter  =  appsAdapter;
         this .setOrientation(LinearLayout.VERTICAL);
        bindView();
    }
     public   void  checkPressed( int  tableRowId,  int  tableRowColumnId) {
        ViewGroup view  =  (ViewGroup)  this .getChildAt(tableRowId);
        checkColumnID  =  tableRowColumnId;
        checkRowID  =  tableRowId;
        changeImageState(view.getChildAt(tableRowColumnId), app);
    }
     public   void  onClick( int  tableRowId,  int  tableRowColumnId, Context context) {
        LinearLayout view  =  (LinearLayout) ((ViewGroup)  this
                .getChildAt(tableRowId)).getChildAt(tableRowColumnId);
        TextView tv  =  (TextView) view.findViewById(R.id.folder);
        final String[] name  =  tv.getText().toString().split( " - " );
        Intent intent  =   null ;
         if  (name[ 0 ].toString().equals( " com.android.contacts " )) {
             if  (name[ 1 ].toString().equals(
                     " com.android.contacts.DialtactsActivity " )) {
                intent  =   new  Intent(Intent.ACTION_DIAL);
            }
             if  (name[ 1 ].toString().equals(
                     " com.android.contacts.DialtactsContactsEntryActivity " )) {
                intent  =   new  Intent(Intent.ACTION_CALL_BUTTON);
            }
        }  else  {
            intent  =  getContext().getPackageManager()
                    .getLaunchIntentForPackage(name[ 0 ].toString());
        }
        context.startActivity(intent);
    }
     /* *
     * 改变图片状态
     * 
     * @param v
     * @param list
      */
     private   void  changeImageState(View v, List < HashMap < String, Object >>  list) {
         int  size  =  list.size();
         for  ( int  i  =   0 ; i  <  size; i ++ ) {
            View view  =  (View) list. get (i). get ( " touch " );
            view.setPressed( false );
            list.remove(i);
        }
        v.setPressed( true );
        HashMap < String, Object >  map  =   new  HashMap < String, Object > ();
        map.put( " touch " , v);
        list.add(map);
    }
     public   void  unCheckPressed() {
         if  (checkColumnID  !=   - 1   &&  checkRowID  !=   - 1 ) {
            ViewGroup view  =  (ViewGroup)  this .getChildAt(checkRowID);
            view.getChildAt(checkColumnID).setPressed( false );
        }
    }
     public   class  TableRow {
         private  TableCell[] cell;
         public  TableRow(TableCell[] cell) {
             this .cell  =  cell;
        }
         public   int  getSize() {
             return  cell.length;
        }
         public  TableCell getCellValue( int  index) {
             if  (index  >=  getSize()) {
                 return   null ;
            }
             return  cell[index];
        }
         public   int  getCellCount() {
             return  cell.length;
        }
         public   int  getLastCellCount() {
             return  lastRowCount;
        }
    }
     static   public   class  TableCell {
         private  Object value;
         public  TableCell(Object value) {
             this .value  =  value;
        }
         public  Object getValue() {
             return  value;
        }
    }
}

每行显示的LAYOUT文件:

< LinearLayout  android:orientation ="vertical"
    android:background ="@drawable/lessbtn"  android:gravity ="center"
    android:layout_width ="fill_parent"  android:id ="@+id/grid_layout"
    android:layout_height ="fill_parent"  xmlns:android ="https://schemas.android.com/apk/res/android" > 
     < ImageView  android:id ="@+id/btn_appicon"
        android:layout_width ="55dip"  android:layout_height ="55dip" ></ ImageView >
     < TextView  android:id ="@+id/tv_name"  android:layout_width ="wrap_content"
        android:textColor ="#030303"  android:layout_height ="wrap_content" ></ TextView >
     < TextView  android:id ="@+id/folder"  android:layout_width ="wrap_content"
        android:visibility ="invisible"  android:layout_height ="wrap_content" ></ TextView >
</ LinearLayout >

完成这一系列的编写后,你就可以在xml直接写或者在JAVA文件里面new 出来,但注意要设置它每列显示的个数。

下篇将讲述如何实现手势切屏,如何实现分页显示数据,如何实现封装分页控件。

相关文章
|
5天前
|
Android开发
Android 分享机顶盒项目的封装类《GridView》(二)(转)
Android 分享机顶盒项目的封装类《GridView》(二)(转)
16 2
|
3天前
|
达摩院 安全 Java
80 PM撸代码之Android【武侠讲封装、继承、多态】
80 PM撸代码之Android【武侠讲封装、继承、多态】
8 0
|
5天前
|
Android开发
Android教程之Android 用户界面-表格视图(GridView)
Android教程之Android 用户界面-表格视图(GridView)
12 1
|
3天前
|
设计模式 算法 前端开发
Android面经分享,失业两个月,五一节前拿到Offer,设计思想与代码质量优化+程序性能优化+开发效率优化
Android面经分享,失业两个月,五一节前拿到Offer,设计思想与代码质量优化+程序性能优化+开发效率优化
|
3天前
|
XML Android开发 数据格式
ConstraintLayout 2,Android高级开发面试
ConstraintLayout 2,Android高级开发面试
|
2天前
|
编解码 数据库 Android开发
安卓应用开发:打造高效用户界面的五大技巧
【5月更文挑战第18天】在竞争激烈的应用市场中,一个流畅且直观的用户界面(UI)对于安卓应用的成功至关重要。本文将探讨五种提升安卓应用用户界面性能的技巧,包括合理布局设计、优化资源使用、利用硬件加速、内存管理以及响应式编程。通过这些方法,开发者可以创建出既美观又高效的应用体验,从而吸引和保留用户。
|
3天前
|
JSON Android开发 数据格式
Android框架-Google官方Gson解析,android开发实验报告总结
Android框架-Google官方Gson解析,android开发实验报告总结
|
3天前
|
前端开发 Android开发
Android架构组件JetPack之DataBinding玩转MVVM开发实战(四)
Android架构组件JetPack之DataBinding玩转MVVM开发实战(四)
Android架构组件JetPack之DataBinding玩转MVVM开发实战(四)
|
3天前
|
安全 Linux Android开发
Android最强保活黑科技的最强技术实现,2024年最新阿里资深Android开发带你搞懂Framework
Android最强保活黑科技的最强技术实现,2024年最新阿里资深Android开发带你搞懂Framework
Android最强保活黑科技的最强技术实现,2024年最新阿里资深Android开发带你搞懂Framework