【版权说明】
1. 请支持原创,转载请注明出处:https://blog.csdn.net/cbk861110/article/details/86665564
2. 项目源码请移步:https://github.com/caobaokang419/WeatherApp(欢迎Github Fork&Star,框架和技术实现不妥之处,请帮忙指正),谢谢!
-------------------------------------------
基于MVVM框架的高德天气APP:
功能点实现说明:
- 左右滑动实现:ViewPager实现左右页面滑动切换;
一、 实现方式选型:
ViewPager+PagerAdapter
备注:弃用方案:ViewPager+FragmentPagerAdapter,不适合此场景(View一致,data不同,count动态)
二、 代码实现:
1. WtMainActivity.java 相关实现:
private ViewPager mViewPager;
private CityPagerAdapter mCityPagerAdapter;
private PageIndicatorView mPageIndicatorView;
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mCityPagerAdapter = new CityPagerAdapter(this, mViewModel);
mViewPager.setAdapter(mCityPagerAdapter);
mViewModel.registerPageChangeListener(mViewPager);
mPageIndicatorView = findViewById(R.id.page_indicatior);
mPageIndicatorView.registerPageChangeListener(mViewPager);
2. CityPagerAdapter.java Pager页面实现:
public class CityPagerAdapter extends PagerAdapter {
private Context mContext;
private List<CityBean> mCityBeans;
private Map<CityBean, View> mViewDatas = new HashMap<>();
private LayoutInflater mLayoutInflater;
private MainActivityViewModel mViewModel;
public CityPagerAdapter(Context context, MainActivityViewModel viewmodel) {
this.mContext = context;
this.mViewModel = viewmodel;
this.mCityBeans = viewmodel.getCityInfoList();
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mCityBeans.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
//return false;
return view == object;
}
@Override
public View instantiateItem(ViewGroup container, int position) {
//demo code
/*TextView textView = new TextView(mContext);
textView.setText(mCityBeans.get(position).adrName);
container.addView(textView);
return textView;*/
View createdView = getViewItemByCity(mCityBeans.get(position));
if (createdView == null) {
View newView = mLayoutInflater.inflate(R.layout.item_city_pager_view, container,false);//无false参数报错
RecyclerView recyclerView = newView.findViewById(R.id.recycler_view);
recyclerView.setAdapter(mViewModel.getCityWeatherRecyclerAdapter(mCityBeans.get(position)));
recyclerView.setLayoutManager(new LinearLayoutManager(mContext));//必须添加此项,否则无法显示
createdView = newView;
}
container.addView(createdView);
return createdView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
container.removeView(view);
}
public View getViewItemByCity(CityBean cityinfo) {
if (mViewDatas == null) {
return null;
}
return mViewDatas.get(cityinfo);
}
}
3. activity_weather_main.xml 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activity.WtMainActivity">
<com.example.commonui.ActionBar xmlns:actionbar="http://schemas.android.com/apk/res-auto"
android:id="@+id/action_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/actionbar_height"
android:background="@color/actionbar_background"
android:gravity="center_vertical"
actionbar:iv_left_icon="@drawable/ic_action_bar_left"
actionbar:iv_right_icon="@drawable/ic_action_bar_right"
actionbar:tv_title_str="深圳" />
<com.example.commonui.PageIndicatorView
android:id="@+id/page_indicatior"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_below="@+id/action_bar"
android:layout_centerHorizontal="true">
</com.example.commonui.PageIndicatorView>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_below="@+id/page_indicatior"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
4. item_city_pager_view.xml 单个页面的具体实现:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/action_bar" />
</LinearLayout>
-------------------------------------------
文章目录(未完,待续):
一:android 应用子功能及移动框架总述 https://blog.csdn.net/cbk861110/article/details/86665564
二:android 高德天气API说明及城市天气查询实现 https://blog.csdn.net/cbk861110/article/details/86665655
三:android 自定义控件实现(ActionBar + PageIndicatorView) https://blog.csdn.net/cbk861110/article/details/86665790
四:android ViewPager实现左右页面滑动切换 https://blog.csdn.net/cbk861110/article/details/86665964
五:android应用权限动态申请 https://blog.csdn.net/cbk861110/article/details/86666321
六:android RecyclerView 封装及使用 https://blog.csdn.net/cbk861110/article/details/86666392
七:android Xutils3文件下载实现(高德天气城市配置) https://blog.csdn.net/cbk861110/article/details/86666573
八:android DiskLruCache 磁盘缓存 封装和使用 https://blog.csdn.net/cbk861110/article/details/86666664
九:android ThreadPoolExecutor线程池 封装及使用 https://blog.csdn.net/cbk861110/article/details/86667101
十:android 天气网络请求框架(retrofit2&okhttp3&Gson) 封装及使用 https://blog.csdn.net/cbk861110/article/details/86667375
十一:android RxAndroid(响应式编程) 异步网络请求实现 https://blog.csdn.net/cbk861110/article/details/86669178
十二:android DataBinding 数据和UI双向绑定实现 https://blog.csdn.net/cbk861110/article/details/86669708
十三:android room数据库 天气数据读写实现 https://blog.csdn.net/cbk861110/article/details/86670354
十四:android LiveData 使用方法(实现城市天气自动刷新) https://blog.csdn.net/cbk861110/article/details/86670531
十五:android ViewModel 使用方法 https://blog.csdn.net/cbk861110/article/details/86670703
十六:android 集成友盟消息推送机制(U-Push) https://blog.csdn.net/cbk861110/article/details/86683849
-------------------------------------------
【版权说明】
1. 请支持原创,转载请注明出处:https://blog.csdn.net/cbk861110/article/details/86665564
2. 项目源码请移步:https://github.com/caobaokang419/WeatherApp(欢迎Github Fork&Star,框架和技术实现不妥之处,请帮忙指正),谢谢!