Android UI开发第二十八篇——Fragment中使用左右滑动菜单
Fragment实现了Android UI的分片管理,尤其在平板开发中,好处多多。这一篇将借助Android UI开发第二十六篇——Fragment间的通信、 Android UI开发第二十七篇——实现左右划出菜单 来实现。看图1的效果,左边使用LeftFragment实现,中间使用的CenterFragment实现。
public class SlidingActivity extends FragmentActivity implements LeftShowCenter{ private SlidingMenu mSlidingMenu; private CenterFragment centerFragment; @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); setContentView(R.layout.main); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); mSlidingMenu = (SlidingMenu) findViewById(R.id.slidingMenu); mSlidingMenu.setLeftWidth((dm.widthPixels / 5) * 2); mSlidingMenu.setRightWidth((dm.widthPixels / 5) * 3); View leftView = getLayoutInflater().inflate(R.layout.left_frame, null); View rightView = getLayoutInflater().inflate(R.layout.right_frame, null); View centerView = getLayoutInflater().inflate(R.layout.center_frame, null); mSlidingMenu.setLeftView(leftView); mSlidingMenu.setRightView(rightView); mSlidingMenu.setCenterView(centerView); FragmentTransaction t = this.getSupportFragmentManager() .beginTransaction(); LeftFragment leftFragment = new LeftFragment(); t.replace(R.id.left_frame, leftFragment); RightFragment rightFragment = new RightFragment(); t.replace(R.id.right_frame, rightFragment); centerFragment = new CenterFragment(); t.replace(R.id.center_frame, centerFragment); t.commit(); } public void showLeft() { mSlidingMenu.showLeftView(); } public void showRight() { mSlidingMenu.showRightView(); } public void showCenter() { mSlidingMenu.showCenterView(); } @Override public void leftShowCenter(int id) { // TODO Auto-generated method stub if(centerFragment!=null){ centerFragment.updateView(id); } } }
SlidingActivity继承自FragmentActivity,实现了LeftShowCenter的接口方法,LeftShowCenter接口的主要目的是实现Fragment间的通信。
图1
/**
* @author 张兴业