gridview安卓实现单行多列横向滚动

  <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnCount="1" >

             <HorizontalScrollView
                 android:layout_width="match_parent"
                 android:layout_height="88dp"
                 android:layout_column="0"
                 android:layout_gravity="left|bottom"
                 android:layout_row="0" >

                 <LinearLayout
                     android:id="@+id/linearLayout_gridtableLayout"
                     android:layout_width="1000dp"
                     android:layout_height="79dp"
                     android:orientation="horizontal" >

                     <GridView
                         android:id="@+id/gridview1"
                         android:layout_width="600dp"
                         android:layout_height="match_parent"
                         android:background="#ffffff"
                         android:columnWidth="100dp"
                         android:gravity="center"
                         android:horizontalSpacing="1dp"
                         android:numColumns="6"
                         android:scrollbarAlwaysDrawHorizontalTrack="true"
                         android:scrollbarAlwaysDrawVerticalTrack="true"
                         android:scrollbars="horizontal|vertical"
                         android:verticalSpacing="1dp" />                 </LinearLayout>
             </HorizontalScrollView>

        </GridLayout>

指定其中LinearLayout的宽度就能够实现你GridView的长宽变化,如果它的长超过屏幕,则自动添加水平滚动条。

 private int[] icon = { R.drawable.camera, R.drawable.wifi, R.drawable.temperature,
            R.drawable.lamp, R.drawable.wechat, R.drawable.mic,  };
    private String[] iconName = { "摄像头", "网络", "温湿度", "电器", "微信", "语音" };

    private GridView gview;
    private List<Map<String, Object>> data_list;
    private SimpleAdapter sim_adapter;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                  

         //gridview数据适配器
        gview = (GridView) findViewById(R.id.gridview1);
        //新建List
        data_list = new ArrayList<Map<String, Object>>();
        //获取数据
        getData();
        //新建适配器
        String [] from ={"image","text"};
        int [] to = {R.id.image,R.id.title};
        sim_adapter = new SimpleAdapter(this, data_list, R.layout.items, from, to);
        //配置适配器
        gview.setAdapter(sim_adapter);

   http://www.cnblogs.com/xiaobo-Linux/ qq463431476

    }

      public List<Map<String, Object>> getData(){
            //cion和iconName的长度是相同的,这里任选其一都可以
            for(int i=0;i<icon.length;i++){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("image", icon[i]);
                map.put("text", iconName[i]);
                data_list.add(map);
            }

            return data_list;
        }
        

Fragment里:

oncreate里边初始化数据,oncreateview里边初始化gridview和绑定适配器
 
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                   Bundle savedInstanceState) {
               // TODO Auto-generated method stub
               View view = inflater.inflate(R.layout.gridview,container, false);

              gridview = (GridView) view.findViewById(R.id.gridview1);
                  //新建List
                data_list = new ArrayList<Map<String, Object>>();

        //http://www.cnblogs.com/xiaobo-Linux/ qq463431476
                //获取数据
                getData();
                //新建适配器
                String [] from ={"image","text"};
                int [] to = {R.id.image,R.id.title};
                sim_adapter = new SimpleAdapter(getActivity(), data_list, R.layout.items, from, to);
                //配置适配器
                gridview.setAdapter(sim_adapter);  

               return view;
           }
     public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

        }  

       // http://www.cnblogs.com/xiaobo-Linux/ qq463431476

           public List<Map<String, Object>> getData(){
               //cion和iconName的长度是相同的,这里任选其一都可以
               for(int i=0;i<icon.length;i++){
                   Map<String, Object> map = new HashMap<String, Object>();
                   map.put("image", icon[i]);
                   map.put("text", iconName[i]);
                   data_list.add(map);
               }

               return data_list;
           }
               

实现按钮事件:

           public List<Map<String, Object>> getData(){
               //cion和iconName的长度是相同的,这里任选其一都可以
               for(int i=0;i<icon.length;i++){
                   Map<String, Object> map = new HashMap<String, Object>();
                   map.put("image", icon[i]);
                   map.put("text", iconName[i]);
                   data_list.add(map);
               }
         http://www.cnblogs.com/xiaobo-Linux/ qq463431476 赵存档 (小波)
               // 添加点击事件
               gridview.setOnItemClickListener(new OnItemClickListener() {

                   @Override
                   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                       int index = arg2 + 1;// id是从0开始的,所以需要+1
                       Toast.makeText(getActivity().getApplicationContext(), "你按下了选项:" + index, 0).show();
                       switch(index){
                       case 1:
                        getFragmentManager().beginTransaction()
                        .replace(R.id.container, new Camera()).commit();
                           break;
                       case 2:
                        getFragmentManager().beginTransaction()
                        .replace(R.id.container, new Wifi()).commit();
                           break;
                       case 3:
                        getFragmentManager().beginTransaction()
                        .replace(R.id.container, new Temperature()).commit();
                           break;
                       case 4:
                           getFragmentManager().beginTransaction()
                           .replace(R.id.container, new HomeApplication()).commit();
                           break;
                       case 5:
                           getFragmentManager().beginTransaction()
                           .replace(R.id.container, new Wechat()).commit();
                           break;
                       case 6:
                           getFragmentManager().beginTransaction()
                           .replace(R.id.container, new Voice()).commit();
                           break;
                       case 7:
                           getFragmentManager().beginTransaction()
                           .replace(R.id.container, new Useronline()).commit();
                           break;
                       }
                   }
               }); 

               return data_list;
           }
上一篇:sql语句判断两个时间段是否有交集


下一篇:pbfunc外部函数扩展应用-直接在Datawindow中生成QR二维码,非图片方式