GridView的实现

发布时间:2020-06-12 00:47:08 作者:JustMetU
来源:网络 阅读:582

代码实现后界面如图:

GridView的实现


一。xml文件

1.<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/relativelayout">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="当前时间:"
        android:textSize="16dp" />

<GridView
        android:id="@+id/gv1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/text1"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="spacingWidthUniform"
        android:verticalSpacing="5dp" >

</GridView>

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/text1"
        android:text="----------"
        android:textSize="16dp" />

</RelativeLayout>


(注text是当前时间显示,不用管,直接一个GridView即可)


2.每个图标的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/p_w_picpathview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:scaleType="fitXY"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center_horizontal"/>

</LinearLayout>


二。界面很简单,下面是MainAcyivity代码:

1.public class MainActivity extends Activity {

    GridView gridview;
    PictureAdapter adapter;
    public static int colorcolor=Color.WHITE;
    int[] p_w_picpaths=new int[]{R.drawable.newdate,R.drawable.beiwang,R.drawable.newout,R.drawable.newin,R.drawable.alldate,R.drawable.allbeiwang,R.drawable.allout,R.drawable.allin,R.drawable.personmanager,R.drawable.game,R.drawable.chat,R.drawable.relax,R.drawable.seeitlogo,R.drawable.aboutflag,R.drawable.settings,R.drawable.out};
    String[] titles=new String[]{"写日记","写备忘","新增支出","新增收入","我的日记","我的备忘","总支出","总收入","个人管理","游戏娱乐","聊天室","最简浏览器","待开发。。。","关于我","系统设置","退出"};
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridview=(GridView)findViewById(R.id.gv1);
        adapter=new PictureAdapter(titles,p_w_picpaths,this);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Intent intent=null;
                switch(arg2){
                case 0:intent=new Intent(MainActivity.this,newDate.class);
                       startActivity(intent);
                       finish();
                       break;
                case 1:intent=new Intent(MainActivity.this,newMark.class);
                       startActivity(intent);
                       finish();
                       break;
                case 2:intent=new Intent(MainActivity.this,newOut.class);
                       startActivity(intent);
                       finish();
                       break;
                case 3:intent=new Intent(MainActivity.this,newIn.class);
                       startActivity(intent);
                       finish();
                       break;
                case 4:intent=new Intent(MainActivity.this,allDate.class);
                       startActivity(intent);
                       finish();
                       break;
                case 5:intent=new Intent(MainActivity.this,allMark.class);
                       startActivity(intent);
                       finish();break;
                case 6:intent=new Intent(MainActivity.this,allOut.class);
                       startActivity(intent);
                       finish();
                       break;
                case 7:intent=new Intent(MainActivity.this,allIn.class);
                       startActivity(intent);
                       finish();
                       break;
                case 8:intent=new Intent(MainActivity.this,PersonManager.class);
                       startActivity(intent);
                       finish();
                       break;
                case 9:intent=new Intent(MainActivity.this,Game.class);
                       startActivity(intent);
                       finish();
                       break;
                case 10:intent=new Intent(MainActivity.this,Chat.class);
                       startActivity(intent);
                       finish();
                       break;
                case 11:intent=new Intent(MainActivity.this,WebGo.class);
                        startActivity(intent);
                        finish();
                        break;
                case 12:intent=new Intent(MainActivity.this,TabsFrame.class);
                        startActivity(intent);
                        finish();
                        break;    
                case 13:intent=new Intent(MainActivity.this,Aboutauthor.class);
                        startActivity(intent);
                        finish();
                        break;
                case 14:MainActivity.this.openOptionsMenu();
                        break;
                case 15:showdialog(MainActivity.this);
                        break;
                    
                }
                
            }
        });

}



2.PictureAdapter类来自BaseAdapter自己写:

package com.example.licaizhuanjia;

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

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

public class PictureAdapter extends BaseAdapter {

    private List<Picture> pictures;
    private LayoutInflater inflater;
    public static ViewHolder vh;
    public PictureAdapter(String[] titles,int[] p_w_picpaths,Context context){
        super();
        pictures=new ArrayList<Picture>();
        inflater=LayoutInflater.from(context);
        for(int i=0;i<p_w_picpaths.length;i++){
            Picture picture=new Picture(titles[i],p_w_picpaths[i]);
            pictures.add(picture);
        }
        
    }
    public int getCount() {
        // TODO Auto-generated method stub
        if(null!=pictures){
            return pictures.size();
        }
        else
            return 0;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return pictures.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        if(arg1==null){
            arg1=inflater.inflate(R.layout.gv1p_w_picpath, null);
            vh=new ViewHolder();
            vh.title=(TextView)arg1.findViewById(R.id.textview);
            vh.p_w_picpath=(ImageView)arg1.findViewById(R.id.p_w_picpathview);
            arg1.setTag(vh);
        }
            else
                vh=(ViewHolder)arg1.getTag();
        vh.title.setText(pictures.get(arg0).getTitle());
        settextcolor(MainActivity.colorcolor);
        vh.p_w_picpath.setImageResource(pictures.get(arg0).getImage());
        return arg1;
    }

    public static void settextcolor(int color){
        vh.title.setTextColor(color);
    }
}

3.Picture类

package com.example.licaizhuanjia;

public class Picture {
    private String title;
    private int p_w_picpath;
    public Picture(){
        super();
    }
    public Picture(String title,int p_w_picpath){
        super();
        this.title=title;
        this.p_w_picpath=p_w_picpath;
    }
    public void setImage(int p_w_picpath){
        this.p_w_picpath=p_w_picpath;
    }
    public void setTitle(String title){
        this.title=title;
    }
    public int getImage(){
        return p_w_picpath;
    }
    public String getTitle(){
        return title;
    }
}

4.ViewHolder类

package com.example.licaizhuanjia;

import android.widget.ImageView;
import android.widget.TextView;

public class ViewHolder {
    public TextView title;
    public ImageView p_w_picpath;

}


(ps:可以吧picture类和ViewHolder类写进PictureAdapter类,一样的效果)

推荐阅读:
  1. 如何进行Gridview的实现
  2. GridView分隔线的实现方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

gridview baseadapter pictureadapter

上一篇:TCP/IP协议及配置、配置工作组网络、配置互联网接入

下一篇:一文了解“Service Mesh(服务网格)”的历史与现在

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》