2月7日学习内容

今天观看视频学习了grideview网格视图

刚开始的时候自己新建一个包,然后在包里面新建一个空的Activity

package com.example.yangy.myapplication123.grideview;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.example.yangy.myapplication123.R;

public class GrideViewActivity extends ActionBarActivity {

    private GridView mgv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gride_view);
        mgv= (GridView) findViewById(R.id.gv_1);
        mgv.setAdapter(new Myadapter(GrideViewActivity.this) );
        mgv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                Toast.makeText(GrideViewActivity.this, "点击 pos", Toast.LENGTH_SHORT).show();
            }
        });
        mgv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                Toast.makeText(GrideViewActivity.this, "长按 pos", Toast.LENGTH_LONG).show();
            }
        });
    }
}
GrideViewActivity

然后再他里面的.xml里面写网格视图GrideView

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">
    <GridView
        android:id="@+id/gv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="2"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"/>
RelativeLayout>
grideview.xml

其中

android:numColumns="2" 有2列

android:horizontalSpacing="10dp"列与列之间行间距

android:verticalSpacing="10dp"垂直方向的间距

 

 需要自己新建一个Adapter继承BaseAdapter

package com.example.yangy.myapplication123.grideview;

import android.content.Context;
import android.media.Image;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
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 com.example.yangy.myapplication123.R;

public class Myadapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    public Myadapter(Context context)
    {
        this.mContext=context;
        mLayoutInflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        //数量是多少
        return 5;
    }

    @Override
    public Object getItem(int i) {
    return  null;
    }

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

    static class ViewHolder{
        public ImageView imageview;
        public TextView textView;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view ==null){
            view=mLayoutInflater.inflate(R.layout.activity_myadapter,null);
            holder =new ViewHolder();
            holder.imageview= (ImageView) view.findViewById(R.id.iv_tu);
            holder.textView= (TextView) view.findViewById(R.id.tv_title);
            view.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)view.getTag();
        }
        //赋值
        holder.textView.setText("话");
    return view;
    }
}
MyAdapter

然后在layout中新建一个.xml

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:padding="20dp"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/iv_tu"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#000"/>
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈哈哈"
        android:layout_marginTop="10dp"
        android:textSize="20dp"
        android:textColor="#FF9900"
        android:layout_gravity="center"/>
LinearLayout>
myadapter.xml

 

 

 

你可能感兴趣的:(2月7日学习内容)