大局观:
智能农业是 远程监控一定区域内的 空气浓度,co2浓度,预警,等
效果图
首先 我们首先的问题就是静态布局:
强调内容
首先我们在布局里 进行设置哪块区域进行滑动效果;
.support.v4.view.ViewPager
android:id="@+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/main_down"
>
.support.v4.view.ViewPager>
创建适配器 继承 FragmentPagerAdapter;
public class Myadaper extends FragmentPagerAdapter{
private List mfragmentList;
public Myadaper(FragmentManager fm,List fragmentList) {
super(fm);
this.mfragmentList=fragmentList;
}
@Override
public Fragment getItem(int position) {
return mfragmentList.get(position);
}
@Override
public int getCount() {
return mfragmentList.size();
}
}
因为有3个页面 首页 、设置、帮助 所以我们创建了3个Fragment
在Mainticity里 ,我们创建数组Fragment 和 创建对象适配器 ,并且进行绑定:
private ViewPager viewPager;
private LinearLayout shouye;
private LinearLayout shezhi;
private LinearLayout bangzhu;
private List fragmentList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
binID();
AFragment aFragment=new AFragment();
BFragment bFragment=new BFragment();
CFragment cFragment=new CFragment();
fragmentList.add(bFragment);
fragmentList.add(aFragment);
fragmentList.add(cFragment);
Myadaper adaper=new Myadaper(getSupportFragmentManager(),fragmentList);
viewPager.setAdapter(adaper);
}
看我们那个图片 是不是想到用自定义view? 其实我没有, 我只需要3行代码就完成了 很神奇吧!
第一行:(添加依赖包)
compile 'com.makeramen:roundedimageview:2.2.1'
第二行:(替换Imagview)
将Imagvew 替换成 com.makeramen.roundedimageview.RoundedImageView
第3行 (注意我上面里面的内容多出什么了吗)
这样我们的图片就变成圆形了吗 而不是单一的了
难点 :如何解析数据: 用什么方式解析
添加依赖包:
compile 'com.loopj.android:android-async-http:1.4.9'
创建一个类 :
这里我用post()方法:
public class HttpUrl {
private static AsyncHttpClient client = new AsyncHttpClient();
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
client.get(qwe(url),params,responseHandler);
}
private static String qwe(String re){
return re;
}
}
调用post方法 :
HttpUrl.post(URL + "getSensor", requestParams, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(getActivity(), "失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Gson gson = new Gson();
MyDemo myDemo = gson.fromJson(responseString, MyDemo.class);
airHumidity = myDemo.getAirHumidity();
airTemperature = myDemo.getAirTemperature();
soilTemperature = myDemo.getSoilTemperature();
light = myDemo.getLight();
Co2 = myDemo.getCo2();
soilHumidity = myDemo.getSoilHumidity();
airHumidity1.setText(airHumidity + " ");
airTemperature1.setText(airTemperature + " ");
soilTemperature1.setText(soilTemperature + " ");
light1.setText(light + " ");
Co21.setText(Co2 + " ");
soilHumidity1.setText(soilHumidity + " ");
Log.e("1111", "onSuccess: " + responseString);
Toast.makeText(getActivity(), "成功", Toast.LENGTH_SHORT).show();
}
});
这里我用了Gson 解析 而不是json 所以 我们需要添加他的依赖包:
compile 'com.google.code.gson:gson:2.7'
和上面的一样,我们还是创建一个类
public class Okhttp {
private static OkHttpClient client = new OkHttpClient();
public static void sendOkHttpRequest(String address,okhttp3.Callback callback){
Request request = new Request.Builder().url(address).build();
client.newCall(request).enqueue(callback);
}
public static void postJsonByOkHttp(String url, JSONObject jsonObject,okhttp3.Callback callback){
MediaType mediaType = MediaType.parse("application/json;Charset=UTF-8");
RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString());
Request request = new Request.Builder().url(url).post(requestBody).build();
client.newCall(request).enqueue(callback);
}
}
在解析的页面里:
private void getAppValues() {
Okhttp.sendOkHttpRequest(basicUrl2, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
closeProgressDialog();
Toast.makeText(getActivity(), "network failture", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
flag++;
String responseString = null;
try {
responseString = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
Log.e(TAG, "onResponse: getval"+ responseString);
Gson gson = new Gson();
App app = gson.fromJson(responseString,App.class);
airHumidity = app.getAirHumidity();
PM25 = app.getPM25();
airTemperature = app.getAirTemperature();
soilTemperature = app.getSoilTemperature();
co2 = app.getCo2();
soilHumidity = app.getSoilHumidity();
light = app.getLight();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if(flag==2){
closeProgressDialog();
}
co2Tv.setText(co2+"");
lightTv.setText(light+"");
soilTempTv.setText(soilTemperature+"");
soilWetnessTv.setText(soilHumidity+"");
airTempTv.setText(airTemperature+"");
airWetnessTv.setText(airHumidity+"");
//Log.e(TAG, "onSuccess: "+responseString );
}
});
}
});
难点:
编程思想 :
由于四个指数 里面都有很多的子控件,所以 我们可以创建公共类 来优化代码 减轻代码压力
在公共类里面 我举例其中一个例子:
public void open1() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("Roadlamp", 1);
} catch (JSONException e) {
e.printStackTrace();
}
ByteArrayEntity entity = null;
try {
entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpUrlSecond.post(GuangzhaoActivity.this, "control", entity, "application/json", new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Status.Roadlamp = 1;
}
});
}
public void close1() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("Roadlamp", 0);
} catch (JSONException e) {
e.printStackTrace();
}
ByteArrayEntity entity = null;
try {
entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpUrlSecond.post(GuangzhaoActivity.this, "control", entity, "application/json", new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Status.Roadlamp = 0;
}
});
}
通过添加标志位对 图片进行切换效果:
设置if语句:
if (Result == 1) {
open1();
guangzhao.setImageResource(R.mipmap.dakaiguangzhao2);
Toast.makeText(GuangzhaoActivity.this, "打开", Toast.LENGTH_SHORT).show();
} else if (Result == 0) {
close1();
guangzhao.setImageResource(R.mipmap.dakaiguangzhao);
Toast.makeText(GuangzhaoActivity.this, "关闭", Toast.LENGTH_SHORT).show();
}
客户不知道数据什么显示,所以我们需要设置一个进度条 让客户知道,软件实时的反馈:
dialog
private void showProgressDialog(){
if (progressDialog==null){
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("loading....");
progressDialog.setCanceledOnTouchOutside(false);
}
progressDialog.show();
}
private void closeProgressDialog(){
if(progressDialog!=null){
progressDialog.dismiss();//当获取后,关闭进度条
}
}
总结难点: