GridView的实现-创新互联

代码实现后界面如图:

创新互联公司是一家专注于成都网站设计、成都网站制作与策划设计,尉犁网站建设哪家好?创新互联公司做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:尉犁等地区。尉犁做网站价格咨询:028-86922220

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类,一样的效果)

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:GridView的实现-创新互联
网页地址:http://pwwzsj.com/article/dhgdcj.html