Android Studio 利用 GridView布局实现简单的照片墙

Android Studio 利用 GridView布局实现简单的照片墙,第1张

布局文件:

     1. GridView



      
      

2.item布局文件

        





    

    

java 文件

        1.MainActivtiy

                

package com.example.myimg;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Context context=MainActivity.this;
    private   List list;
    private   GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//获取GridView控件
    gridView =(GridView) findViewById(R.id.grid_view);
//ArrayList 装入 image对象
    list=new ArrayList();
//添加 图片内容
        list.add(new image("图片1",R.drawable.iv_icon_1));
        list.add(new image("图片2",R.drawable.iv_icon_2));
        list.add(new image("图片3",R.drawable.iv_icon_3));
        list.add(new image("图片4",R.drawable.iv_icon_4));
        list.add(new image("图片5",R.drawable.iv_icon_5));
        list.add(new image("图片6",R.mipmap.ic_launcher));

//初始化adapter
        MyAdapter adapter=new MyAdapter(context,list);

//设置执行适配器
        gridView.setAdapter(adapter);

//设置item监听器,消息显示
  gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView adapterView, View view, int i, long l) {
          Toast.makeText(context,"你点击的是第"+(i+1)+"项",Toast.LENGTH_LONG).show();
      }
      public void onNothingSelected(AdapterView parent) {
          // TODO Auto-generated method stub
      }
  });





    }
}

        2.自定义 myAdapter适配器

                

package com.example.myimg;

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

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private Context context;
    private List list;
    private LayoutInflater mInflater;
    MyAdapter(){

    }

    public MyAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater _LayoutInflater=LayoutInflater.from(context);
        view=_LayoutInflater.inflate(R.layout.item, null);
        if(view!=null){
            ImageView image = view.findViewById(R.id.item_image);
            TextView text = view.findViewById(R.id.item_text);
            image.setImageResource(list.get(i).getId());
            text.setText(list.get(i).getName());
        }
         return view;
    }
}

        3.image 实体类

                

package com.example.myimg;

public class image {

    String name;
    int id;

    public image(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/722277.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存