m层
public class ModelImpl implements MainIcontrcat.IModel{
private static final String URLGET="http://172.17.8.100/movieApi/cinema/v1/findRecommendCinemas?longitude=116.30551391385724&latitude=40.04571807462411&page=1&count=10&userld=18&sessionld=15320748258726";
@Override
public void requestData(final onCallBack onCallBack) {
OKHttpUtils.getInstance().get(URLGET, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String estring = e.getMessage().toString();
onCallBack.stringMsg(estring);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responsestring = response.body().string();
onCallBack.stringMsg(responsestring);
}
});
}
}
m层
p层
public class PresenterImpl implements MainIcontrcat.IPresenter<MainIcontrcat.IView>{
MainIcontrcat.IView iView;
private MainIcontrcat.IModel iModel;
private WeakReference<MainIcontrcat.IView> viewWeakReference;
private WeakReference<MainIcontrcat.IModel> modelWeakReference;
@Override
public void attData(MainIcontrcat.IView iView) {
this.iView = iView;
iModel= new ModelImpl();
viewWeakReference = new WeakReference<>(iView);
modelWeakReference = new WeakReference<>(iModel);
}
@Override
public void deleteData(MainIcontrcat.IView iView) {
viewWeakReference.clear();
modelWeakReference.clear();
}
@Override
public void infoData() {
iModel.requestData(new MainIcontrcat.IModel.onCallBack() {
@Override
public void stringMsg(String Msg) {
iView.showData(Msg);
}
});
}
}
p层
main层
public interface MainIcontrcat {
interface IView{
void showData(String msg);
}
interface IModel{
interface onCallBack{
void stringMsg(String Msg);
};
void requestData(onCallBack onCallBack);
}
interface IPresenter<IView>{
void attData(IView iView);
void deleteData(IView iView);
void infoData();
}
}
main层
mainacrivity
public class MainActivity extends AppCompatActivity implements MainIcontrcat.IView{
@BindView(R.id.recyler_view)
RecyclerView recyclerView;
@BindView(R.id.main_img)
ImageView mainImg;
private MainIcontrcat.IPresenter iPresenter;
private List<MainBean.ResultBean.NearbyCinemaListBean> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mainImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MapActivity.class);
startActivity(intent);
}
});
iPresenter = new PresenterImpl();
iPresenter.attData(this);
iPresenter.infoData();
}
@Override
public void showData(final String msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
MainBean mainBean = gson.fromJson(msg, MainBean.class);
MainBean.ResultBean result = mainBean.getResult();
data = result.getNearbyCinemaList();
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
MainAdapter adapter = new MainAdapter(data, MainActivity.this);
recyclerView.setAdapter(adapter);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
iPresenter.deleteData(this);
}
}
mainactivity
mapactivoty
public class MapActivity extends AppCompatActivity implements LocationSource, AMapLocationListener {
//AMap是地图对象
private AMap aMap;
private MapView mapView;
//声明AMapLocationClient类对象,定位发起端
private AMapLocationClient mLocationClient = null;
//声明mLocationOption对象,定位参数
public AMapLocationClientOption mLocationOption = null;
//声明mListener对象,定位监听器
private LocationSource.OnLocationChangedListener mListener = null;
//标识,用于判断是否只显示一次定位信息和用户重新定位
private boolean isFirstLoc = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
}
setContentView(R.layout.activity_map);
//获取地图控件引用
mapView = findViewById(R.id.map);
//在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),实现地图生命周期管理
mapView.onCreate(savedInstanceState);
if (aMap == null) {
aMap = mapView.getMap();
//设置显示定位按钮 并且可以点击
UiSettings settings = aMap.getUiSettings();
aMap.setLocationSource(this);//设置了定位的监听
// 是否显示定位按钮
settings.setMyLocationButtonEnabled(true);
aMap.setMyLocationEnabled(true);//显示定位层并且可以触发定位,默认是flase
}
//开始定位
location();
}
private void location() {
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听
mLocationClient.setLocationListener(this);
//初始化定位参数
mLocationOption = new AMapLocationClientOption();
//设置定位模式为Hight_Accuracy高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
// mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
// //设置是否返回地址信息(默认返回地址信息)
// mLocationOption.setNeedAddress(true);
// //设置是否只定位一次,默认为false
mLocationOption.setOnceLocation(false);
//设置是否强制刷新WIFI,默认为强制刷新
mLocationOption.setWifiActiveScan(true);
//设置是否允许模拟位置,默认为false,不允许模拟位置
mLocationOption.setMockEnable(false);
//设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(2000);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation();
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mapView.onDestroy();
mLocationClient.stopLocation();//停止定位
mLocationClient.onDestroy();//销毁定位客户端。
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView.onResume (),实现地图生命周期管理
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView.onPause (),实现地图生命周期管理
mapView.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),实现地图生命周期管理
mapView.onSaveInstanceState(outState);
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
//定位成功回调信息,设置相关消息
aMapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见官方定位类型表
aMapLocation.getLatitude();//获取纬度
aMapLocation.getLongitude();//获取经度
aMapLocation.getAccuracy();//获取精度信息
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(aMapLocation.getTime());
df.format(date);//定位时间
aMapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
aMapLocation.getCountry();//国家信息
aMapLocation.getProvince();//省信息
aMapLocation.getCity();//城市信息
aMapLocation.getDistrict();//城区信息
aMapLocation.getStreet();//街道信息
aMapLocation.getStreetNum();//街道门牌号信息
aMapLocation.getCityCode();//城市编码
aMapLocation.getAdCode();//地区编码
// 如果不设置标志位,此时再拖动地图时,它会不断将地图移动到当前的位置
if (isFirstLoc) {
//设置缩放级别
aMap.moveCamera(CameraUpdateFactory.zoomTo(17));
//将地图移动到定位点
aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));
//点击定位按钮 能够将地图的中心移动到定位点
mListener.onLocationChanged(aMapLocation);
//添加图钉
// aMap.addMarker(getMarkerOptions(amapLocation));
//获取定位信息
StringBuffer buffer = new StringBuffer();
buffer.append(aMapLocation.getCountry() + ""
+ aMapLocation.getProvince() + ""
+ aMapLocation.getCity() + ""
+ aMapLocation.getProvince() + ""
+ aMapLocation.getDistrict() + ""
+ aMapLocation.getStreet() + ""
+ aMapLocation.getStreetNum());
Toast.makeText(getApplicationContext(), buffer.toString(), Toast.LENGTH_LONG).show();
isFirstLoc = false;
}
} else {
//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ aMapLocation.getErrorCode() + ", errInfo:"
+ aMapLocation.getErrorInfo());
Toast.makeText(getApplicationContext(), "定位失败", Toast.LENGTH_LONG).show();
}
}
}
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
mListener = onLocationChangedListener;
}
@Override
public void deactivate() {
mListener = null;
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}
mapactivity
适配器
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder>{
List<MainBean.ResultBean.NearbyCinemaListBean> data;
Context context;
public MainAdapter(List<MainBean.ResultBean.NearbyCinemaListBean> data, Context context) {
this.data = data;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.itme_neirong, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String split = data.get(position).getLogo();
Picasso.with(context).load(split).into(holder.imageView);
holder.textView.setText(data.get(position).getAddress());
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.itme_img);
textView = itemView.findViewById(R.id.itme_text);
}
}
}
适配器
utils
public class OKHttpUtils {
private static OKHttpUtils okHttpUtil;
private OkHttpClient okHttpClient;
private OKHttpUtils(){
if (null==okHttpClient){
synchronized (OkHttpClient.class){
if (null==okHttpClient){
okHttpClient = new OkHttpClient.Builder().build();
}
}
}
}
public static OKHttpUtils getInstance(){
if (null==okHttpUtil){
synchronized (OKHttpUtils.class){
if (null==okHttpUtil){
okHttpUtil = new OKHttpUtils();
}
}
}
return okHttpUtil;
}
public void get(String stringurl, Callback callback){
Request request = new Request.Builder().url(stringurl).build();
okHttpClient.newCall(request).enqueue(callback);
}
}
utils
main'xml
<ImageView
android:id="@+id/main_img"
android:layout_width="match_parent"
android:layout_height="20dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
mainxml
mapxml
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
></com.amap.api.maps.MapView>
mapxml
itme_neirong
<ImageView
android:id="@+id/itme_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
/>
<TextView
android:id="@+id/itme_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xxxx"
android:textSize="25sp"
/>
itme_neirong