网站建设资讯

NEWS

网站建设资讯

GridView的实现

代码实现后界面如图:

成都创新互联服务项目包括林芝网站建设、林芝网站制作、林芝网页制作以及林芝网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,林芝网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到林芝省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

GridView的实现

一。xml文件

1.    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">

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

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



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

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

2.每个图标的xml


    android:id="@+id/item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

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

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


二。界面很简单,下面是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 pictures;
    private LayoutInflater inflater;
    public static ViewHolder vh;
    public PictureAdapter(String[] titles,int[] p_w_picpaths,Context context){
        super();
        pictures=new ArrayList();
        inflater=LayoutInflater.from(context);
        for(int i=0;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类,一样的效果)


文章名称:GridView的实现
网页URL:http://njwzjz.com/article/gpdpdi.html