zoukankan      html  css  js  c++  java
  • android 之 GridView

    GridView 的用法基本与ListView类似。

    程序布局文件main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="@string/hello" />
        <GridView android:id="@+id/gridview01" android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    其中GridView每一行的布局文件grid_row.xml如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:orientation="horizontal">
        <ImageView android:id="@+id/imageview01" android:scaleType="fitXY"
            android:layout_width="50dip" android:layout_height="50dip" />
        <TextView android:id="@+id/tv01" android:layout_width="100dip"
            android:layout_height="wrap_content" android:textSize="24dip"
            android:paddingLeft="5dip" />
        <TextView android:id="@+id/tv02" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textSize="24dip"
            android:paddingLeft="5dip" />
    </LinearLayout>

    在主函数中配置GridView的Adapter:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        gridview = (GridView) findViewById(R.id.gridview01);
        SimpleAdapter adapter = new SimpleAdapter(this, generateDataList(),
                R.layout.grid_row, new String[] { "col1", "col2", "col3" },
                new int[] { R.id.imageview01, R.id.tv01, R.id.tv02 });
        gridview.setAdapter(adapter);
    }

    其中generateDataList()生成Adapter中的数据,其类型为 List<? extends Map<String, ?>>:

    private List<? extends Map<String, ?>> generateDataList() {
            // TODO Auto-generated method stub
            ArrayList<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
            int rowCount=drawableIds.length;
            for(int i=0;i<rowCount;i++){
                HashMap<String, Object> hmap=new HashMap<String, Object>();
                hmap.put("col1", drawableIds[i]);
                hmap.put("col2", this.getResources().getString(nameIds[i]));
                hmap.put("col3", this.getResources().getString(msgIds[i]));
                list.add(hmap);
            }
            return list;
        }

    image

    为GridView添加事件:

    gridview.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    TextView textview = (TextView) findViewById(R.id.textview01);
                    LinearLayout ll = (LinearLayout) arg1;
                    TextView tv01 = (TextView) ll.getChildAt(1);
                    TextView tv02 = (TextView) ll.getChildAt(2);
                    StringBuilder sb = new StringBuilder();
                    sb.append(tv01.getText());
                    sb.append(" ");
                    sb.append(tv02.getText());
                    textview.setText(sb.toString());

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });
            gridview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    LinearLayout ll = (LinearLayout) arg1;
                    TextView tv01 = (TextView) ll.getChildAt(1);
                    TextView tv02 = (TextView) ll.getChildAt(2);
                    StringBuilder sb = new StringBuilder();
                    sb.append(tv01.getText());
                    sb.append(" ");
                    sb.append(tv02.getText());
                    Toast.makeText(mainActivity.this, sb.toString(),
                            Toast.LENGTH_LONG).show();
                }
            });

    imageimage

  • 相关阅读:
    第一期知识点
    如何正确地停止一个线程?
    JVM知识点总览-高级Java工程师面试必备
    常见GC算法,CMS以及G1的垃圾回收过程,CMS的各个阶段哪两个是Stop the world的,CMS会不会产生碎片,G1的优势。
    深入理解分布式事务,高并发下分布式事务的解决方案
    JVM中的逃逸分析
    JVM内存初学 堆、栈、方法区
    JVM方法栈的工作过程,方法栈和本地方法栈有什么区别。
    JVM的基本结构和JVM的内存结构
    一致性hash算法应用场景、详解与实现(JAVA)
  • 原文地址:https://www.cnblogs.com/yechanglv/p/6923060.html
Copyright © 2011-2022 走看看