美文网首页
Android GridView实现桌面效果

Android GridView实现桌面效果

作者: CrazyBoomer | 来源:发表于2017-03-01 22:59 被阅读0次

    一.在XML文件中设定GridView

        <GridView android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/GL"
            android:horizontalSpacing="10dp"
            android:verticalSpacing="10dp"
            android:numColumns="3" />
    

    二.在Activity中初始化GridView

        private GridView gl;
        protected void onCreate(Bundle savedInstanceState) {
            ······
            gl=(GridView) findViewById(R.id.GL);
        }
    

    三.初始化适配器,并为适配器初始化数据源

        private List<Map<String, Object>> dataList;
        private int[] icon= {R.drawable.address_book,R.drawable.calendar,
        R.drawable.camera,R.drawable.clock,
        R.drawable.games_control,R.drawable.messenger};
        private String[] iconName={"通讯录","日历","相机","时钟","游戏","短信"};
        private SimpleAdapter adapter;
        protected void onCreate(Bundle savedInstanceState) {
            ·······
            dataList=new ArrayList<Map<String,Object>>();
            adapter=new SimpleAdapter(this, gatData(), R.layout.item,
            new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text});
            gl.setAdapter(adapter);
        }
        private List<Map<String, Object>> gatData() {
            // TODO Auto-generated method stub
            for(int i=0;i<icon.length;i++){
                Map<String, Object> map= new HashMap<String, Object>();
                map.put("pic", icon[i]);
                map.put("text", iconName[i]);
                dataList.add(map);
            }
            return dataList;
        }
    
    

    代码理解

    private List<Map<String, Object>> dataList;
    dataList=new ArrayList<Map<String,Object>>();

    初始化数据源dataList

    for(int i=0;i<icon.length;i++){
    Map<String, Object> map=new HashMap<String, Object>();
    map.put("pic", icon[i]);
    map.put("text", iconName[i]);
    dataList.add(map);
    }

    将icon和iconName中的数据通过key(pic、text)-value(icon[i]、iconName[i])映射导入新建立的HashMap中,再将该HashMap加入到数据源的末尾

    adapter=new SimpleAdapter(this, gatData(), R.layout.item,new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text});

    此处new String 中的pic和text分别指向DataList中的key值为pic,text的value,而R.layout.item则是将pic,text通过何种方式显示的布局文件,this则指代该SimpleAdapter所在的上下文

    四.编写item数据布局文件

        <ImageView android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/ic_launcher"
            android:id="@+id/pic"/>
        
        <TextView  android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:id="@+id/text"
            android:textColor="#ffffff"
            android:text="@string/hello_world"/>
    

    五.为实现窗口效果,要在ManiFest文件中选择无标题栏的主题

            android:theme="@android:style/Theme.Black.NoTitleBar" >
    

    六.通过接口方法设置事件监听器

    public class MainActivity extends Activity implements OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            Toast.makeText(this, "我是"+iconName[position],Toast.LENGTH_SHORT).show();
        }
    
    
    
            gl.setOnItemClickListener(this);
    
    

    相关文章

      网友评论

          本文标题:Android GridView实现桌面效果

          本文链接:https://www.haomeiwen.com/subject/nzjqgttx.html