• Android基础——网格视图和适配器,自定义适配器的使用


    额外新建一个layout,用来布局ImageView组件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="100dp"
            android:layout_height="75dp"
            android:id="@+id/imageView"
            />
    </LinearLayout>

    网格视图的布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
    <GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3">
    </GridView>
    
    
        </RelativeLayout>

    java的调用,使用了适配器SimpleAdapter,将ImageView通过适配器交给GridView

    package com.example.myhighuiiii;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.SimpleAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MainActivity extends AppCompatActivity {
    
        private int[] picture = new int[]{
                R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d
        };
        ImageView imageView = null;
        GridView gridView = null;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            gridView = (GridView) findViewById(R.id.gridView);
            //用一个List来存图片对象
            List<Map<String,Object>> listItem = new ArrayList<Map<String, Object>>();
            //把map对象添加到list中
            for(int i=0;i<picture.length;i++){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("image",picture[i]);
                listItem.add(map);
            }
            //创建simpleAdapter对象
            SimpleAdapter simpleAdapter = new SimpleAdapter(//构造器总共有五个参数
                    this,listItem,R.layout.celi,new String[]{"image"},new int[]{R.id.imageView});
            //为网格视图设置适配器
            gridView.setAdapter(simpleAdapter);
        }
    }

     自定义适配器使用

    layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <GridView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="auto_fit"
            android:gravity="center"
            android:verticalSpacing="5dp"
            android:columnWidth="100dp"
            />
    
    
    </LinearLayout>

    java调用代码

    package com.example.myhighuiiii;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.app.Activity;
    import android.content.Context;
    import android.media.Image;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.GridLayout;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.SimpleAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MainActivity extends Activity {
    
        private int[] picture = new int[]{
                R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d
        };
        ImageView imageView = null;
        GridView gridView = null;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //为网格视图设置适配器
            gridView = findViewById(R.id.gridView);
            gridView.setAdapter(new ImageAdpater(MainActivity.this));
        }
    
        //自定义适配器
        public class ImageAdpater extends BaseAdapter{
            private Context mContext;
            public ImageAdpater(Context context){
                mContext = context;
            }
            @Override//返回图片数组长度
            public int getCount() {
                return picture.length;
            }
            @Override
            public Object getItem(int position) {
                return null;
            }
            @Override
            public long getItemId(int position) {
                return 0;
            }
            //创建一个新的ImageView,用来显示图片,并且将这个图片
            //通过适配器加载到新的ImageView中
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imageView = null;
                if(convertView==null){
                    //如果传过来的值为空,新建一个
                    imageView = new ImageView(mContext);
                    //设置宽和高
                    imageView.setLayoutParams(new GridView.LayoutParams(100,90));
                    //设置拉伸方式:保持纵横比
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                }
                else {
                    //将传过来的值赋给ImageView即可
                    imageView = (ImageView) convertView;
                }
                //设置图片资源
                imageView.setImageResource(picture[position]);
                return imageView;
            }
        }
    }
  • 相关阅读:
    资源 | 辟谣平台
    数据分析常用思维
    数据分析常用工具
    杂谈 | 标准化和个性化
    杂谈 | 学以致用
    杂谈 | 工具思维的陷阱
    杂谈 | 习得性无助&习得性乐观
    spark连接mysql
    spark累加器
    java操作excel数据写入map集合并按照value排序
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12233124.html
Copyright © 2020-2023  润新知