现在Drawer Layout 与 Fragment在很多APP中都有出现,所以写篇博客,自己总结下。
先上两张Weico和豆瓣FM的效果图。
下面是测试的DemoApp
首先是布局。Demo用到了四个xml文件:activity_main.xml、drawer_list.xml、fragment_oil.xml、fragment_item1.xml
activity_main.xml为整体的布局,里面包含两块:<FrameLayout>用于显示主题体内容,<ListView>用于显示侧栏
fragment_oil为选择油量查询弹出的Fragment,fragment_item1为选择item1弹出额Fragment
接下来是MainActivity
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
设置list选项、监听
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item, mCarpTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
上面用到了自定义的类DraweItemClickListener,下面定义这个类
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
}
private void selectItem(int position) {
String carpString = null;
switch (position) {
case 0:
// update the main content by replacing fragments
Fragment oilFragment = new OilFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, oilFragment).commit();
break;
case 1:
// update the main content by replacing fragments
Fragment item1Fragment = new Item1Fragment();
FragmentManager fragmentManager1 = getFragmentManager();
fragmentManager1.beginTransaction()
.replace(R.id.content_frame, item1Fragment).commit();
break;
default:
break;
}
//close the drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
OilFragment和Item1Fragment是定义的类,继承自Fragment。
注意点:
1.空的构造函数是必须的;
2.对于Fragment内的控件的操作,放在onActivityCreated中;
3.或许控件需要使用getview()
4.填充布局,也就是fragment_oil.xml、fragment_item1.xml
public static class OilFragment extends Fragment {
public OilFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Button mConsumeOilButton = (Button) getView().findViewById(
R.id.button1);
mProgress = (ProgressBar) getView().findViewById(R.id.progressBar1);
showOilTextView = (TextView) getView().findViewById(R.id.textView1);
mConsumeOilButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new UpdateProgressBar().execute();
// 启动线程,检测油量,当低于20%的时候,发送notification
Intent intent = new Intent(getActivity(),
OilDetectService.class);
getActivity().startService(intent);
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_oil, container,
false);
return rootView;
}
}
上面用到了UpdateProcessBar extends AsyncTask,用于消耗油量。另外开启了一个Service,当油量少于20%的时候,发出Notification
下面是Service,继承自IntentService。构造函数和onHandleIntent是必须的,onHandleIntent的形参intent是startService传递过来的(这里并没用到)
public class OilDetectService extends IntentService {
public OilDetectService() {
super("OilDetectService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
while (20<MainActivity.mProgressStatus) {
}
NotificationManager notificationManager = (NotificationManager) super.getSystemService(Activity.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle("警告")
.setContentText("油量低于20%,请及时加油")
.setSmallIcon(R.drawable.ic_launcher)
.build();
//notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(null,1,notification);
}
}