package org.hjw.lanchertest; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.graphics.drawable.Drawable; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import android.widget.ViewSwitcher; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { public static final int NUMBER_PER_SCREEN=16; public static class DataItem{//存放应用的图标和名字 public String dataname; public Drawable drawable; } private ArrayList<DataItem> items=new ArrayList<DataItem>(); private int screenNo=-1;//当前屏 private int screenCount;//屏数 ViewSwitcher switcher; LayoutInflater inflater; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inflater=LayoutInflater.from(MainActivity.this); for(int i=0;i<40;i++){ String label=""+i; Drawable drawable=getResources().getDrawable(R.drawable.ic_launcher); DataItem item=new DataItem(); item.dataname=label; item.drawable=drawable; items.add(item); } screenCount=items.size()%NUMBER_PER_SCREEN==0? items.size()/NUMBER_PER_SCREEN: items.size()/NUMBER_PER_SCREEN+1; switcher=(ViewSwitcher)findViewById(R.id.viewSwitcher); switcher.setFactory(new ViewFactory() { public View makeView() { // TODO Auto-generated method stub return inflater.inflate(R.layout.slidelistview, null); } }); next(null); } public void next(View v){ if(screenNo<screenCount-1){ screenNo++; switcher.setInAnimation(this,R.anim.slide_in_right); switcher.setOutAnimation(this,R.anim.slide_out_left); ((GridView) switcher.getNextView()).setAdapter(adpter); switcher.showNext(); } } public void prev(View v){ if(screenNo>0){ screenNo--; switcher.setInAnimation(this,R.anim.slide_out_left); switcher.setOutAnimation(this,R.anim.slide_in_right); ((GridView) switcher.getNextView()).setAdapter(adpter); switcher.showPrevious(); } } private BaseAdapter adpter=new BaseAdapter() { @Override public View getView(int arg0, View arg1, ViewGroup arg2) { View view=arg1; if(arg1==null){ view=inflater.inflate(R.layout.labelicon, null); } ImageView imageView=(ImageView) view.findViewById(R.id.imageview); imageView.setImageDrawable(getItem(arg0).drawable); TextView textView=(TextView)view.findViewById(R.id.textview); textView.setText(getItem(arg0).dataname); return view; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public DataItem getItem(int arg0) { // TODO Auto-generated method stub return items.get((screenNo)*NUMBER_PER_SCREEN+arg0); } @Override public int getCount() { if(screenNo==screenCount-1&&items.size()%NUMBER_PER_SCREEN!=0){ return items.size()%NUMBER_PER_SCREEN; } return NUMBER_PER_SCREEN; } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
ps:1.最关键的代码无非是setFactory那一段,加载GridView组件
2.BaseAdapter的使用,负责为每屏为显示GridView提供列表项
3.动画资源文件的使用,下面是往右拖的动画资源文件
<set xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 设置从右边拖进来的动画 android:duration指定动画持续时间 --> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime" /> </set>