酷欧天气2.0(四)——显示城市天气数据WeatherActivity

思路

用两个Fragment来承载城市的天气数据,再在WeatherActivity中根据滑动方向加载城市信息,并将这个数据显示在另一个切换出来的Fragment上。

代码

一、如何让两个Fragment的状态栏透明
如果用《第一行代码中》中的方法,会出现最先被add的Fragment状态栏透明,但后add的Fragment的状态栏却消失的情况。
解决方法就是先将系统的状态栏消失,再自己为Fragment的布局用View写个状态栏并设为透明,然后获取手机状态栏的高度,动态设置View的高度,就完成了。
1、系统状态栏消失

if(Build.VERSION.SDK_INT>=21){//如果版本号大于或等于21
            View decorView=getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    |View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
}

2、将View设置为状态栏



    
        
            
            
            
            
            
            
        
    

3、获取手机状态栏的高度

public class WindowAttr {
    public static int getStateBarHeight(Activity a) {
        int result = 0;
        int resourceId = a.getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = a.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}

4、动态设置View的高度

view_statusBar=(View)view.findViewById(R.id.view_statusBar);
view_statusBar.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowAttr.getStateBarHeight(getActivity())));

二、WeatherFragment:
1、两个Fragment除了名字完全一致,都用于显示城市的天气信息。

public void showWeatherInfo(Weather weather){
        String cityName=weather.basic.cityName;
        String updateTime=weather.basic.update.updateTime.split(" ")[1];
        String degree=weather.now.temperature+"℃";
        String weatherInfo=weather.now.more.info;
        titleCity.setText(cityName);
        titleUpdateTime.setText(updateTime);
        degreeText.setText(degree);
        weatherInfoText.setText(weatherInfo);
        forecastLayout.removeAllViews();
        for(Forecast forecast:weather.forecastList){//将forecastLayout的子项forecast_item加载进去
            View view= LayoutInflater.from(getActivity()).inflate(R.layout.forecast_item,forecastLayout,false);
            TextView dateText=(TextView)view.findViewById(R.id.date_text);
            TextView infoText=(TextView)view.findViewById(R.id.info_text);
            TextView maxText=(TextView)view.findViewById(R.id.max_text);
            TextView minText=(TextView)view.findViewById(R.id.min_text);
            dateText.setText(forecast.date);
            infoText.setText(forecast.more.info);
            maxText.setText(forecast.temperature.max+"℃");
            minText.setText(forecast.temperature.min+"℃");
            forecastLayout.addView(view);
        }
        if(weather.aqi!=null){
            aqiText.setText(weather.aqi.city.aqi);
            pm25Text.setText(weather.aqi.city.pm25);
        }
        String comfort="舒适度:"+weather.suggestion.comfort.info;
        String carWash="洗车指数:"+weather.suggestion.carWash.info;
        String sport="运动建议:"+weather.suggestion.sport.info;
        comfortText.setText(comfort);
        carWashText.setText(carWash);
        sportText.setText(sport);
        weatherLayout.setVisibility(View.VISIBLE);
}

2、切换Fragment时,被hide的WeatherFragment需要被置顶,但切换是有动画效果的,置顶的操作不能在切换时被发现,所以我延迟了500ms执行置顶效果。

public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
            /*延时500毫秒*/
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    weatherLayout.smoothScrollTo(0,0);//将fragment置顶
                }
            }, 500);
        }else {

        }
    }

3、在WeatherActivity初始化Fragment,先隐藏FragmentTwo,显示FragmentOne。

private void initFragment(){
        fragmentOne=new WeatherFragmentOne();
        fragmentTwo=new WeatherFragmentTwo();
        transaction=getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.framelayout_weather,fragmentOne);
        transaction.add(R.id.framelayout_weather,fragmentTwo);
        transaction.hide(fragmentTwo);
        transaction.commit();
}

三、onStart()
因为从WeatherAddActivity退回到WeatherActivity后,城市信息会有变化,所以我把获取城市数据和显示城市天气信息的操作都挪到了onStart()中进行。

1、获取数据和显示信息,并得到数据库中城市的数量weatherNum,及当前要显示数据库中第几个城市的信息defNum。

private void showFragment(){
        List countyAdditions= DataSupport.findAll(County_Addition.class);
        for(County_Addition countyAddition:countyAdditions){
            weatherNum++;
            if(countyAddition.getDef()==true)
                defNum=weatherNum;
            requestWeather(countyAddition.getCountyWeatherId(),countyAddition,STATE);
        }
}
public void requestWeather(final String weatherId, final County_Addition county_addition,final int frag_state){
        String weatherUrl="http://guolin.tech/api/weather?cityid="+weatherId+
                "&key=63660528a9dc4f968243a7f0669cbe26";
        HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String responseText=response.body().string();
                final Weather weather=Utility.handleWeatherResponse(responseText);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(weather!=null&&"ok".equals(weather.status)){
                            county_addition.setCountyWeather(responseText);
                            county_addition.save();
                            if(county_addition.getDef()==true){
                                if(frag_state==1)
                                    fragmentOne.showWeatherInfo(weather);
                                else if(frag_state==2)
                                    fragmentTwo.showWeatherInfo(weather);
                            }
                        }else {
                            Toast.makeText(WeatherActivity.this,"获取天气信息失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
            public void onFailure(Call call, IOException e){
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(WeatherActivity.this,"获取天气信息失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

2、根据手指触控事件切换城市。一开始我用的onTouchEvent来判断左右滑动指示,但因为我的Fragment界面用到了ScrollView,所以自己写的onToucheEvent不能触发,大概是被ScrollView消费了,之后改成dispatchTouchEvent居然就解决了!!!

public boolean dispatchTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            x1=event.getX();
        }
        if(event.getAction()==MotionEvent.ACTION_UP){
            x2=event.getX();
            if(x2-x1>100){
                changeFragment(LEFT);
            }else if(x1-x2>100){
                changeFragment(RIGHT);
            }
        }
        return super.dispatchTouchEvent(event);
}

3、Fragment切换操作

private void changeFragment(int lr){
        transaction=getSupportFragmentManager().beginTransaction();
        if(lr==RIGHT){
            transaction.setCustomAnimations(R.anim.push_left_in,R.anim.push_left_out);
            if(defNum1){
                defNum--;
                showFragment(transaction,defNum);
            }
        }
        transaction.commit();
}
private void showFragment(FragmentTransaction transaction,int num){
        int i=0;
        if(STATE==1){
            transaction.hide(fragmentOne);
            transaction.show(fragmentTwo);
            List countyAdditions= DataSupport.findAll(County_Addition.class);
            for(County_Addition countyAddition:countyAdditions){
                i++;
                if(i==num){
                    fragmentTwo.showWeatherInfo(Utility.handleWeatherResponse(countyAddition.getCountyWeather()));
                    break;
                }
            }
            STATE=2;
        }else if(STATE==2){
            transaction.hide(fragmentTwo);
            List countyAdditions= DataSupport.findAll(County_Addition.class);
            for(County_Addition countyAddition:countyAdditions){
                i++;
                if(i==num){
                    fragmentOne.showWeatherInfo(Utility.handleWeatherResponse(countyAddition.getCountyWeather()));
                    break;
                }
            }
            transaction.show(fragmentOne);
            STATE=1;
        }
}

源码

百度网盘:https://pan.baidu.com/s/1ZxDCr3ceptIUOFHWc_q2QA (密码: hdkf)

你可能感兴趣的:(酷欧天气2.0(四)——显示城市天气数据WeatherActivity)