一个小综合的项目
2018-01-15 10:18
288 查看
首先是主页面布局文件<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.animee.test.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabSelectedTextColor="@color/colorAccent"
app:tabIndicatorColor="@color/colorAccent"
app:tabIndicatorHeight="5dp">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
然后是副界面
接下来是两个item
和
public class HttpUtils {
public static String getStringContent(String path){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
InputStream is = conn.getInputStream();
byte[]buf = new byte[1024];
int hasRead = 0;
while((hasRead = is.read(buf))!=-1){
baos.write(buf,0,hasRead);
}
} catch (IOException e) {
e.printStackTrace();
}
return baos.toString();
}
}
接下来是
一个适配器
另外一个activity
Fragment
另外一个适配器
VP适配器
XML解析
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.animee.test.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabSelectedTextColor="@color/colorAccent"
app:tabIndicatorColor="@color/colorAccent"
app:tabIndicatorHeight="5dp">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
然后是副界面
<?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="wrap_content" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/header_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="近期天气情况:" android:textColor="@color/colorPrimary" android:textSize="20sp"/> <Spinner android:id="@+id/header_sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/header_tv2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="今日指数分析:" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:textColor="@color/colorPrimary" android:textSize="20sp"/> </LinearLayout>
接下来是两个item
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/lv_tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="穿衣" android:textSize="20sp" android:textColor="@color/colorAccent"/> <TextView android:id="@+id/lv_tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" 4000 android:layout_alignParentStart="true" android:layout_below="@+id/lv_tv1" android:layout_marginTop="10dp" android:text="穿衣指数" /> <TextView android:id="@+id/lv_tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/lv_tv2" android:layout_marginLeft="30dp" android:layout_marginStart="30dp" android:layout_toEndOf="@+id/lv_tv2" android:layout_toRightOf="@+id/lv_tv2" android:text="冷" /> <TextView android:id="@+id/lv_tv4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/lv_tv2" android:textColor="@color/colorPrimary" android:padding="10dp" android:background="#eee" android:layout_margin="10dp" android:text="天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。" /> </RelativeLayout>
和
<?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" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#eee" android:padding="20dp"> <TextView android:id="@+id/sp_tv_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:textSize="18sp" android:text="周五 01月12日 (实时:-3℃)" /> <TextView android:id="@+id/sp_tv_day" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/sp_tv_date" android:layout_marginTop="10dp" android:text="白天情况:晴" /> <TextView android:id="@+id/sp_tv_fengxiang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/sp_tv_day" android:layout_marginTop="10dp" android:text="西南风3-4级" /> <TextView android:id="@+id/sp_tv_wendu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/sp_tv_fengxiang" android:layout_marginLeft="30dp" android:layout_marginStart="30dp" android:layout_toEndOf="@+id/sp_tv_day" android:layout_toRightOf="@+id/sp_tv_day" android:text="温度:3 ~ -8℃" /> <ImageView android:id="@+id/sp_img_bt" android:layout_width="40dp" android:layout_height="30dp" android:layout_alignBottom="@+id/sp_tv_day" app:srcCompat="@android:drawable/btn_dialog" android:layout_alignLeft="@+id/sp_tv_wendu" android:layout_alignStart="@+id/sp_tv_wendu" /> <TextView android:id="@+id/sp_tv_night" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/sp_tv_fengxiang" android:text="夜间情况:" android:layout_marginTop="10dp"/> <ImageView android:id="@+id/sp_img_night" android:layout_width="40dp" android:layout_height="30dp" android:layout_alignBottom="@+id/sp_tv_night" android:layout_alignStart="@+id/sp_img_bt" android:layout_alignLeft="@+id/sp_img_bt" android:layout_marginTop="10dp" app:srcCompat="@android:drawable/btn_dialog" /> <ImageView android:layout_width="match_parent" android:layout_height="2dp" android:layout_below="@+id/sp_img_night" android:background="@color/colorPrimary" android:layout_marginTop="10dp"/> </RelativeLayout>一个listview布局
<!-- TODO: Update blank fragment layout --> <ListView android:id="@+id/fragment_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@color/colorPrimary" android:dividerHeight="1dp" /> </LinearLayout>然后是网络获取数据http
public class HttpUtils {
public static String getStringContent(String path){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
InputStream is = conn.getInputStream();
byte[]buf = new byte[1024];
int hasRead = 0;
while((hasRead = is.read(buf))!=-1){
baos.write(buf,0,hasRead);
}
} catch (IOException e) {
e.printStackTrace();
}
return baos.toString();
}
}
接下来是
public class MainActivity extends AppCompatActivity { private TabLayout tabLayout; private ViewPager vp; // 接口 public String bjUrl = "http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=xml&ak=mXBIDrvTOwwmYaTtN03Lo0j2"; public String syUrl = "http://api.map.baidu.com/telematics/v3/weather?location=%E6%B2%88%E9%98%B3&output=xml&ak=mXBIDrvTOwwmYaTtN03Lo0j2"; public String hzUrl = "http://api.map.baidu.com/telematics/v3/weather?location=%E4%B8%8A%E6%B5%B7&output=xml&ak=mXBIDrvTOwwmYaTtN03Lo0j2"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { tabLayout = (TabLayout) findViewById(R.id.tablayout); vp = (ViewPager) findViewById(R.id.vp); initPager(); } private void initPager() { List<Fragment>fragments = new ArrayList<>(); String url[] = {bjUrl,syUrl,hzUrl}; List<String>titleList = new ArrayList<>(); titleList.add("北京"); titleList.add("沈阳"); titleList.add("杭州"); for (int i = 0; i < 3; i++) { Fragment frag = new WeatherFragment(); Bundle bundle = new Bundle(); bundle.putString("url",url[i]); frag.setArguments(bundle); fragments.add(frag); } VPAdapter adapter = new VPAdapter(getSupportFragmentManager(), fragments, titleList); vp.setAdapter(adapter); // 使TabLayout和ViewPager产生关联 tabLayout.setupWithViewPager(vp); } }
一个适配器
public class ZhishuAdapter extends BaseAdapter { private Context context; private List<WeatherBean.Zhishu>mDatas; public ZhishuAdapter(Context context, List<WeatherBean.Zhishu> mDatas) { this.context = context; this.mDatas = mDatas; } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ZhiShuViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_lv,null); holder = new ZhiShuViewHolder(convertView); convertView.setTag(holder); }else{ holder = (ZhiShuViewHolder) convertView.getTag(); } WeatherBean.Zhishu zhishu = mDatas.get(position); holder.titletTv.setText(zhishu.getTitle()); holder.tiptTv.setText(zhishu.getTipt()); holder.zsTv.setText(zhishu.getZs()); holder.detailTv.setText(zhishu.getDes()); return convertView; } class ZhiShuViewHolder{ TextView titletTv,zsTv,tiptTv,detailTv; public ZhiShuViewHolder(View view){ titletTv = (TextView) view.findViewById(R.id.lv_tv1); zsTv = (TextView) view.findViewById(R.id.lv_tv3); tiptTv = (TextView) view.findViewById(R.id.lv_tv2); detailTv = (TextView) view.findViewById(R.id.lv_tv4); } } }
另外一个activity
public class DayActivity extends AppCompatActivity { public String imgUrl = "http://img.my.csdn.net/uploads/201407/26/1406383299_1976.jpg"; private ImageView iv; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_day); iv = (ImageView) findViewById(R.id.iv); context = this; } public void onClick(View view) { new AsyncTask<String,Void,Bitmap>(){ @Override protected Bitmap doInBackground(String... params) { byte[] bytes = HttpUtils.getByteArray(params[0]); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); // 保存bitmap到文件当中 /sdcard/Android/data/您的包名/cache/ File file = new File(context.getExternalCacheDir(), "1976.jpg"); try { FileOutputStream fos = new FileOutputStream(file); fos.write(bytes); fos.flush(); fos.close(); }catch (IOException e) { e.printStackTrace(); } return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { iv.setImageBitmap(bitmap); } }.execute(imgUrl); } }
Fragment
public class WeatherFragment extends Fragment { private String url; private ListView lv; private Spinner sp; private List<WeatherBean.WeatherDate> weatherList; private List<WeatherBean.Zhishu> zhishuList; private ZhishuAdapter zhishuAdapter; private WeatherAdapter weatherAdapter; private String TAG = "WeatherFragment"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_weather, container, false); Bundle bundle = getArguments(); url = bundle.getString("url"); initView(view); return view; } private void initView(View view) { lv = (ListView)view.findViewById(R.id.fragment_lv); View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_layout,null); lv.addHeaderView(headerView); sp = (Spinner) headerView.findViewById(R.id.header_sp); // 数据源: weatherList = new ArrayList<>(); zhishuList = new ArrayList<>(); zhishuAdapter = new ZhishuAdapter(getActivity(),zhishuList); lv.setAdapter(zhishuAdapter); weatherAdapter = new WeatherAdapter(getActivity(),weatherList); sp.setAdapter(weatherAdapter); loadData(url); } private void loadData(String url) { new AsyncTask<String,Void,String>(){ @Override protected String doInBackground(String... params) { String content = HttpUtils.getStringContent(params[0]); return content; } @Override protected void onPostExecute(String s) { // 判断数据,解析数据 if (s!=null&&!s.isEmpty()) { WeatherBean bean = ParseXML.readXML(s); Log.i(TAG, "onPostExecute:Weather_dateList().size =="+bean.getWeather_dateList().size()); weatherList.addAll(bean.getWeather_dateList()); weatherAdapter.notifyDataSetChanged(); Log.i(TAG, "onPostExecute: ZhishuList().size====="+bean.getZhishuList().size()); zhishuList.addAll(bean.getZhishuList()); zhishuAdapter.notifyDataSetChanged(); } } }.execute(url); } }实体类
public class WeatherBean { private List<WeatherDate>weather_dateList; private List<Zhishu>zhishuList; public WeatherBean() { } public List<WeatherDate> getWeather_dateList() { return weather_dateList; } public void setWeather_dateList(List<WeatherDate> weather_dateList) { this.weather_dateList = weather_dateList; } public List<Zhishu> getZhishuList() { return zhishuList; } public void setZhishuList(List<Zhishu> zhishuList) { this.zhishuList = zhishuList; } @Override public String toString() { return "WeatherBean{" + "weather_dateList=" + weather_dateList + ", zhishuList=" + zhishuList + '}'; } public WeatherBean(List<WeatherDate> weather_dateList, List<Zhishu> zhishuList) { this.weather_dateList = weather_dateList; this.zhishuList = zhishuList; } public static class Zhishu{ private String title; private String zs; private String tipt; private String des; @Override public String toString() { return "Zhishu{" + "title='" + title + '\'' + ", zs='" + zs + '\'' + ", tipt='" + tipt + '\'' + ", des='" + des + '\'' + '}'; } public String getTitle() { return title; } public String getZs() { return zs; } public String getTipt() { return tipt; } public String getDes() { return des; } public Zhishu() { } public void setTitle(String title) { this.title = title; } public void setZs(String zs) { this.zs = zs; } public void setTipt(String tipt) { this.tipt = tipt; } public void setDes(String des) { this.des = des; } public Zhishu(String title, String zs, String tipt, String des) { this.title = title; this.zs = zs; this.tipt = tipt; this.des = des; } } public static class WeatherDate{ private String date; private String dayPictureUrl; private String nightPictureUrl; private String weather; private String wind; private String temperature; @Override public String toString() { return "WeatherDate{" + "date='" + date + '\'' + ", dayPictureUrl='" + dayPictureUrl + '\'' + ", nightPictureUrl='" + nightPictureUrl + '\'' + ", weather='" + weather + '\'' + ", wind='" + wind + '\'' + ", temperature='" + temperature + '\'' + '}'; } public WeatherDate() { } public WeatherDate(String date, String dayPictureUrl, String nightPictureUrl, String weather, String wind, String temperature) { this.date = date; this.dayPictureUrl = dayPictureUrl; this.nightPictureUrl = nightPictureUrl; this.weather = weather; this.wind = wind; this.temperature = temperature; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } b692 public String getDayPictureUrl() { return dayPictureUrl; } public void setDayPictureUrl(String dayPictureUrl) { this.dayPictureUrl = dayPictureUrl; } public String getNightPictureUrl() { return nightPictureUrl; } public void setNightPictureUrl(String nightPictureUrl) { this.nightPictureUrl = nightPictureUrl; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } } }
另外一个适配器
public class WeatherAdapter extends BaseAdapter{ private Context context; private List<WeatherBean.WeatherDate>mDatas; public WeatherAdapter(Context context, List<WeatherBean.WeatherDate> mDatas) { this.context = context; this.mDatas = mDatas; } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { WeatherViewHolder holder = null; if (convertView==null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_sp,null); holder = new WeatherViewHolder(convertView); convertView.setTag(holder); }else{ holder = (WeatherViewHolder) convertView.getTag(); } WeatherBean.WeatherDate weatherDate = mDatas.get(position); holder.wenduTv.setText("温度:"+weatherDate.getTemperature()); holder.dateTv.setText(weatherDate.getDate()); holder.windTv.setText(weatherDate.getWind()); holder.weaherTv.setText("白天情况:"+weatherDate.getWeather()); Picasso.with(context).load(weatherDate.getDayPictureUrl()).into(holder.dayIv); Picasso.with(context).load(weatherDate.getNightPictureUrl()).into(holder.nightTv); return convertView; } class WeatherViewHolder{ TextView dateTv,wenduTv,windTv,weaherTv; ImageView dayIv,nightTv; public WeatherViewHolder(View view){ dateTv = (TextView) view.findViewById(R.id.sp_tv_date); wenduTv = (TextView) view.findViewById(R.id.sp_tv_wendu); windTv = (TextView) view.findViewById(R.id.sp_tv_fengxiang); weaherTv = (TextView) view.findViewById(R.id.sp_tv_day); dayIv = (ImageView) view.findViewById(R.id.sp_img_bt); nightTv = (ImageView) view.findViewById(R.id.sp_img_night); } } }
VP适配器
public class VPAdapter extends FragmentPagerAdapter{ private List<Fragment>mDatas; private List<String>mTitleList; public VPAdapter(FragmentManager fm,List<Fragment>mDatas,List<String>mTitleList) { super(fm); this.mDatas = mDatas; this.mTitleList = mTitleList; } @Override public Fragment getItem(int position) { return mDatas.get(position); } @Override public int getCount() { return mDatas.size(); } @Override public CharSequence getPageTitle(int position) { return mTitleList.get(position); } }
XML解析
public class ParseXML { public static String TAG = "ParseXML"; public static WeatherBean readXML(String content){ WeatherBean bean = new WeatherBean(); List<WeatherBean.WeatherDate> weatherList = new ArrayList<>(); List<WeatherBean.Zhishu> zhishuList = new ArrayList<>(); bean.setWeather_dateList(weatherList); bean.setZhishuList(zhishuList); // 开始解析 try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser pullParser = factory.newPullParser(); ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes()); pullParser.setInput(bais,"utf-8"); int code = pullParser.getEventType(); WeatherBean.WeatherDate weatherDate = null; WeatherBean.Zhishu zhishu = null; while (code != XmlPullParser.END_DOCUMENT) { String name = pullParser.getName(); Log.i(TAG, "readXML: ==name==="+name); switch (code) { case XmlPullParser.START_TAG: if (name.equals("date")) { Log.i(TAG, "readXML: ==date=创建对象了="); weatherDate = new WeatherBean.WeatherDate(); weatherDate.setDate(pullParser.nextText()); }else if (name.equals("dayPictureUrl")) { weatherDate.setDayPictureUrl(pullParser.nextText()); }else if (name.equals("nightPictureUrl")) { weatherDate.setNightPictureUrl(pullParser.nextText()); }else if (name.equals("weather")) { weatherDate.setWeather(pullParser.nextText()); }else if (name.equals("wind")) { weatherDate.setWind(pullParser.nextText()); }else if (name.equals("temperature")) { weatherDate.setTemperature(pullParser.nextText()); weatherList.add(weatherDate); }else if (name.equals("title")) { zhishu = new WeatherBean.Zhishu(); zhishu.setTitle(pullParser.nextText()); }else if (name.equals("zs")) { zhishu.setZs(pullParser.nextText()); }else if (name.equals("tipt")) { zhishu.setTipt(pullParser.nextText()); }else if (name.equals("des")){ zhishu.setDes(pullParser.nextText()); zhishuList.add(zhishu); } break; case XmlPullParser.END_TAG: if (name.equals("temperature")) { Log.i(TAG, "readXML: ==添加到集合了=="); //没执行到? 没找到原因,但是换一种写法 // weatherList.add(weatherDate); }else if (name.equals("des")) { // zhishuList.add(zhishu); } break; } code = pullParser.next(); } } catch (XmlPullParserException e) { Log.i(TAG, "readXML: ====XmlPullParserException==="); e.printStackTrace(); } catch (IOException e) { Log.i(TAG, "readXML: =====IOException====="); e.printStackTrace(); } Log.i(TAG, "readXML: ===="+bean); return bean; } }
相关文章推荐
- 综合应用WPF/WCF/WF/LINQ之三十四:在XBAP项目中模拟弹出一个自定义的“模态窗口”
- 综合应用:一个《购票系统》项目需求分析
- 开放思路,综合考虑,心胸开阔,做一个合格的项目经理
- 一个历时3年的综合运维监控项目所带来的感想
- 一个比较综合的项目--》>图片缓存,下拉刷新等
- 续Gulp使用入门-综合运用>使用Gulp构建一个项目
- 用Maven创建一个Web项目
- 一个项目经理的个人体会、经验总结
- 谷歌开源可视化工具Facets,将用于人+AI协作项目研究——无非就是一个用于特征工程探索的绘图工具集,pandas可以做的
- 一个项目涉及到的50个Sql语句
- 一个web项目的目录结构示例
- 一个想法照进现实-《IT连》创业项目:创业时该不该用新手程序员
- SpringMvc+MyBatis+MySQL+Maven+Redis搭建一个基础的web项目
- 一个项目涉及到的50个Sql语句
- 使用eclipse构建Maven项目及发布一个Maven项目
- springmvc与+hibernate制作的一个聊天室(开源项目)
- 教育类项目的一个功能架构
- 在一个项目中管理好基础架构和开发团队
- 同一个项目的不同的项目工作经验总结--程序员丙
- 一个项目经理的个人体会:最忌讳的就是完美主义倾向