最近工作需要做一个项目的demo来演示,所以就试试手做了一个简单的demo供演示使用,主要是利用fragment的简单使用以及百度地图定位、添加覆盖物等知识点,所以特此记录。
先来一发效果图吧。
从效果图来看,我们需要实现的Tab之间的切换,然后在Fragment中嵌套MapView控件进行使用,点进去有详情路线,百度地图模块主要有定位功能和添加覆盖物功能。下面我们分别看看三个Fragment页面的实现。
通过效果图中,我们可以看到Fragment中含有一个ListView控件,所以接下来我们就要处理ListView的内容填充。
(1)、我们首先看看布局文件:
<?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:background="#F1F0F3" android:orientation="vertical" >
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:padding="6dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="名称" android:gravity="center_horizontal" android:layout_weight="1" android:textSize="16sp" android:textColor="#000000"/>
<View android:layout_width="2dp" android:layout_marginLeft="10dp" android:layout_height="match_parent" android:background="@color/light_black" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="地址" android:gravity="center_horizontal" android:layout_weight="10" android:textSize="16sp" android:textColor="#000000"/>
<View android:layout_width="2dp" android:layout_marginLeft="10dp" android:layout_height="match_parent" android:background="@color/light_black" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="投递情况" android:gravity="center_horizontal" android:layout_weight="1" android:textSize="16sp" android:textColor="#000000"/>
</LinearLayout>
<ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:scrollbars="@null" android:listSelector="#00000000" />
</LinearLayout>
为了填充ListView中的数据,我们新建实体类PackageEntity用于存储信息。
public class PackageEntity implements Serializable {
private static final long serialVersionUID = 1l;
private String sender;
private String receiver;
private String address;
private int distance;
private String date;
private int state;//1:投�?�?2:未妥投 3:已签收
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
处理很简单,所以我们直接看看这个Fragment的内部处理。
public class GoodsFragement extends Fragment {
private ListView listView;
private GoodsAdapter adapter;
private ArrayList<PackageEntity> list;
private WayDetailItemFragment wayItemFragemnt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initData();
wayItemFragemnt = new WayDetailItemFragment();
adapter = new GoodsAdapter(getActivity(),list);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fragment_goods, container,false);
listView = (ListView) view.findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
FragmentManager manager = getFragmentManager();
FragmentTransaction translation = manager.beginTransaction();
translation.replace(R.id.fragment_container, wayItemFragemnt);
translation.commit();
((HomeActivity) getActivity()).setWayButton();
}
});
return view;
}
private void initData(){
list = new ArrayList<PackageEntity>();
PackageEntity entity = new PackageEntity();
entity.setAddress("济南市历下区经十路中信银");
entity.setDate("2015-9-22");
entity.setDistance(2);
entity.setReceiver("张先");
entity.setSender("李二");
entity.setState(2);
list.add(entity);
entity = new PackageEntity();
entity.setAddress("济南市历下区经十路华电国");
entity.setDate("2015-9-22");
entity.setDistance(3);
entity.setReceiver("王先");
entity.setSender("顾女");
entity.setState(1);
list.add(entity);
entity = new PackageEntity();
entity.setAddress("济南市历下区经十路天津银");
entity.setDate("2015-9-22");
entity.setDistance(2);
entity.setReceiver("薛先");
entity.setSender("季先");
entity.setState(1);
list.add(entity);
entity = new PackageEntity();
entity.setAddress("济南市历下区经十路嘉和园小区");
entity.setDate("2015-9-22");
entity.setDistance(2);
entity.setReceiver("陈女");
entity.setSender("郑先");
entity.setState(3);
list.add(entity);
entity = new PackageEntity();
entity.setAddress("济南市历下区经十路东子锋");
entity.setDate("2015-9-22");
entity.setDistance(1);
entity.setReceiver("田先");
entity.setSender("潘女");
entity.setState(3);
list.add(entity);
entity = new PackageEntity();
entity.setAddress("济南市历下燕文东路舜泽钢琴城");
entity.setDate("2015-9-22");
entity.setDistance(1);
entity.setReceiver("高先");
entity.setSender("杜女");
entity.setState(3);
list.add(entity);
}
}
至此,我们的第一个列表Fragment就完成了。也算是Fragment的简单用法吧!接下来我们说第二个Fragment。
通过效果图,可以看到这里面包含一个MapView,然后我们在里面标注几个点。也算是百度地图的简单用法吧!这里面就是百度地图的几个简单用法,我就针对用法简单记录下,具体的逻辑有兴趣的就稍后下载实例代码看吧!
(1)、百度地图基本使用
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="开发者 key" />
(2)、百度地图定位功能:在Fragment中我们需要管理MapView的生命周期。百度地图定位使用者几个类进行:LocationClient、LocationClientOption进行,然后通过BDLocationListener广播收听。
public class WayFragement extends Fragment {
private MapView mapView;
private LocationClient locationClient;
private MyLocationListener locationListener = null;
private BaiduMap mBaiduMap;
private List<LongLatitue> list;
private WayDetailItemFragment wayItemFragemnt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().getWindow().setFormat(PixelFormat.TRANSLUCENT);
View view = inflater.inflate(R.layout.activity_fragment_way, container,false);
mapView = (MapView) view.findViewById(R.id.mapView);
mBaiduMap = mapView.getMap();
mapView.showScaleControl(true);
mapView.showZoomControls(true);
locationListener = new MyLocationListener();
locationClient = new LocationClient(getActivity().getApplicationContext());
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll");
option.setLocationMode(LocationMode.Hight_Accuracy);
option.setScanSpan(5000);
option.setIsNeedAddress(true);
option.setOpenGps(true);
option.setNeedDeviceDirect(true);
locationClient.setLocOption(option);
locationClient.registerLocationListener(locationListener);
locationClient.start();
list = new ArrayList<WayFragement.LongLatitue>();
wayItemFragemnt = new WayDetailItemFragment();
setOnMapListner();
return view;
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
initData();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
locationClient.unRegisterLocationListener(locationListener);
locationClient.stop();
mapView.onDestroy();
}
/** * 定位广播接受者,接受定位信息 * @author dsw * */
class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
double latitude = 36.660592;//location.getLatitude();
double longitude = 117.06238;//location.getLongitude();
MyLocationData locationData = new MyLocationData.Builder()
.accuracy(location.getRadius()).latitude(latitude).longitude(longitude).build();
mBaiduMap.setMyLocationData(locationData);
LatLng pt = new LatLng(latitude, longitude);
MapStatus mapStatus = new MapStatus.Builder()
.target(pt).zoom(16).build();
MapStatusUpdate statusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
mBaiduMap.setMapStatus(statusUpdate);
mapView.refreshDrawableState();
locationClient.unRegisterLocationListener(locationListener);
locationClient.stop();
}
};
}
(3)如何将某点移动到视图中。通过MyLocationData、MapStatus、MapStatusUpdate进行,若下代码:
double latitude = 36.660592;//location.getLatitude();
double longitude = 117.06238;//location.getLongitude();
MyLocationData locationData = new MyLocationData.Builder()
.accuracy(location.getRadius()).latitude(latitude).longitude(longitude).build();
mBaiduMap.setMyLocationData(locationData);
LatLng pt = new LatLng(latitude, longitude);
MapStatus mapStatus = new MapStatus.Builder()
.target(pt).zoom(16).build();
MapStatusUpdate statusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
mBaiduMap.setMapStatus(statusUpdate);
mapView.refreshDrawableState();
locationClient.unRegisterLocationListener(locationListener);
locationClient.stop();
(4)在百度地图上添加点覆盖物。
LatLng point = new LatLng(longlati.getLatitude(), longlati.getLongitude());
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(id);
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions()
.position(point)
.icon(bitmap);
//在地图上添加Marker,并显示
mBaiduMap.addOverlay(option);
//进行刷新显示
mapView.refreshDrawableState();
(5)添加线覆盖物,通过PolylineOptions类。实例如下:
private void initData() {
LatLng pointStart = new LatLng(36.653743, 117.058896);
LatLng pointMiddOne = new LatLng(36.654223,117.062326);
LatLng pointMiddle = new LatLng(36.660592,117.06238);
//构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(R.drawable.state_mdd);
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions()
.position(pointMiddle)
.icon(bitmap);
//在地图上添加Marker,并显示
mBaiduMap.addOverlay(option);
//定义Maker坐标�?
LatLng pointEnd = new LatLng(36.66076, 117.068112);
//构建Marker图标
bitmap = BitmapDescriptorFactory
.fromResource(R.drawable.map_zd);
//构建MarkerOption,用于在地图上添加Marker
option = new MarkerOptions()
.position(pointEnd)
.icon(bitmap);
//在地图上添加Marker,并显示
mBaiduMap.addOverlay(option);
List<LatLng> points = new ArrayList<LatLng>();
List<Integer> index = new ArrayList<Integer>();
points.add(pointStart);
points.add(pointMiddOne);
points.add(pointMiddle);
//构�?对象
OverlayOptions ooPolyline = new PolylineOptions().width(10).color(0xFF0000FF).
points(points).textureIndex(index);
mBaiduMap.addOverlay(ooPolyline);
//
List<BitmapDescriptor> customList = new ArrayList<BitmapDescriptor>();
BitmapDescriptor custom1 = BitmapDescriptorFactory
.fromResource(R.color.red);
BitmapDescriptor custom2 = BitmapDescriptorFactory
.fromResource(R.color.red);
customList.add(custom1);
customList.add(custom2);
//添加到地�?
points = new ArrayList<LatLng>();
points.add(pointMiddle);
points.add(pointEnd);
ooPolyline = new PolylineOptions().width(10).color(0xAAFF0000).
points(points).customTextureList(customList).textureIndex(index);
mBaiduMap.addOverlay(ooPolyline);
mMapView.refreshDrawableState();
}
(6)添加覆盖物的监听。
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
FragmentManager manager = getFragmentManager();
FragmentTransaction translation = manager.beginTransaction();
translation.replace(R.id.fragment_container, wayItemFragemnt);
translation.commit();
((HomeActivity) getActivity()).setWayButton();
return false;
}
});
至此,所用到的百度地图功能就已经实现,现在主要就是MainActivity中的Fragment的基本使用。
btn_goodsList.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
btn_goodsList.setBackgroundColor(getResources().getColor(R.color.home_focus));
btn_onway.setBackgroundColor(getResources().getColor(R.color.home_nomal));
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTrans = manager.beginTransaction();
fragmentTrans.replace(R.id.fragment_container, goodsFragment);
fragmentTrans.commit();
}
});
btn_onway.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
btn_onway.setBackgroundColor(getResources().getColor(R.color.home_focus));
btn_goodsList.setBackgroundColor(getResources().getColor(R.color.home_nomal));
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTrans = manager.beginTransaction();
fragmentTrans.replace(R.id.fragment_container, wayFragment);
fragmentTrans.commit();
}
});
通过点击进行切换不同的Fragment进行显示。至此,所有基本功能都已实现,还有一个bug就是在fragment中嵌套MapView进行切换的时候,回闪黑屏,不知道大家有什么好的意见,欢迎留言交流。
源码地址
作者:mr_dsw 欢迎转载,与人分享是进步的源泉!
转载请保留地址:http://blog.csdn.net/mr_dsw