依赖
implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.squareup.okhttp3:okhttp:3.11.0' implementation 'com.github.bumptech.glide:glide:3.7.0' implementation 'jp.wasabeef:glide-transformations:1.0.6' implementation 'com.google.code.gson:gson:2.8.5' implementation 'cn.yipianfengye.android:zxing-library:2.2' //3D地图so及jar implementation 'com.amap.api:3dmap:latest.integration' //定位功能 implementation 'com.amap.api:location:latest.integration' //搜索功能 implementation 'com.amap.api:search:latest.integration'
权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--允许程序打开网络套接字--> <uses-permission android:name="android.permission.INTERNET" /> <!--允许程序设置内置sd卡的写权限--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!--允许程序获取网络状态--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--允许程序访问WiFi网络信息--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!--允许程序读写手机状态和身份--> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!--允许程序访问CellID或WiFi热点来获取粗略的位置--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
mainlayout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/ditu_text" android:text="地图"/> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/sao" android:text="扫一扫"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="布局切换" android:id="@+id/qiehuan"/> </LinearLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/recyclerView"></android.support.v7.widget.RecyclerView> </LinearLayout>
main2layout
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main2Activity"> <com.amap.api.maps.MapView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/map"></com.amap.api.maps.MapView> </android.support.constraint.ConstraintLayout>
itemlayout
<?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="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="200dp" android:layout_height="100dp" android:layout_margin="10dp" android:id="@+id/item_iamge"/> <LinearLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="100dp" android:orientation="vertical" android:layout_marginTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="店名" android:id="@+id/name_text"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="地址" android:layout_marginTop="10dp" android:id="@+id/address_text"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="距离" android:layout_margin="10dp" android:id="@+id/juli_text"/> </LinearLayout>
adapter
package dongyushan.bwie.com.lala.adapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; import dongyushan.bwie.com.lala.R; import dongyushan.bwie.com.lala.bean.UserBean; public class ShowAdapter extends RecyclerView.Adapter<ShowAdapter.MyViewHolder>{ private Context context; private List<UserBean.ResultBean.NearbyCinemaListBean> list; public ShowAdapter(Context context, List<UserBean.ResultBean.NearbyCinemaListBean> list) { this.context = context; this.list = list; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(context).inflate(R.layout.itemlayout,viewGroup,false); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) { myViewHolder.name_text.setText(list.get(i).getName()); myViewHolder.address_text.setText(list.get(i).getAddress()); Glide.with(context).load(list.get(i).getLogo()).into(myViewHolder.item_image); } @Override public int getItemCount() { return list.size(); } public class MyViewHolder extends RecyclerView.ViewHolder{ private TextView name_text,address_text,juli_text; private ImageView item_image; public MyViewHolder(View itemView) { super(itemView); name_text = itemView.findViewById(R.id.name_text); address_text = itemView.findViewById(R.id.address_text); juli_text = itemView.findViewById(R.id.juli_text); item_image = itemView.findViewById(R.id.item_iamge); } } }
bean
package dongyushan.bwie.com.lala.bean; import java.util.List; public class UserBean { /** * result : {"followCinemas":[],"nearbyCinemaList":[{"address":"北京市海淀区上地南口华联商厦4F","commentTotal":129,"distance":1647,"followCinema":false,"id":18,"logo":"http://172.17.8.100/images/movie/logo/ctjh.jpg","name":"橙天嘉禾影城北京上地店"},{"address":"北京海淀区海淀区清河中街68号五彩城购物中心东区7层","commentTotal":105,"distance":3315,"followCinema":false,"id":22,"logo":"http://172.17.8.100/images/movie/logo/CGVyc.jpg","name":"CGV影城(清河店)"},{"address":"北京海淀区悦秀路99号二层大地影院","commentTotal":40,"distance":3416,"followCinema":false,"id":19,"logo":"http://172.17.8.100/images/movie/logo/ddyy.jpg","name":"大地影院-北京海淀西三旗物美"},{"address":"北京市育知东路30号华联商厦4层","commentTotal":24,"distance":4838,"followCinema":false,"id":20,"logo":"http://172.17.8.100/images/movie/logo/wmyc.jpg","name":"北京沃美影城(回龙观店)"},{"address":"北京市昌平区黄平路19号院龙旗购物中心3层","commentTotal":6,"distance":5090,"followCinema":false,"id":17,"logo":"http://172.17.8.100/images/movie/logo/blgj.jpg","name":"保利国际影城北京龙旗广场店"},{"address":"海淀区中关村广场购物中心津乐汇三层(鼎好一期西侧)","commentTotal":5,"distance":6387,"followCinema":false,"id":12,"logo":"http://172.17.8.100/images/movie/logo/mjhlyc.jpg","name":"美嘉欢乐影城中关村店"},{"address":"北京市海淀区中关村大街19号新中关购物中心B1层","commentTotal":1,"distance":6982,"followCinema":false,"id":15,"logo":"http://172.17.8.100/images/movie/logo/jy.jpg","name":"金逸北京中关村店"},{"address":"北京市海淀区中关村大街28号","commentTotal":9,"distance":7224,"followCinema":false,"id":16,"logo":"http://172.17.8.100/images/movie/logo/hdjy.jpg","name":"海淀剧院"},{"address":"北京市海淀区远大路1号B座5层魔影国际影城","commentTotal":9,"distance":8836,"followCinema":false,"id":4,"logo":"http://172.17.8.100/images/movie/logo/mygj.jpg","name":"魔影国际影城"},{"address":"海淀区远大路1号金源时代购物中心5层东首","commentTotal":3,"distance":9140,"followCinema":false,"id":11,"logo":"http://172.17.8.100/images/movie/logo/xmgj.jpg","name":"星美国际影城"}]} * message : 查询成功 * status : 0000 */ private ResultBean result; private String message; private String status; public ResultBean getResult() { return result; } public void setResult(ResultBean result) { this.result = result; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public static class ResultBean { private List<?> followCinemas; private List<NearbyCinemaListBean> nearbyCinemaList; public List<?> getFollowCinemas() { return followCinemas; } public void setFollowCinemas(List<?> followCinemas) { this.followCinemas = followCinemas; } public List<NearbyCinemaListBean> getNearbyCinemaList() { return nearbyCinemaList; } public void setNearbyCinemaList(List<NearbyCinemaListBean> nearbyCinemaList) { this.nearbyCinemaList = nearbyCinemaList; } public static class NearbyCinemaListBean { /** * address : 北京市海淀区上地南口华联商厦4F * commentTotal : 129 * distance : 1647 * followCinema : false * id : 18 * logo : http://172.17.8.100/images/movie/logo/ctjh.jpg * name : 橙天嘉禾影城北京上地店 */ private String address; private int commentTotal; private int distance; private boolean followCinema; private int id; private String logo; private String name; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getCommentTotal() { return commentTotal; } public void setCommentTotal(int commentTotal) { this.commentTotal = commentTotal; } public int getDistance() { return distance; } public void setDistance(int distance) { this.distance = distance; } public boolean isFollowCinema() { return followCinema; } public void setFollowCinema(boolean followCinema) { this.followCinema = followCinema; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLogo() { return logo; } public void setLogo(String logo) { this.logo = logo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } } }
event
package dongyushan.bwie.com.lala.event; import java.io.IOException; import okhttp3.Call; import okhttp3.Response; public interface ShowCallBack { void onSuccess(Call call, Response response); void onFailure(Call call, IOException e); }
model
package dongyushan.bwie.com.lala.model; import java.io.IOException; import dongyushan.bwie.com.lala.event.ShowCallBack; import dongyushan.bwie.com.lala.utils.OkHttpUtils; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class ShowModel { OkHttpClient okHttpClient = new OkHttpClient(); public void show(final ShowCallBack showCallBack) { /*FormBody formBody = new FormBody.Builder() .add("userId", "18") .add("sessionId", "15320748258726") .add("longitude", "116.305513913852724") .add("latitude", "40.04571807462411") .add("page", "1") .add("count", "10") .build(); Request request = new Request.Builder() .url("http://172.17.8.100/movieApi/cinema/v1/findRecommendCinemas?userId=18&sessionId=15320748258726&longitude=116.305513913852724&latitude=40.04571807462411&page=1&count=10") //.post(formBody) .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { showCallBack.onFailure(call, e); } @Override public void onResponse(Call call, Response response) throws IOException { showCallBack.onSuccess(call, response); } });*/ OkHttpUtils.getstance().getdata("http://172.17.8.100/movieApi/cinema/" + "v1/findRecommendCinemas?userId=18&sessionId=1532" + "0748258726&longitude=116.305513913852724&latitude=40.04571807462411&page=1&count=10", new Callback() { @Override public void onFailure(Call call, IOException e) { showCallBack.onFailure(call, e); } @Override public void onResponse(Call call, Response response) throws IOException { showCallBack.onSuccess(call, response); } }); } }
present
package dongyushan.bwie.com.lala.present; import android.util.Log; import java.io.IOException; import dongyushan.bwie.com.lala.event.ShowCallBack; import dongyushan.bwie.com.lala.model.ShowModel; import dongyushan.bwie.com.lala.view.ShowView; import okhttp3.Call; import okhttp3.Response; public class ShowPresenter { private ShowView showView; private ShowModel showModel; public ShowPresenter(ShowView showView) { this.showView = showView; showModel=new ShowModel(); } public void show(){ showModel.show(new ShowCallBack() { @Override public void onSuccess(Call call, Response response) { try { String result = response.body().string(); Log.i("aaa",result+"--------------------------------"); showView.onSuccess(result); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Call call, IOException e) { showView.onFailure("失败"); } }); } }
utils
package dongyushan.bwie.com.lala.utils; import android.util.Log; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class OkHttpUtils { private static OkHttpUtils okHttpUtils; public OkHttpClient okHttpClient; public OkHttpUtils(){ okHttpClient=new OkHttpClient(); } //自定义拦截器 class longinterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request.Builder builder = chain.request().newBuilder().addHeader("source", "android"); Log.d("xxxxxx", "request:" + request); Response proceed = chain.proceed(request); return proceed; } } public static OkHttpUtils getstance(){ if (okHttpUtils==null){ synchronized (OkHttpUtils.class){ if (okHttpUtils==null){ okHttpUtils=new OkHttpUtils(); } } } return okHttpUtils; } public void getdata(String path, Callback callback){ Request request=new Request.Builder().url(path).build(); Call call=okHttpClient.newCall(request); call.enqueue(callback); } }
view
package dongyushan.bwie.com.lala.view; public interface ShowView { void onSuccess(String result); void onFailure(String msg); }
main2ac
package dongyushan.bwie.com.lala; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.amap.api.maps.AMap; import com.amap.api.maps.MapView; import com.amap.api.maps.model.MyLocationStyle; public class Main2Activity extends AppCompatActivity { private MapView mMapView = null; AMap aMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //获取地图控件引用 mMapView =findViewById(R.id.map); //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图 mMapView.onCreate(savedInstanceState); if (aMap == null) { aMap = mMapView.getMap(); } MyLocationStyle myLocationStyle; myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。 myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。 aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style //aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示,非必需设置。 aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。 } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制 mMapView.onPause(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态 mMapView.onSaveInstanceState(outState); } }
main
package dongyushan.bwie.com.lala; import android.content.Intent; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import com.uuzuche.lib_zxing.activity.CaptureActivity; import com.uuzuche.lib_zxing.activity.CodeUtils; import com.uuzuche.lib_zxing.activity.ZXingLibrary; import java.util.List; import dongyushan.bwie.com.lala.adapter.ShowAdapter; import dongyushan.bwie.com.lala.bean.UserBean; import dongyushan.bwie.com.lala.present.ShowPresenter; import dongyushan.bwie.com.lala.view.ShowView; public class MainActivity extends AppCompatActivity implements ShowView{ private ShowPresenter showPresenter; private RecyclerView recyclerView; private TextView ditu_text,qiehuan,sao; private List<UserBean.ResultBean.NearbyCinemaListBean> list; private int isi = 1; private int REQUEST_CODE = 0x1000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ZXingLibrary.initDisplayOpinion(this); recyclerView = findViewById(R.id.recyclerView); ditu_text = findViewById(R.id.ditu_text); qiehuan = findViewById(R.id.qiehuan); sao = findViewById(R.id.sao); showPresenter=new ShowPresenter(this); showPresenter.show(); ditu_text.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } }); qiehuan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (isi % 2 == 0){ recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false)); recyclerView.setAdapter(new ShowAdapter(MainActivity.this,list)); isi--; }else { recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,2)); recyclerView.setAdapter(new ShowAdapter(MainActivity.this,list)); isi++; } } }); sao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,CaptureActivity.class ); startActivityForResult(intent,REQUEST_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE) { //处理扫描结果(在界面上显示) if (null != data) { Bundle bundle = data.getExtras(); if (bundle == null) { return; } if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) { String result = bundle.getString(CodeUtils.RESULT_STRING); Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show(); } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) { Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show(); } } } } @Override public void onSuccess(final String result) { runOnUiThread(new Runnable() { @Override public void run() { //Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show(); Gson gson = new Gson(); UserBean userBean = gson.fromJson(result, UserBean.class); list = userBean.getResult().getNearbyCinemaList(); recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false)); recyclerView.setAdapter(new ShowAdapter(MainActivity.this,list)); } }); } @Override public void onFailure(final String msg) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show(); } }); } }