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


    GridView:网格列表,也支持适配器。

    package com.sxt.day05_01;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.AdapterView.OnItemLongClickListener;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.sxt.day05_01.entity.GeneralBean;
    
    public class MainActivity extends Activity {
        GridView mgvGeneral;//MVC的V层,
        List<GeneralBean> mGenerals;//MVC的M层,代表十个军事家的集合
        GeneralAdapter mAdapter;//MVC的C层,
        int[] resid={
            R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
            R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
            R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
            R.drawable.zhuyuanzhang
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initData();//初始化数据
            initView();
            setListener();
        }
        
        private void setListener() {
            setOnItemClickListener();
            setOnItemLongClickListener();
            
        }
    
        private void setOnItemLongClickListener() {
            mgvGeneral.setOnItemLongClickListener(new OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被长按", 2000).show();
                    return true;
                }
            });
        }
    
        private void setOnItemClickListener() {
            mgvGeneral.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被短按", 2000).show();
                }
            });
        }
    
        private void initView() {
            mgvGeneral=(GridView) findViewById(R.id.gvGeneral);//获取gvGeneral
            mAdapter=new GeneralAdapter();//创建适配器
            mgvGeneral.setAdapter(mAdapter);//给gvGeneral设置适配器
        }
    
        private void initData() {
            //将资源中的字符串组数转换为Java数组
            String[] names=getResources().getStringArray(R.array.generals);
            mGenerals=new ArrayList<GeneralBean>();
            for (int i = 0; i < names.length; i++) {
                GeneralBean bean=new GeneralBean(resid[i], names[i]);
                mGenerals.add(bean);
            }
        }
    
        //适配器
        class GeneralAdapter extends BaseAdapter{
    
            @Override
            public int getCount() {
                return mGenerals.size();
            }
    
            @Override
            public GeneralBean getItem(int position) {
                return mGenerals.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            //得到V层的一行
            public View getView(int position, View convertView, ViewGroup parent) {
                //Inflate可用于将一个xml中定义的布局控件找出来,获取一行的布局item_generals.xml并要转换为View类型的对象
                /*
                <?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="wrap_content"
                    android:orientation="vertical" >
                    <ImageView 
                        android:id="@+id/ivThumb"
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/baiqi"/>
                    <TextView 
                        android:id="@+id/tvName"
                        android:layout_width="80dp"
                        android:layout_height="wrap_content"
                        android:text="白起"
                        android:textSize="20sp"
                        android:gravity="center_horizontal"/>
                </LinearLayout>*/
                //
                View layout=View.inflate(MainActivity.this, R.layout.item_generals, null);
                //设置该一行
                ImageView ivThumb=(ImageView) layout.findViewById(R.id.ivThumb);
                TextView tvName=(TextView) layout.findViewById(R.id.tvName);
                GeneralBean bean=mGenerals.get(position);
                ivThumb.setImageResource(bean.getResid());
                tvName.setText(bean.getName());
                return layout;//返回一行的View即item_generals.xml的LinearLayout
            }
        }
    }

    main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <GridView
            android:id="@+id/gvGeneral"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnWidth="60dp"
            android:horizontalSpacing="2dp"
            android:verticalSpacing="5dp"
            android:numColumns="auto_fit"/>
    
    </RelativeLayout>
    item_generals.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="wrap_content"
        android:orientation="vertical" >
        <ImageView 
            android:id="@+id/ivThumb"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:scaleType="fitXY"
            android:src="@drawable/baiqi"/>
        <TextView 
            android:id="@+id/tvName"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="白起"
            android:textSize="20sp"
            android:gravity="center_horizontal"/>
    </LinearLayout>

    GeneralBean.java

    public class GeneralBean {
    
        private int resid;//图片的id值
        private String name;//军事家的姓名
        public int getResid() {
            return resid;
        }
  • 相关阅读:
    链表--判断一个链表是否为回文结构
    矩阵--“之”字形打印矩阵
    二叉树——平衡二叉树,二叉搜索树,完全二叉树
    链表--反转单向和双向链表
    codeforces 490C. Hacking Cypher 解题报告
    codeforces 490B.Queue 解题报告
    BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告
    codeforces 488A. Giga Tower 解题报告
    codeforces 489C.Given Length and Sum of Digits... 解题报告
    codeforces 489B. BerSU Ball 解题报告
  • 原文地址:https://www.cnblogs.com/yaowen/p/4887633.html
Copyright © 2011-2022 走看看