GridView(网格视图)应用场景代码

作者: android_Pie | 来源:发表于2020-03-10 15:27 被阅读0次
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements OnItemClickListener{
    
        private int imgs[]={
        R.drawable.one,R.drawable.two,      
        R.drawable.three,R.drawable.four,       
        R.drawable.five,R.drawable.six,     
        R.drawable.seven,R.drawable.eight,      
        };
        private String[] names={"A","B","C","D","E","F","G","H"};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //1.获得GridView
            GridView gv=(GridView)findViewById(R.id.gv01);
            //2.创建适配器对象
            GAdapter adapter=new GAdapter();
            //3.关联适配器对象
            gv.setAdapter(adapter);
            //4.添加点击事件
            gv.setOnItemClickListener(this);
        }
    
        class GAdapter extends BaseAdapter{
    
            @Override
            public int getCount() {
                return imgs.length;
            }
    
            /**返回具体某个item中的数据*/
            @Override
            public Object getItem(int position) {
                return imgs[position];
            }
    
            /**返回item的位置*/
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            /**借助此方法构建item对象*/
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                //1.item view (列表项视图)
                RelativeLayout layout=(RelativeLayout) View.inflate(MainActivity.this,
                R.layout.grid_item_2, null);
                //2.item data (列表项数据)
                int imgId=imgs[position];
                String name=names[position];
                //3.将item data放到itemView上
                //3.1获得放图片和文本具体位置
                ImageView iv=(ImageView)layout.findViewById(R.id.img01);
                TextView tv=(TextView) layout.findViewById(R.id.tv01);
                iv.setImageResource(imgId);
                tv.setText(name);
                return layout;
            }
            
        }
    
        /**点击GridView中的某个item时会自动执行此方法*/
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
            //获得图片id
            int imgId=(Integer)parent.getItemAtPosition(position);
        }
    }
    
    <!--activity_main.xml-->
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/gv01"
        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"
        android:numColumns="2"
        android:horizontalSpacing="1dp"
        android:verticalSpacing="1dp"
        tools:context=".MainActivity" >
    </GridView>
    
    //适配器item view布局grid_item_2.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff669900" >
        <ImageView 
            android:id="@+id/img01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/one"
            />
        <TextView
            android:id="@+id/tv01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/img01"
            android:layout_centerHorizontal="true"
            android:text="2020"
            android:textColor="#FFFFFFFF" />
    </RelativeLayout>
    
    

    相关文章

      网友评论

        本文标题:GridView(网格视图)应用场景代码

        本文链接:https://www.haomeiwen.com/subject/uqiwdhtx.html