zoukankan      html  css  js  c++  java
  • android之gridview图片列表

    本文介绍下android的个grid view的用法,

    gridview是一项显示二维的viewgroup,可滚动的网格。网格项时自动插入的布局使用listadapter。
    1.创建一个新的项目:gridview.

    2,打开 res/layout/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"
        >
    <GridView
             android:id="@+id/gridview"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:columnWidth="90dp"
             android:numColumns="auto_fit"
             android:verticalSpacing="10dp"
             android:horizontalSpacing="10dp"
             android:stretchMode="columnWidth"
             android:gravity="center"
             />
     
    </LinearLayout>

    做好以后就可以在屏幕上显示一个grid view

    3.打开gridview.java首先要导入包:

    package cn.gridview;

    import android.widget.GridView;
    import android.widget.Toast;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.AdapterView;
    import android.view.View;

    在onCreate添加方法:

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            GridView gridview=(GridView)findViewById(R.id.gridview);//找到main.xml中定义gridview 的id
            gridview.setAdapter(new ImageAdapter(this));//调用ImageAdapter.java
            gridview.setOnItemClickListener(new OnItemClickListener(){//监听事件
             public void onItemClick(AdapterView<?> parent, View view, int position, long id)
             {
              Toast.makeText(gridview.this, ""+position,Toast.LENGTH_SHORT).show();//显示信息;
             }
            });
        }

    ImageAdapter.java

    package cn.gridview;

    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.content.Context;
    import android.widget.ImageView;
    import android.widget.GridView;


    public class ImageAdapter extends BaseAdapter {
     private Context mContext;
     
     public ImageAdapter(Context c)
     {
      mContext=c;
     }
     @Override
     public int getCount() {
      // TODO Auto-generated method stub
      return mThumbIds.length;
     }

     @Override
     public Object getItem(int position) {
      // TODO Auto-generated method stub
      return null;
     }

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

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      
      ImageView imageview;
      if(convertView==null)
      {
       imageview=new ImageView(mContext);
       imageview.setLayoutParams(new GridView.LayoutParams(85, 85));
       imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
       imageview.setPadding(8,8,8,8);
     }
      else
      {
       imageview=(ImageView) convertView;
      }
      imageview.setImageResource(mThumbIds[position]);
      return imageview;
      }

     private Integer[] mThumbIds={//显示的图片数组
      
      R.drawable.sample_2,R.drawable.sample_3,
      R.drawable.sample_4,R.drawable.sample_5,
      R.drawable.sample_6,R.drawable.sample_7,
      R.drawable.sample_0,R.drawable.sample_1,
      R.drawable.sample_2,R.drawable.sample_3,
      R.drawable.sample_4,R.drawable.sample_5,
      R.drawable.sample_6,R.drawable.sample_7,
      R.drawable.sample_0,R.drawable.sample_1,
      R.drawable.sample_2,R.drawable.sample_3,
      R.drawable.sample_4,R.drawable.sample_5,
      R.drawable.sample_6,R.drawable.sample_7,
     };
     

    }

    运行后界面如图:

  • 相关阅读:
    python疑难问题---5、二维列表初始化
    python疑难问题---4、python文件读写
    心得体悟帖---200701(你以为后面比当下好,其实还真的不一定)
    心得体悟帖---200701(《隐秘的角落》成熟的孩子小白)
    div垂直居中 css div盒子上下垂直居中
    iOS 7 新版微信 URL 不支持跳转 App Store 的解决方案
    HTML5 LocalStorage 本地存储
    WDCP是什么 关于WDCP的详细介绍
    前端框架用哪个好
    GWT 实现文件上传和下载
  • 原文地址:https://www.cnblogs.com/stulife/p/1752536.html
Copyright © 2011-2022 走看看