标签:gridview pictureadapter baseadapter
代码实现后界面如图:
一。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/imageview"
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[] images=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,images,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[] images,Context context){
super();
pictures=new ArrayList<Picture>();
inflater=LayoutInflater.from(context);
for(int i=0;i<images.length;i++){
Picture picture=new Picture(titles[i],images[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.gv1image, null);
vh=new ViewHolder();
vh.title=(TextView)arg1.findViewById(R.id.textview);
vh.image=(ImageView)arg1.findViewById(R.id.imageview);
arg1.setTag(vh);
}
else
vh=(ViewHolder)arg1.getTag();
vh.title.setText(pictures.get(arg0).getTitle());
settextcolor(MainActivity.colorcolor);
vh.image.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 image;
public Picture(){
super();
}
public Picture(String title,int image){
super();
this.title=title;
this.image=image;
}
public void setImage(int image){
this.image=image;
}
public void setTitle(String title){
this.title=title;
}
public int getImage(){
return image;
}
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 image;
}
(ps:可以吧picture类和ViewHolder类写进PictureAdapter类,一样的效果)
本文出自 “软件学习总结” 博客,请务必保留此出处http://bigcrab.blog.51cto.com/10626858/1691070
标签:gridview pictureadapter baseadapter
原文地址:http://bigcrab.blog.51cto.com/10626858/1691070