Skip to content

Commit

Permalink
代码优化,优化命名问题
Browse files Browse the repository at this point in the history
  • Loading branch information
maning0303 committed Oct 16, 2020
1 parent bcb2f54 commit c9cace3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 60 deletions.
80 changes: 42 additions & 38 deletions app/src/main/java/com/maning/gankmm/ui/adapter/WeatherAdapter.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.maning.gankmm.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.maning.gankmm.R;
import com.maning.gankmm.bean.rolltools.HolidayBean;
import com.maning.gankmm.bean.rolltools.WeatherFuturedaysResultBean;
Expand Down Expand Up @@ -61,46 +63,46 @@ public void updateDatas(WeatherInfoBean weatherEntity, HolidayBean holidayBean,
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0) {
View inflate = layoutInflater.inflate(R.layout.item_weather_header, parent, false);
return new WeatherAdapter.MyViewHolder01(inflate);
return new WeatherAdapter.ViewHolderWeatherInfo(inflate);
} else if (viewType == 1) {
View inflate = layoutInflater.inflate(R.layout.item_weather_later, parent, false);
return new WeatherAdapter.MyViewHolder02(inflate);
return new WeatherAdapter.ViewHolderWeatherForecast(inflate);
} else if (viewType == 2) {
View inflate = layoutInflater.inflate(R.layout.item_weather_suggestion, parent, false);
return new WeatherAdapter.MyViewHolderSuggestion(inflate);
return new WeatherAdapter.ViewHolderSuggestion(inflate);
} else if (viewType == 3) {
View inflate = layoutInflater.inflate(R.layout.item_weather_calendar, parent, false);
return new WeatherAdapter.MyViewHolder04(inflate);
return new WeatherAdapter.ViewHolderHoliday(inflate);
} else {
return null;
}
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder01) {
final MyViewHolder01 myViewHolder01 = (MyViewHolder01) holder;
myViewHolder01.tv_01.setText(weatherEntity.getTemperature() + "°");
myViewHolder01.tv_02.setText(weatherEntity.getWeather_desc());
myViewHolder01.tv_03.setText("湿度:" + weatherEntity.getHumidity() + "%");
myViewHolder01.tv_04.setText("体感:" + weatherEntity.getFeels_like() + "°");
myViewHolder01.tv_05.setText(weatherEntity.getWind_direction() + " " + weatherEntity.getWind_scale() + "级");
if (holder instanceof ViewHolderWeatherInfo) {
final ViewHolderWeatherInfo viewHolderWeatherInfo = (ViewHolderWeatherInfo) holder;
viewHolderWeatherInfo.tv_01.setText(weatherEntity.getTemperature() + "°");
viewHolderWeatherInfo.tv_02.setText(weatherEntity.getWeather_desc());
viewHolderWeatherInfo.tv_03.setText("湿度:" + weatherEntity.getHumidity() + "%");
viewHolderWeatherInfo.tv_04.setText("体感:" + weatherEntity.getFeels_like() + "°");
viewHolderWeatherInfo.tv_05.setText(weatherEntity.getWind_direction() + " " + weatherEntity.getWind_scale() + "级");

} else if (holder instanceof MyViewHolder02) {
final MyViewHolder02 myViewHolder02 = (MyViewHolder02) holder;
} else if (holder instanceof ViewHolderWeatherForecast) {
final ViewHolderWeatherForecast viewHolderWeatherForecast = (ViewHolderWeatherForecast) holder;

//初始化RecycleView
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
myViewHolder02.recycle_later.setLayoutManager(linearLayoutManager);
myViewHolder02.recycle_later.setItemAnimator(new DefaultItemAnimator());
myViewHolder02.recycle_later.addItemDecoration(new VerticalDividerItemDecoration.Builder(mContext).color(mContext.getResources().getColor(R.color.lineColor)).build());
viewHolderWeatherForecast.recycle_later.setLayoutManager(linearLayoutManager);
viewHolderWeatherForecast.recycle_later.setItemAnimator(new DefaultItemAnimator());
viewHolderWeatherForecast.recycle_later.addItemDecoration(new VerticalDividerItemDecoration.Builder(mContext).color(mContext.getResources().getColor(R.color.lineColor)).build());
if (weatherForecasts != null && weatherForecasts.size() > 0) {
Weather2Adapter weather2Adapter = new Weather2Adapter(mContext, weatherForecasts);
myViewHolder02.recycle_later.setAdapter(weather2Adapter);
viewHolderWeatherForecast.recycle_later.setAdapter(weather2Adapter);

}
} else if (holder instanceof MyViewHolderSuggestion) {
final MyViewHolderSuggestion viewHolderSuggestion = (MyViewHolderSuggestion) holder;
} else if (holder instanceof ViewHolderSuggestion) {
final ViewHolderSuggestion viewHolderSuggestion = (ViewHolderSuggestion) holder;
//生活指数
if (lifeSuggestionBean != null) {
viewHolderSuggestion.tv_suggestion_01.setText(lifeSuggestionBean.getCar_washing().getBrief());
Expand All @@ -117,15 +119,15 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
viewHolderSuggestion.tv_suggestion_05.setText("旅游");
viewHolderSuggestion.tv_suggestion_06.setText("紫外线");
}
} else if (holder instanceof MyViewHolder04) {
final MyViewHolder04 myViewHolder04 = (MyViewHolder04) holder;
} else if (holder instanceof ViewHolderHoliday) {
final ViewHolderHoliday viewHolderHoliday = (ViewHolderHoliday) holder;

myViewHolder04.tv_01.setText(holidayBean.getDate());
myViewHolder04.tv_06.setText(holidayBean.getTypeDes());
myViewHolder04.tv_02.setText(holidayBean.getLunarCalendar());
myViewHolder04.tv_03.setText(holidayBean.getYearTips() + " (" + holidayBean.getChineseZodiac() + ") - " + holidayBean.getSolarTerms());
myViewHolder04.tv_04.setText(holidayBean.getSuit());
myViewHolder04.tv_05.setText(holidayBean.getAvoid());
viewHolderHoliday.tv_01.setText(holidayBean.getDate());
viewHolderHoliday.tv_06.setText(holidayBean.getTypeDes());
viewHolderHoliday.tv_02.setText(holidayBean.getLunarCalendar());
viewHolderHoliday.tv_03.setText(holidayBean.getYearTips() + " (" + holidayBean.getChineseZodiac() + ") - " + holidayBean.getSolarTerms());
viewHolderHoliday.tv_04.setText(holidayBean.getSuit());
viewHolderHoliday.tv_05.setText(holidayBean.getAvoid());

}

Expand All @@ -145,7 +147,7 @@ public int getItemViewType(int position) {
return position;
}

public static class MyViewHolder01 extends RecyclerView.ViewHolder {
public static class ViewHolderWeatherInfo extends RecyclerView.ViewHolder {

@Bind(R.id.tv_01)
TextView tv_01;
Expand All @@ -158,24 +160,26 @@ public static class MyViewHolder01 extends RecyclerView.ViewHolder {
@Bind(R.id.tv_05)
TextView tv_05;

public MyViewHolder01(View itemView) {
public ViewHolderWeatherInfo(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}

public static class MyViewHolder02 extends RecyclerView.ViewHolder {
public static class ViewHolderWeatherForecast extends RecyclerView.ViewHolder {

@Bind(R.id.recycle_later)
RecyclerView recycle_later;
@Bind(R.id.ll_root_weather_forecast)
LinearLayout ll_root_weather_forecast;

public MyViewHolder02(View itemView) {
public ViewHolderWeatherForecast(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}

public static class MyViewHolderSuggestion extends RecyclerView.ViewHolder {
public static class ViewHolderSuggestion extends RecyclerView.ViewHolder {

@Bind(R.id.tv_suggestion_01)
TextView tv_suggestion_01;
Expand All @@ -190,7 +194,7 @@ public static class MyViewHolderSuggestion extends RecyclerView.ViewHolder {
@Bind(R.id.tv_suggestion_06)
TextView tv_suggestion_06;

public MyViewHolderSuggestion(View itemView) {
public ViewHolderSuggestion(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
Expand All @@ -208,7 +212,7 @@ public MyViewHolderAir(View itemView) {
}


public static class MyViewHolder04 extends RecyclerView.ViewHolder {
public static class ViewHolderHoliday extends RecyclerView.ViewHolder {

@Bind(R.id.tv_01)
TextView tv_01;
Expand All @@ -223,7 +227,7 @@ public static class MyViewHolder04 extends RecyclerView.ViewHolder {
@Bind(R.id.tv_06)
TextView tv_06;

public MyViewHolder04(View itemView) {
public ViewHolderHoliday(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.maning.gankmm.utils.NetUtils;
import com.maning.gankmm.utils.PermissionUtils;
import com.maning.gankmm.utils.SharePreUtil;
import com.maning.gankmm.utils.ThreadPoolUtils;

/**
* Created by maning on 16/6/21.
Expand Down Expand Up @@ -100,22 +101,27 @@ public void destroyLocation() {

@Override
public void initDatas() {
SharePreUtil.saveIntData(context, "未知", R.drawable.icon_weather_none);
SharePreUtil.saveIntData(context, "晴", R.drawable.icon_weather_sunny);
SharePreUtil.saveIntData(context, "阴", R.drawable.icon_weather_cloudy);
SharePreUtil.saveIntData(context, "多云", R.drawable.icon_weather_cloudy);
SharePreUtil.saveIntData(context, "少云", R.drawable.icon_weather_cloudy);
SharePreUtil.saveIntData(context, "晴间多云", R.drawable.icon_weather_cloudytosunny);
SharePreUtil.saveIntData(context, "局部多云", R.drawable.icon_weather_cloudy);
SharePreUtil.saveIntData(context, "雨", R.drawable.icon_weather_rain);
SharePreUtil.saveIntData(context, "小雨", R.drawable.icon_weather_rain);
SharePreUtil.saveIntData(context, "中雨", R.drawable.icon_weather_rain);
SharePreUtil.saveIntData(context, "大雨", R.drawable.icon_weather_rain);
SharePreUtil.saveIntData(context, "阵雨", R.drawable.icon_weather_rain);
SharePreUtil.saveIntData(context, "雷阵雨", R.drawable.icon_weather_thunderstorm);
SharePreUtil.saveIntData(context, "霾", R.drawable.icon_weather_haze);
SharePreUtil.saveIntData(context, "雾", R.drawable.icon_weather_fog);
SharePreUtil.saveIntData(context, "雨夹雪", R.drawable.icon_weather_snowrain);
ThreadPoolUtils.execute(new Runnable() {
@Override
public void run() {
SharePreUtil.saveIntData(context, "未知", R.drawable.icon_weather_none);
SharePreUtil.saveIntData(context, "晴", R.drawable.icon_weather_sunny);
SharePreUtil.saveIntData(context, "阴", R.drawable.icon_weather_cloudy);
SharePreUtil.saveIntData(context, "多云", R.drawable.icon_weather_cloudy);
SharePreUtil.saveIntData(context, "少云", R.drawable.icon_weather_cloudy);
SharePreUtil.saveIntData(context, "晴间多云", R.drawable.icon_weather_cloudytosunny);
SharePreUtil.saveIntData(context, "局部多云", R.drawable.icon_weather_cloudy);
SharePreUtil.saveIntData(context, "雨", R.drawable.icon_weather_rain);
SharePreUtil.saveIntData(context, "小雨", R.drawable.icon_weather_rain);
SharePreUtil.saveIntData(context, "中雨", R.drawable.icon_weather_rain);
SharePreUtil.saveIntData(context, "大雨", R.drawable.icon_weather_rain);
SharePreUtil.saveIntData(context, "阵雨", R.drawable.icon_weather_rain);
SharePreUtil.saveIntData(context, "雷阵雨", R.drawable.icon_weather_thunderstorm);
SharePreUtil.saveIntData(context, "霾", R.drawable.icon_weather_haze);
SharePreUtil.saveIntData(context, "雾", R.drawable.icon_weather_fog);
SharePreUtil.saveIntData(context, "雨夹雪", R.drawable.icon_weather_snowrain);
}
});
}


Expand All @@ -128,13 +134,13 @@ public void onSuccess(AMapLocation aMapLocation) {
double latitude = aMapLocation.getLatitude();
//获取城市
cityName = aMapLocation.getCity();
if (cityName.endsWith("市")) {
cityName = cityName.substring(0, cityName.length() - 1);
}
provinceName = aMapLocation.getProvince();
if (provinceName.endsWith("省") || provinceName.endsWith("市")) {
provinceName = provinceName.substring(0, provinceName.length() - 1);
}
// if (cityName.endsWith("市")) {
// cityName = cityName.substring(0, cityName.length() - 1);
// }
// if (provinceName.endsWith("省") || provinceName.endsWith("市")) {
// provinceName = provinceName.substring(0, provinceName.length() - 1);
// }
getCityWeather(provinceName, cityName, longitude, latitude);
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/item_weather_later.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_root_weather_forecast"
android:background="#66000000"
android:orientation="vertical">

Expand Down

0 comments on commit c9cace3

Please sign in to comment.