Android阶段学习总结 7.25-7.29 天气预报 知识点
来源:互联网 发布:js 设置style display 编辑:程序博客网 时间:2024/05/18 03:43
总布局:一个Activity跳转到一个FragmentActivity中,并传递Json,FragmentActivity通过Fragment实现tab,并把Json传给每一个Fragment
一、MainActivity作为主界面,用AutoCompleteTextView选择地区,,提交地点,在按钮监听事件中,拼接好查找json路径,开子线程从网上得到json,在主线程中,通过Activity跳转,给Main传一个带Json的JsonParser对象
Handler handler = new Handler() { @Override public void handleMessage(Message msg) { parser.setJson((String)msg.obj); Context context = MainActivity.this; Intent intent = new Intent(MainActivity.this, Main.class); intent.putExtra("parser", parser); startActivity(intent); } };
JsonParser定义如下,他是序列化的,才能通过Intent传输
public class JsonParser implements Serializable{ Context context; String json; public void setJson(String json) { this.json = json; } public String getJson() { return json; }//省略每一个JSONObject的Json解析方法,在上一博文中有具体步骤}
二、FragmentActivity: Main 接受JsonParser对象
设置get set 方法
public JsonParser getParser() { return parser; }public void setParser(JsonParser parser) { this.parser = parser; }
Intent intent = getIntent(); JsonParser parser = (JsonParser)intent.getSerializableExtra("parser"); this.setParser(parser);
二、动态加载Fragment实现每一页的Tab
FragmentActivity关联的xml布局如下
<?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="match_parent" android:background="@mipmap/back" android:orientation="vertical"> <include layout="@layout/top" /> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="center" android:layout_weight="1"></FrameLayout> <include layout="@layout/bottom" /></LinearLayout>
写每一个Fragment 关联的xml布局,并定义Fragment,以一个为例,此Fragment中实现了未来7天的ListView
activity_recently.xml
<?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="match_parent" android:orientation="vertical"> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent"></ListView></LinearLayout>
设置的每一个item的布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff"> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:orientation="vertical" android:layout_height="100dp"> <TextView android:id="@+id/tv_recent_date" android:background="#66ffffff" android:text="29°" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_recent_week" android:background="#66ffffff" android:text="33°" android:textSize="10sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <ImageView android:id="@+id/iv_recent_weather" android:layout_weight="1" android:layout_width="0dp" android:layout_height="100dp" android:src="@mipmap/rain"/> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:orientation="vertical" android:layout_height="100dp"> <TextView android:id="@+id/tv_recent_temp" android:background="#66ffffff" android:text="29°" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_recent_wind" android:background="#66ffffff" android:text="33°" android:textSize="10sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>
Fragment定义必须的语句
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_recently, container, false); return view; }
通过下面的语句拿到有json的Parser对象
Main activity = (Main)getActivity(); parser = activity.getParser();
FragmentFuture如下:
public class FragmentFuture extends ListFragment { private JsonParser parser; private RecentAdapter recentAdapter; private Context context; public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_recently, container, false); return view; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Main activity = (Main)getActivity(); parser = activity.getParser(); context = activity; List<Bean.ResultBean.FutureBean> futureList = parser.getFutureList(parser.getJson()); recentAdapter = new RecentAdapter(futureList,context); setListAdapter(recentAdapter); }}
RecentAdapter
public class RecentAdapter extends BaseAdapter { private List<Bean.ResultBean.FutureBean> list; private Context context; //触发布局 private LayoutInflater inflater; public RecentAdapter(List<Bean.ResultBean.FutureBean> list, Context context) { this.list = list; this.context = context; inflater=LayoutInflater.from(context); } public RecentAdapter(Context context) { this.context = context; inflater=LayoutInflater.from(context); } public void setData(List<Bean.ResultBean.FutureBean> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder = null; if (convertView == null) { convertView = inflater.inflate(R.layout.item_recently,null); holder = new Holder(convertView); convertView.setTag(holder); } else { holder = (Holder)convertView.getTag(); } Bean.ResultBean.FutureBean futureBean = list.get(position); holder.tv_recent_date.setText(""+futureBean.getDate()); holder.tv_recent_week.setText(futureBean.getWeek()); holder.tv_recent_temp.setText(futureBean.getTemperature()); holder.tv_recent_wind.setText(futureBean.getWind()); return convertView; } class Holder { private TextView tv_recent_date; private TextView tv_recent_week; private ImageView iv_recent_weather; private TextView tv_recent_temp; private TextView tv_recent_wind; public Holder(View v) { tv_recent_date = (TextView)v.findViewById(R.id.tv_recent_date); tv_recent_week = (TextView)v.findViewById(R.id.tv_recent_week); iv_recent_weather = (ImageView)v.findViewById(R.id.iv_recent_weather); tv_recent_temp = (TextView)v.findViewById(R.id.tv_recent_temp); tv_recent_wind = (TextView)v.findViewById(R.id.tv_recent_wind); } }}
Main 中对Fragment进行设置
package cn.edu.hpu.xiaoqian.weather2;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.view.Window;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.Toast;/** * Created by qianxiao on 2016/7/28. */public class Main extends FragmentActivity implements View.OnClickListener { private JsonParser parser; private LinearLayout tab1; private LinearLayout tab2; private LinearLayout tab3; private LinearLayout tab4; private ImageView img1; private ImageView img2; private ImageView img3; private ImageView img4; private Fragment frag1; private Fragment frag2; private Fragment frag3; private Fragment frag4; public JsonParser getParser() { return parser; } public void setParser(JsonParser parser) { this.parser = parser; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); Intent intent = getIntent(); JsonParser parser = (JsonParser)intent.getSerializableExtra("parser"); this.setParser(parser); initView(); initEvent(); setSelect(1); } private void initView() { tab1 = (LinearLayout) findViewById(R.id.tab1); tab2 = (LinearLayout) findViewById(R.id.tab2); tab3 = (LinearLayout) findViewById(R.id.tab3); tab4 = (LinearLayout) findViewById(R.id.tab4); img1 = (ImageView) findViewById(R.id.iv_1); img2 = (ImageView) findViewById(R.id.iv_2); img3 = (ImageView) findViewById(R.id.iv_3); img4 = (ImageView) findViewById(R.id.iv_4); } private void initEvent() { tab1.setOnClickListener(this); tab2.setOnClickListener(this); tab3.setOnClickListener(this); tab4.setOnClickListener(this); } private void setSelect(int i) { FragmentManager fm = getSupportFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); hideFragment(transaction); //图片设置 switch (i) { case 0: if(frag1 == null ) { frag1 = new FragmentIndex(); transaction.add(R.id.content,frag1); } else { transaction.show(frag1); // transaction.add(R.id.content, frag1); } break; case 1: if(frag2 == null ) { frag2 = new FragmentWeather(); transaction.add(R.id.content,frag2); } else { transaction.show(frag2); // transaction.add(R.id.content, frag1); } break; case 2: if(frag3 == null ) { frag3 = new FragmentFuture(); transaction.add(R.id.content,frag3); } else { transaction.show(frag3); // transaction.add(R.id.content, frag1); } break; case 3: if(frag4 == null ) { frag4 = new FragmentSetting(); transaction.add(R.id.content,frag4); } else { transaction.show(frag4); // transaction.add(R.id.content, frag1); } break; } transaction.commit(); } private void hideFragment(FragmentTransaction transaction) { if(frag1 != null) { transaction.hide(frag1); } if(frag2 != null) { transaction.hide(frag2); } if(frag3 != null) { transaction.hide(frag3); } if(frag4 != null) { transaction.hide(frag4); } } @Override public void onClick(View v) { //resetImage switch (v.getId()) { case R.id.tab1: setSelect(0); Toast.makeText(Main.this, "点击一", Toast.LENGTH_SHORT).show(); break; case R.id.tab2: setSelect(1); break; case R.id.tab3: setSelect(2); break; case R.id.tab4: setSelect(3); break; } }}
界面实现情况:
把这阶段的学习成果运用到了实际开发,项目虽小,但是很开心,很充实,我的成绩也被学长认可,很有收获。
学前端的同学29号用H5做了app,感觉很羡慕,也让我开阔了眼界,混合移动开发可能比较有发展空间,学好Android原生开发以后,好好学一下。不足:Json串中,天气情况不能反应在图片上,界面较简单
0 0
- Android阶段学习总结 7.25-7.29 天气预报 知识点
- Android阶段学习笔记 7.25-7.29 之 解析聚合数据 全国天气预报 Json
- Objective-C学习阶段的知识点总结
- Objective-C学习阶段的知识点总结
- mysql知识点阶段总结
- android学习:知识点总结
- 【android学习】android知识点总结
- 天气预报项目学习总结
- 天气预报项目学习总结
- 学习android之 知识点总结
- android学习天气预报系统
- STL学习阶段总结
- 近阶段学习总结
- 阶段学习总结
- 阶段学习总结
- 学习阶段总结
- linux学习阶段总结
- 内核阶段学习总结
- Linux多进程多线程互斥同步例子
- C#扩展(2):Random的扩展
- js拼接json字符串
- Android ImageSpan与TextView中的text居中对齐问题解决(无论TextView设置行距与否)
- content = "IE=edge,chrome=1" 详解
- Android阶段学习总结 7.25-7.29 天气预报 知识点
- VC 自绘制控件
- 深度可视化语义表述和图像描述实验
- 用BroadCast来退出应用
- 关于并发访问相同servlet的问题
- iOS应用程序5种常见的存储方式
- HDU 5774 (Where Amazing Happens 简单计算)
- (4.6.17.6)进程保活(Android的5.0分界线):Android5.0以上版本的force close到底发生了什么改变?
- A - Oil Deposits