一个小综合的项目

首先是主页面布局文件

<?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>
然后是副界面
<?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"
        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 = "https://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;
        }

        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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值