Drawer Layout 与 Fragment的组合使用

本文探讨了如何在Android应用中结合使用Drawer Layout和Fragment,通过实例展示了它们在多个APP中的常见应用。详细介绍了布局文件如activity_main.xml、drawer_list.xml、fragment_oil.xml和fragment_item1.xml的结构,并讲解了MainActivity的设置,包括监听和Fragment的管理。同时强调了在Fragment中操作控件的注意事项,如必须的空构造函数、在onActivityCreated中的控件初始化等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在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);
	}
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值