GridView网格视图的使用

GridView组件用来以网格方式排列视图,与矩阵类似,当屏幕上有很多元素时,可以使用该组件.

android:numColumns="auto_fit"    设置列数  该属性为自动适合屏宽  设置数字为好
android:verticalSpacing="10dp"      行之间垂直间隔
android:horizontalSpacing="10dp"   列之间的水平间隔
android:columnWidth="90dp"     每列的宽度
android:stretchMode="columnWidth" 拉升模式,  属性值如下:

          ColumnWidth 根据列宽伸展,与gravity配合使用,可使view居中[建议使用]

          spacingWidth 根据空白伸展,与gravity配合使用,可使view左对齐

          spacingWidthUniform 均匀空白伸展,与gravity配合使用,可使view右对齐

android:gravity      每一个单元在表格中的对齐方式

如下面这种方式就还不错:

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.ts.mylrucache.MainActivity">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="120dp"
        android:stretchMode="columnWidth"
        android:numColumns="auto_fit"      //规定了列的宽度,,自动定列数,剩下的空间平均分配做列间距
        android:verticalSpacing="10dp"
        android:gravity="center"
        android:id="@+id/gridView" />
</RelativeLayout>

 

<?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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:src="@mipmap/ic_launcher"
        android:layout_centerInParent="true"
        />
</LinearLayout>

 

 

功能:长按可以删除

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private GridView gridView;
    private ArrayList<Integer> list =new ArrayList<Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        Integer[] imgId=new Integer[]{R.drawable.f000,R.drawable.f001,R.drawable.f002,R.drawable.f003,R.drawable.f004,
                R.drawable.f005,R.drawable.f006,R.drawable.f007,R.drawable.f008,R.drawable.ic_launcher};
        for (Integer integer : imgId) {
            list.add(integer);
        }
       
        gridView=(GridView) findViewById(R.id.gridView);
        final MyAdapter myAdapter = new MyAdapter();
        gridView.setAdapter(myAdapter);
        
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(MainActivity.this, "单击", 0).show();
                myAdapter.setEdit(false);//改变标签,且刷新
            }
        });
        //长按单击事件
        gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(MainActivity.this, "长按", 0).show();
                myAdapter.setEdit(true);
                return true;
            }
            
        });
    }
    class MyAdapter extends BaseAdapter{
        /**
         * 是否是编辑模式
         */
        private boolean isEdit=false;
        
        
        public boolean isEdit() {
            return isEdit;
        }

        public void setEdit(boolean isEdit) {
            this.isEdit = isEdit;
            notifyDataSetChanged();//更新视图
        }

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

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

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView==null){
                convertView=View.inflate(MainActivity.this, R.layout.item_layout, null);
                holder=new ViewHolder();
                holder.image=(ImageView) convertView.findViewById(R.id.imageView);
                holder.delete=(ImageView) convertView.findViewById(R.id.delete);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            holder.image.setImageResource(list.get(position));
            //为编辑模式就显示删除图片,否则不显示
            holder.delete.setVisibility(isEdit?View.VISIBLE:View.GONE);
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.remove(position);
                    notifyDataSetChanged();
                }
            });
            return convertView;
        }
        class ViewHolder{
            private ImageView image;
            private ImageView delete;
        }
    }
}

 

布局文件activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >
   
    <GridView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        android:gravity="center"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        android:id="@+id/gridView"/>

</RelativeLayout>

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher" />
     <ImageView
        android:id="@+id/delete"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/delete" />

</RelativeLayout>

效果图:

    

 

posted @ 2016-07-29 15:49  ts-android  阅读(238)  评论(0编辑  收藏  举报