码迷,mamicode.com
首页 > 其他好文 > 详细

GridView

时间:2016-05-21 11:43:58      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

package com.hanqi.testapp2;

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 java.util.ArrayList;
import java.util.List;

public class TestActivity9 extends AppCompatActivity {
    GridView gv_1;
    List<Integer> liv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test9);
        GridView gv_1=(GridView)findViewById(R.id.gv_1);
        //1数据
        liv=new ArrayList<>();
        liv.add(R.drawable.p7);
        liv.add(R.drawable.p7);
        liv.add(R.drawable.p7);
        liv.add(R.drawable.p7);
        liv.add(R.drawable.p7);
        liv.add(R.drawable.p7);
        liv.add(R.drawable.p7);
        liv.add(R.drawable.p7);
        gv_1.setAdapter(new ImagerAdapter());



    }

    //适配器
    class ImagerAdapter 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) {
           //得到数据
           Integer ivid=liv.get(position);
            //准备视图view
            if(convertView==null) {
                convertView = new ImageView(TestActivity9.this);
            }
            //适配
            //转换
            ImageView imageView=(ImageView) convertView;
            imageView.setImageResource(ivid);
//            imageView.setMaxWidth(70);
//            imageView.setMaxHeight(70);
            imageView.setLayoutParams(new GridView.LayoutParams(100,100));
            return imageView;
        }
    }

}

 

GridView

标签:

原文地址:http://www.cnblogs.com/lizhe313/p/5514216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!