public class Main extends ActivityGroup implements OnTouchListener,OnGestureListener {
private LocalActivityManager localActivityManager = null;
private LinearLayout mainTabContainer = null;
private Bundle bundle= null;
private int tag=1;
private GestureDetector mGestureDetector=null;
private RadioButton zwss,glzx,hyzp,zczx,more;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(1);
setContentView(R.layout.main);
mGestureDetector = new GestureDetector((OnGestureListener)this);
localActivityManager = getLocalActivityManager();
mainTabContainer = (LinearLayout)findViewById(R.id.tab_main);
mainTabContainer.setOnTouchListener(this);
mainTabContainer.setLongClickable(true);
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rg_tab_foot);
zczx = (RadioButton)radioGroup.findViewById(R.id.zczx);
hyzp = (RadioButton)radioGroup.findViewById(R.id.hyzp);
glzx = (RadioButton)radioGroup.findViewById(R.id.glzx);
zwss = (RadioButton)radioGroup.findViewById(R.id.zwss);
more = (RadioButton)radioGroup.findViewById(R.id.more);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.zwss:
setContainerView(String.valueOf(tag),JobSearch.class);
break;
case R.id.glzx:
if (UserHelper.getUsername() == null || UserHelper.getUsername()=="") {
setContainerView(String.valueOf(tag),Login.class);
} else {
setContainerView(String.valueOf(tag),PersonCenter.class);
}
break;
case R.id.hyzp:
setContainerView(String.valueOf(tag),TradeCategory.class);
break;
case R.id.zczx:
setContainerView(String.valueOf(tag),NewsCategory.class);
break;
case R.id.more:
setContainerView(String.valueOf(tag),About.class);
break;
}
}
});
bundle = getIntent().getExtras();
if (bundle !=null) {
if (bundle.get("to")!=null) {
if (bundle.get("to").equals("NewsCategory")) {
zczx.setChecked(true);
}
else if(bundle.get("to").equals("TradesCategory")) {
hyzp.setChecked(true);
}
}
else {
glzx.setChecked(true);
}
}
else {
zwss.setChecked(true);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
UserHelper.ExitDialog(Main.this);
}
return super.onKeyDown(keyCode, event);
}
//切换activity
public void setContainerView(String id,Class<?> activity){
mainTabContainer.removeAllViews();
Intent mainTabIntent = new Intent(this,activity);
if (bundle != null && bundle.get("from") !=null) {
if (bundle.get("from").equals("JobDetail")) {
mainTabIntent.putExtra("from", bundle.get("from").toString());
mainTabIntent.putExtra("sn", bundle.get("sn").toString());
}
}
mainTabContainer.addView(localActivityManager.startActivity(id, mainTabIntent).getDecorView());
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > SnsConstant.getFlingMinDistance()
&& Math.abs(velocityX) > SnsConstant.getFlingMinVelocity()) {
tag = Integer.parseInt(localActivityManager.getCurrentId());
if (tag == 5) {
tag = 1;
} else {
tag++;
}
go();
} else if (e2.getX() - e1.getX() > SnsConstant.getFlingMinDistance()
&& Math.abs(velocityX) > SnsConstant.getFlingMinVelocity()) {
tag = Integer.parseInt(localActivityManager.getCurrentId());
if (tag ==1) {
tag=5;
}
else {
tag--;
}
go();
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
private void go(){
switch (tag) {
case 1:
zwss.setChecked(true);
break;
case 2:
glzx.setChecked(true);
break;
case 3:
hyzp.setChecked(true);
break;
case 4:
zczx.setChecked(true);
break;
case 5:
more.setChecked(true);
break;
}
}
public static class SnsConstant {
private static final int FLING_MIN_DISTANCE = 20;
private static final int FLING_MIN_VELOCITY = 0;
public static int getFlingMinDistance() {
return FLING_MIN_DISTANCE;
}
public static int getFlingMinVelocity() {
return FLING_MIN_VELOCITY;
}
}
}
不足:碰到上下拉动的listview左右切换会失效!