zoukankan      html  css  js  c++  java
  • GridView

    layout代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.my.myapplication.Test_GridView">
    
        <GridView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/gv_1"
            android:numColumns="4"
            android:stretchMode="columnWidth"
            android:horizontalSpacing="30dp"></GridView>
    </LinearLayout>
    View Code

    gridviewlayout代码:

    <?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="110dp"
            android:layout_height="110dp"
            android:src="@drawable/f1"
            android:id="@+id/iv_2"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_9"/>
    </LinearLayout>
    View Code

    Activity代码:

    package com.example.my.myapplication;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    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 java.util.ArrayList;
    import java.util.List;
    
    public class Test_GridView extends AppCompatActivity {
    
        GridView gv_1;
        List<MyClass> liv;
        TextView tv_9;
        ImageView iv_2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test__grid_view);
    
            gv_1=(GridView)findViewById(R.id.gv_1);
            tv_9=(TextView)findViewById(R.id.tv_9);
            iv_2=(ImageView)findViewById(R.id.iv_2);
    
            //1.数据 图片的id
            liv=new ArrayList<>();
            MyClass myClass=new MyClass(R.drawable.f1,"美食1的价格");
            liv.add(myClass);
            liv.add(new MyClass(R.drawable.f2,"美食2的价格"));
            liv.add(new MyClass(R.drawable.f3,"美食3的价格"));
            liv.add(new MyClass(R.drawable.f4,"美食4的价格"));
            liv.add(new MyClass(R.drawable.f5,"美食5的价格"));
            liv.add(new MyClass(R.drawable.f6,"美食6的价格"));
            liv.add(new MyClass(R.drawable.f7,"美食7的价格"));
            liv.add(new MyClass(R.drawable.f8,"美食8的价格"));
            liv.add(new MyClass(R.drawable.f9,"美食9的价格"));
            liv.add(new MyClass(R.drawable.f10,"美食10的价格"));
            ImageAdapter imageAdapter =new ImageAdapter();
            {
                gv_1.setAdapter(imageAdapter);
            }
    
        }
    
        class MyClass
        {
            private int img;
            private String content;
            public  int getImg()
            {
                return img;
            }
            public void setImg(int img)
            {
                this.img=img;
            }
            public String getContent()
            {
                return content;
            }
            public void setContent(String content)
            {
                this.content=content;
            }
            public MyClass(int img,String content)
            {
                this.img=img;
                this.content=content;
            }
        }
    
        //适配器
    
        class ImageAdapter extends BaseAdapter
        {
            @Override
            public int getCount() {
                return liv.size();
            }
    
            @Override
            public Object getItem(int position) {
                return liv.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return 0;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
                //1.得到数据 图片的id
                MyClass myClass=liv.get(position);
    
                //2.准备View
                if (convertView==null)
                {
                    convertView=View.inflate(Test_GridView.this,R.layout.gridview,null);
    
                }
    
                //3.适配
                //转换
                ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_2);
                imageView.setImageResource(myClass.getImg());
    
    //            imageView.setMaxWidth(70);
    //            imageView.setMaxHeight(70);
                //imageView.setLayoutParams(new GridView.LayoutParams(70,70));
    
                TextView textView=(TextView)convertView.findViewById(R.id.tv_9);
                textView.setText(myClass.getContent());
                return convertView;
            }
        }
    }
    View Code

    效果图:

  • 相关阅读:
    大数据时代-散记
    MongoDB命令行操作
    easyui获取日期datebox中的值
    linux文件系统学习
    Linux中部署JAVA程序
    百度Clouda的初步探索
    global.asax?app.config?webconfig??
    Android adb install INSTALL_FAILED_DEXOPT
    Android SharedPreferences 权限设置
    vim 支持 markdown 语法
  • 原文地址:https://www.cnblogs.com/beens/p/5514199.html
Copyright © 2011-2022 走看看