ViewSwticher代表视图切换组件,可以将多个View层叠在一起,当程序控制从一个View切换到另外一个View时,ViewSwticher支持制定动画效果(增强班GridView)
仿android系统的应用程序界面
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
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;
import com.example.android_lb_day2.R;
public class ViewSwitcherTest extends Activity {
//定义一个常量,用于显示每屏的应用程序总数
public static final int SCREEN=12;
//每个程序的名字和图标
public static class DataItem{
public String name ;
public Drawable drawable ;
}
//保存所有程序的集合
List<DataItem> list = new ArrayList<DataItem>();
//记录当前正在显示第几屏的程序
private int screenNo =-1;
//保存程序所占的总屏数
private int screenCount ;
ViewSwitcher switcher;
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_swticher);
inflater=LayoutInflater.from( this);
getScreenApplication();
/**
* 计算应用程序所占的总屏数,如果能整出SCREEN,除法的结果就是总屏数、否则要加1
* */
screenCount =list .size()%SCREEN==0? list.size()/SCREEN : list.size()/SCREEN+1;
switcher=(ViewSwitcher)findViewById(R.id.swticher);
switcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
//加载布局,就是一个GridView组件
return inflater.inflate(R.layout.swticher_gridview, null);
}
});
//页面加载的时候先显示第一屏
next( null);
}
public void next(View v) {
if(screenNo <screenCount-1){
screenNo++;
//为Switcher显示过程设置动画
switcher.setInAnimation(this,R.anim.slide_in_right);
//为Switcher隐藏过程设置动画
switcher.setOutAnimation(this,R.anim.slide_out_left);
//控制下一屏将要显示的GridView对应的Adapter
((GridView) switcher.getNextView()).setAdapter(adapter );
switcher.showNext();
}
}
public void pre(View v) {
if(screenNo >0){
screenNo--;
//为Switcher显示过程设置动画
switcher.setInAnimation(this,R.anim.slide_out_left);
//为Switcher隐藏过程设置动画
switcher.setOutAnimation(this,R.anim.slide_in_right);
//控制下一屏将要显示的GridView对应的Adapter
((GridView) switcher.getNextView()).setAdapter(adapter );
switcher.showPrevious();
}
}
BaseAdapter adapter = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(convertView==null){
//加载switcher_label布局文件
view= inflater.inflate(R.layout.swticher_label, null);
}
//获取他们的id并为控件赋值
ImageView imageView =(ImageView)view.findViewById(R.id.labelImg );
imageView.setImageDrawable(getItem(position). drawable);
TextView text = (TextView)view.findViewById(R.id.labelText );
text.setText(getItem(position). name);
return view;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public DataItem getItem(int position) {
return list .get(screenNo* SCREEN+position);
}
@Override
public int getCount() {
//如果到了最后一屏
if(screenNo ==screenCount -1&&list.size()% SCREEN!=0){
return list .size()&SCREEN;
}
return SCREEN ;
}
};
//循环给List里面加入DataItem集合
public void getScreenApplication(){
for(int i =0;i<100;i++){
String label= ""+i;
Drawable daDrawable =getResources().getDrawable(R.drawable.ic_launcher);
DataItem item = new DataItem();
item. name=label;
item. drawable=daDrawable;
list.add(item);
}
}
}
附录1:activity_swticher.xml
<span style="font-size:14px;"><?xml version="1.0" encoding= "utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ViewSwitcher
android:id="@+id/swticher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ViewSwitcher >
<Button
android:id="@+id/pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="<"
android:onClick="pre" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text=">"
android:onClick="next" />
</RelativeLayout>
</span>
<?xml version="1.0" encoding= "utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="@+id/labelImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/labelText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center" />
</LinearLayout><span style="color:#008080;">
</span>
<?xml version="1.0" encoding= "utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate android:fromXDelta= "0"
android:toXDelta="-100%p"
android:duration="@android:integer/config_mediumAnimTime" >
</translate >
</set>
<?xml version="1.0" encoding= "utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate android:fromXDelta= "100%p"
android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime" >
</translate >
</set>