zoukankan      html  css  js  c++  java
  • 一手遮天 Android

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - view(集合类): GridView 基础 1

    示例如下:

    /view/collection/GridViewDemo1.java

    /**
     * GridView - 网格控件
     *
     * 本例演示
     * 1、GridView 通过 BaseAdapter 显示数据
     * 2、GridView 通过 numColumns 来指定网格控件的列数(网格列宽会平均分,行高和行数会自适应)
     * 3、其他更多知识点请参见 ListView 的说明(GridView 和 ListView 都继承自 AbsListView)
     */
    
    package com.webabcd.androiddemo.view.collection;
    
    import android.content.Context;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class GridViewDemo1 extends AppCompatActivity {
    
        private GridView mGridView1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_collection_gridviewdemo1);
    
            mGridView1 = findViewById(R.id.gridView1);
    
            sample();
        }
    
        private void sample() {
            // 构造数据
            List<MyData> myDataList = new ArrayList<MyData>();
            myDataList.add(new MyData(R.drawable.img_sample_son, "中国", "我是中国"));
            myDataList.add(new MyData(R.drawable.img_sample_son, "美国", "我是美国"));
            myDataList.add(new MyData(R.drawable.img_sample_son, "日本", "我是日本"));
            myDataList.add(new MyData(R.drawable.img_sample_son, "中国2", "我是中国2"));
            myDataList.add(new MyData(R.drawable.img_sample_son, "美国2", "我是美国2"));
            myDataList.add(new MyData(R.drawable.img_sample_son, "日本2", "我是日本2"));
    
            // 实例化自定义的 BaseAdapter
            MyAdapter myAdapter = new MyAdapter(myDataList, this);
            mGridView1.setAdapter(myAdapter);
        }
    
        // 自定义实体类
        class MyData {
            private int _logoId;
            private String _name;
            private String _comment;
    
            public MyData() {
            }
    
            public MyData(int logoId, String name, String comment) {
                this._logoId = logoId;
                this._name = name;
                this._comment = comment;
            }
    
            public int getLogoId() {
                return _logoId;
            }
    
            public String getName() {
                return _name;
            }
    
            public String getComment() {
                return _comment;
            }
    
            public void setLogoId(int logoId) {
                this._logoId = logoId;
            }
    
            public void setName(String name) {
                this._name = name;
            }
    
            public void setComment(String comment) {
                this._comment = comment;
            }
        }
    
        // 自定义 BaseAdapter
        class MyAdapter extends BaseAdapter {
    
            private List<MyData> _myDataList;
            private Context _context;
    
            public MyAdapter(List<MyData> myDataList, Context context) {
                this._myDataList = myDataList;
                this._context = context;
            }
    
            // 需要呈现的 item 的总数
            @Override
            public int getCount() {
                return _myDataList.size();
            }
    
            // 返回指定索引位置的 item 的对象
            @Override
            public Object getItem(int position) {
                return _myDataList.get(position);
            }
    
            // 返回指定索引位置的 item 的 id
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            /*
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = LayoutInflater.from(_context).inflate(R.layout.item_view_collection_gridviewdemo1,parent,false);
    
                ImageView imgLogo = (ImageView) view.findViewById(R.id.imgLogo);
                TextView txtName = (TextView) view.findViewById(R.id.txtName);
                TextView txtComment = (TextView) view.findViewById(R.id.txtComment);
    
                imgLogo.setBackgroundResource(_myDataList.get(position).getLogoId());
                txtName.setText(_myDataList.get(position).getName());
                txtComment.setText(_myDataList.get(position).getComment());
    
                return view;
            }
            */
    
            // 每构造一个 item 就会调用一次 getView() 来获取这个 item 的 view
            // 数据量不大的话就可以像上面那样写,数量大的话则可以像下面这样写
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;
                if (convertView == null) {
                    convertView = LayoutInflater.from(_context).inflate(R.layout.item_view_collection_gridviewdemo1, parent, false);
    
                    holder = new ViewHolder();
                    holder.imgLogo = (ImageView) convertView.findViewById(R.id.imgLogo);
                    holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
                    holder.txtComment = (TextView) convertView.findViewById(R.id.txtComment);
                    convertView.setTag(holder); // 将 holder 保存到 convertView 中
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
    
                holder.imgLogo.setBackgroundResource(_myDataList.get(position).getLogoId());
                holder.txtName.setText(_myDataList.get(position).getName());
                holder.txtComment.setText(_myDataList.get(position).getComment());
    
                return convertView;
            }
    
            class ViewHolder {
                ImageView imgLogo;
                TextView txtName;
                TextView txtComment;
            }
        }
    }
    
    

    /layout/activity_view_collection_gridviewdemo1.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="vertical">
    
        <!--
            numColumns - 指定网格控件的列数(网格列宽会平均分,行高和行数会自适应)
        -->
        <GridView
            android:id="@+id/gridView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="2" />
    
    </LinearLayout>
    
    

    /layout/item_view_collection_gridviewdemo1.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:paddingLeft="10dp"
        android:orientation="horizontal">
    
        <ImageView
            android:id="@+id/imgLogo"
            android:layout_width="64dp"
            android:layout_height="64dp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/txtName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#1D1D1D"
                android:textSize="24sp" />
    
            <TextView
                android:id="@+id/txtComment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#B4B4B4"
                android:textSize="14sp" />
    
        </LinearLayout>
    
    </LinearLayout>
    
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    GitHub 的企业版
    我的Tag列表
    .net开发者对android开发一周的学习体会
    Ajax简单聊天B/S
    C#设计模式——享元模式(Flyweight Pattern)
    mongodb的sharding架构搭建
    Java设计模式
    LMAX架构
    Winform开发的常用类库
    C#设置本地网络(DNS、网关、子网掩码、IP)
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_collection_GridViewDemo1.html
Copyright © 2011-2022 走看看