专属空间十——历史上的今天代码
今天来将代码展示出来,以供参考,整个项目信息我会放在文件中名为Today2.rar 。
下面图片是整体项目结构和需要注意的地方。
还有大家如果需要的话不用写TestActivity和他的xml文件。这是因为我要将这个功能加入到一个总项目中的,所以在尾部局
多加了一个button用来返回到总项目的主界面。相关代码可以不需要如下:
整个项目之中只有MainActivity、HistoryActivity、HistoryDescribleActivity是创建的Activity其他都是class java
那么对应的布局除外其他都是自己创建的辅助布局。
base包中的:
package com.example.today.base;
import androidx.appcompat.app.AppCompatActivity;
import org.xutils.common.Callback;import org.xutils.http.RequestParams;import org.xutils.x;
public class BaseActivity extends AppCompatActivity implements Callback.CommonCallback<String>{
public void LoadData(String url){
RequestParams params = new RequestParams(url);
x.http().get(params, this);
}
@Override
public void onSuccess(String result) {
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
}
package com.example.today.base;
public class ContentURL {
// 获取指定日期对应的历史上的今天的网址
public static String getTodayHistoryURL(String version, int month, int day) {// String todayHistoryURL = "http://api.juheapi.com/japi/toh?key=6a877b186ccd134296d131183b74c9f4&v="+version+"&month="+month+"&day="+day;
String todayHistoryURL = "http://api.juheapi.com/japi/toh?key=82f75e4db269378bcf93f896eac7d18b&v=" + version + "&month=" + month + "&day=" + day;
return todayHistoryURL;
}
// 获取指定日期老黄历的网址
public static String getLaohuangliURL(String time) {
String url = "http://v.juhe.cn/laohuangli/d?date=" + time + "&key=24c842ba5d3136fe575b024f1fdcf2c9";
return url;
}
// 根据事件id获取到指定事件详情信息
public static String getHistoryDescURL(String version, String id) {// String url = "http://api.juheapi.com/japi/tohdet?key=6a877b186ccd134296d131183b74c9f4&v="+version+"&id="+id;
String url = "http://api.juheapi.com/japi/tohdet?key=82f75e4db269378bcf93f896eac7d18b&v=" + version + "&id=" + id;
return url;
}
}
package com.example.today.base;
import android.app.Application;
import org.xutils.x;
public class UniteApp extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
}
}
bean包中的:
package com.example.today.bean;
import java.io.Serializable;import java.util.List;
public class HistoryBean implements Serializable {
* error_code : 0
*/
private String reason;
private int error_code;
private List<ResultBean> result;
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean implements Serializable {
/**
* _id : 17161114
* title : 德国数学家莱布尼茨逝世
* pic : http://juheimg.oss-cn-hangzhou.aliyuncs.com/toh/201111/14/B6125711808.jpg
* year : 1716
* month : 11
* day : 14
* des : 在304年前的今天,1716年11月14日 (农历十月初一),德国数学家莱布尼茨逝世。
* lunar : 丙申年十月初一
*/
private String _id;
private String title;
private String pic;
private int year;
private int month;
private int day;
private String des;
private String lunar;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public String getLunar() {
return lunar;
}
public void setLunar(String lunar) {
this.lunar = lunar;
}
}
}
package com.example.today.bean;
import java.util.List;
public class HistoryDescribeBean {
/**
* result : [{"_id":"16301115","title":"德国天文学家开普勒逝世","pic":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/toh/201111/15/1E12445126.jpg","year":1630,"month":11,"day":15,"des":"在390年前的今天,1630年11月15日 (农历十月十二),德国天文学家开普勒逝世。","content":"在390年前的今天,1630年11月15日 (农历十月十二),德国天文学家开普勒逝世。\n开普勒(1571~1630.11.15),德国天文学家。1587年进入蒂宾根大学,开始研究哥白尼的天文学。其主要成就是奠定了天体力学的基础。他分析第谷·布拉赫多年积累的观测资料,提出了行星运动三定律(开普勒定律):第一定律为椭圆轨道定律\u2014\u2014行星轨道呈椭圆形,太阳在一个焦点上;第二定律为相等面积定律\u2014\u2014在相等的时间内,行星和太阳联线所扫过的面积相等;第三定律为调和定律\u2014\u2014任何两行星公转周期的平方同轨道半长径的立方成正比。这为牛顿发现万有引力定律打下了基础。\n","lunar":"庚午年十月十二"}]
* reason : 请求成功!
* error_code : 0
*/
private String reason;
private int error_code;
private List<ResultBean> result;
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean {
/**
* _id : 16301115
* title : 德国天文学家开普勒逝世
* pic : http://juheimg.oss-cn-hangzhou.aliyuncs.com/toh/201111/15/1E12445126.jpg
* year : 1630
* month : 11
* day : 15
* des : 在390年前的今天,1630年11月15日 (农历十月十二),德国天文学家开普勒逝世。
* content : 在390年前的今天,1630年11月15日 (农历十月十二),德国天文学家开普勒逝世。
开普勒(1571~1630.11.15),德国天文学家。1587年进入蒂宾根大学,开始研究哥白尼的天文学。其主要成就是奠定了天体力学的基础。他分析第谷·布拉赫多年积累的观测资料,提出了行星运动三定律(开普勒定律):第一定律为椭圆轨道定律——行星轨道呈椭圆形,太阳在一个焦点上;第二定律为相等面积定律——在相等的时间内,行星和太阳联线所扫过的面积相等;第三定律为调和定律——任何两行星公转周期的平方同轨道半长径的立方成正比。这为牛顿发现万有引力定律打下了基础。
* lunar : 庚午年十月十二
*/
private String _id;
private String title;
private String pic;
private int year;
private int month;
private int day;
private String des;
private String content;
private String lunar;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getLunar() {
return lunar;
}
public void setLunar(String lunar) {
this.lunar = lunar;
}
}
}
package com.example.today.bean;
public class LaoHuangLiBean {
/**
* reason : successed
* result : {"id":"3494","yangli":"2019-11-14","yinli":"己亥(猪)年十月十八","wuxing":"大溪水 定执位","chongsha":"冲鸡(己酉)煞西","baiji":"乙不栽植千株不长 卯不穿井水泉不香","jishen":"时德 天德 月德 四相 阴德 民日 三合 时阴 鸣犬","yi":"订盟 纳采 纳财 开市 立券 祭祀 祈福 移徙 入宅 出行 盖屋 起基 修造 动土 竖柱 上梁 安门 安香 出火 教牛马 会亲友 破土","xiongshen":"元武","ji":"嫁娶 安葬 掘井 置产 造船"}
* error_code : 0
*/
private String reason;
private ResultBean result;
private int error_code;
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public static class ResultBean {
/**
* id : 3494
* yangli : 2019-11-14
* yinli : 己亥(猪)年十月十八
* wuxing : 大溪水 定执位
* chongsha : 冲鸡(己酉)煞西
* baiji : 乙不栽植千株不长 卯不穿井水泉不香
* jishen : 时德 天德 月德 四相 阴德 民日 三合 时阴 鸣犬
* yi : 订盟 纳采 纳财 开市 立券 祭祀 祈福 移徙 入宅 出行 盖屋 起基 修造 动土 竖柱 上梁 安门 安香 出火 教牛马 会亲友 破土
* xiongshen : 元武
* ji : 嫁娶 安葬 掘井 置产 造船
*/
private String id;
private String yangli;
private String yinli;
private String wuxing;
private String chongsha;
private String baiji;
private String jishen;
private String yi;
private String xiongshen;
private String ji;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getYangli() {
return yangli;
}
public void setYangli(String yangli) {
this.yangli = yangli;
}
public String getYinli() {
return yinli;
}
public void setYinli(String yinli) {
this.yinli = yinli;
}
public String getWuxing() {
return wuxing;
}
public void setWuxing(String wuxing) {
this.wuxing = wuxing;
}
public String getChongsha() {
return chongsha;
}
public void setChongsha(String chongsha) {
this.chongsha = chongsha;
}
public String getBaiji() {
return baiji;
}
public void setBaiji(String baiji) {
this.baiji = baiji;
}
public String getJishen() {
return jishen;
}
public void setJishen(String jishen) {
this.jishen = jishen;
}
public String getYi() {
return yi;
}
public void setYi(String yi) {
this.yi = yi;
}
public String getXiongshen() {
return xiongshen;
}
public void setXiongshen(String xiongshen) {
this.xiongshen = xiongshen;
}
public String getJi() {
return ji;
}
public void setJi(String ji) {
this.ji = ji;
}
}
}
HistoryActivity.java
package com.example.today;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;
import com.example.today.bean.HistoryBean;
import java.util.ArrayList;import java.util.List;
public class HistoryActivity extends AppCompatActivity implements View.OnClickListener{
private TextView tvEmpty;
private ListView historyLv;
private ImageView imgBack;
private List<HistoryBean.ResultBean> datas;
private HistoryAdapter adapter;
private HistoryBean historyBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
tvEmpty = findViewById(R.id.history_tvEmpty);
historyLv = findViewById(R.id.historyLv);
imgBack = findViewById(R.id.history_imgBack);
imgBack.setOnClickListener(this);
datas = new ArrayList<>();
adapter = new HistoryAdapter(this, datas);
historyLv.setAdapter(adapter);
try {
Intent intent = getIntent();
//获取intent额外的数据
Bundle bundle = intent.getExtras();
historyBean = (HistoryBean) bundle.getSerializable("history");
List<HistoryBean.ResultBean> list = historyBean.getResult();
datas.addAll(list);
//更新数据 adapter.notifyDataSetChanged();
}catch (Exception e){
tvEmpty.setVisibility(View.VISIBLE);
}
historyLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(HistoryActivity.this,HistoryDescribeActivity.class);
HistoryBean.ResultBean resultBean = datas.get(position);
String beanId = resultBean.get_id();
intent.putExtra("historyId", beanId);
startActivity(intent);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.history_imgBack:
finish();
}
}
}
HistoryAdapter.java
package com.example.today;
import android.content.Context;import android.text.TextUtils;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;
import com.example.today.bean.HistoryBean;import com.squareup.picasso.Picasso;
import java.util.List;
public class HistoryAdapter extends BaseAdapter {
Context context;
List<HistoryBean.ResultBean> mDatas;
public HistoryAdapter(Context context, List<HistoryBean.ResultBean> 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) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_main_timeline, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
HistoryBean.ResultBean resultBean = mDatas.get(position);
//判断当前位置年份和上一个位置年份是否相同
if (position != 0) {
HistoryBean.ResultBean lastBean = mDatas.get(position-1);
if(resultBean.getYear()==lastBean.getYear()){
holder.timeLayout.setVisibility(View.GONE);
}else{
holder.timeLayout.setVisibility(View.VISIBLE);
}
} else {
holder.timeLayout.setVisibility(View.VISIBLE);
}
holder.tvTime.setText(resultBean.getYear()+"-"+resultBean.getMonth()+"-"+resultBean.getDay());
holder.tvTitle.setText(resultBean.getTitle());
String picURL = resultBean.getPic();
if(TextUtils.isEmpty(picURL)){
holder.imgPic.setVisibility(View.GONE);
}else{
holder.imgPic.setVisibility(View.VISIBLE);
Picasso.with(context).load(picURL).into(holder.imgPic);
}
return convertView;
}
class ViewHolder {
TextView tvTime, tvTitle;
ImageView imgPic;
LinearLayout timeLayout;
public ViewHolder(View v) {
tvTime = v.findViewById(R.id.item_main_tvTime);
tvTitle = v.findViewById(R.id.item_main_title);
imgPic = v.findViewById(R.id.item_main_pic);
timeLayout = v.findViewById(R.id.item_main_ll);
}
}
}
HistoryDescribeActivity.java
package com.example.today;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.ImageView;import android.widget.TextView;
import com.example.today.base.BaseActivity;import com.example.today.base.ContentURL;import com.example.today.bean.HistoryDescribeBean;import com.google.gson.Gson;import com.squareup.picasso.Picasso;
public class HistoryDescribeActivity extends BaseActivity implements View.OnClickListener {
private ImageView imgBack, imgShare, imgDesc;
private TextView tvTitle, tvContent;
private HistoryDescribeBean.ResultBean resultBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history_describe);
imgBack = findViewById(R.id.historyDesc_imgBack);
imgShare = findViewById(R.id.historyDesc_imgShare);
imgDesc = findViewById(R.id.historyDesc_imgDesc);
tvTitle = findViewById(R.id.historyDesc_tvTitle);
tvContent = findViewById(R.id.historyDesc_tvContent);
imgBack.setOnClickListener(this);
imgShare.setOnClickListener(this);
String historyId = getIntent().getStringExtra("historyId");
String historyDescURL = ContentURL.getHistoryDescURL("1.0", historyId);
LoadData(historyDescURL);
}
@Override
public void onSuccess(String result) {
HistoryDescribeBean historyDescribeBean = new Gson().fromJson(result, HistoryDescribeBean.class);
resultBean = historyDescribeBean.getResult().get(0);
tvTitle.setText(resultBean.getTitle());
tvContent.setText(resultBean.getContent());
String picURL = resultBean.getPic();
if (TextUtils.isEmpty(picURL)) {
imgDesc.setVisibility(View.GONE);
} else {
imgDesc.setVisibility(View.VISIBLE);
Picasso.with(this).load(picURL).into(imgDesc);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.historyDesc_imgBack:
finish();
break;
case R.id.historyDesc_imgShare:
String text="我发现一款好用的软件专属空间-历史上的今天,快来一起探索这个App吧!";
if (resultBean!=null) {
text="想要了解"+resultBean.getTitle()+"详情吗?快来下载专属空间吧";
}
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,text);
startActivity(Intent.createChooser(intent,"历史上的今天"));
break;
}
}
}
MainActivity.java
package com.example.today;
import android.app.DatePickerDialog;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.widget.AdapterView;import android.widget.DatePicker;import android.widget.ImageButton;import android.widget.ListView;import android.widget.RadioButton;import android.widget.TextView;
import com.example.today.base.BaseActivity;import com.example.today.base.ContentURL;import com.example.today.bean.HistoryBean;import com.example.today.bean.LaoHuangLiBean;import com.google.gson.Gson;
import org.xutils.http.RequestParams;import org.xutils.x;
import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;
import java.util.Date;import java.util.List;
public class MainActivity extends BaseActivity implements View.OnClickListener{
private ListView todayLv;
private ImageButton imgBtn;
List<HistoryBean.ResultBean> mDatas;
private HistoryAdapter adapter;
private Calendar calendar;
private Date date;
//头布局的控件
private TextView tvSolar, tvDay, tvWeek, tvLunar, tvBaiJi, tvWuXin, tvChongSha, tvJiShen, tvXiongShen, tvYi, tvJi;
HistoryBean historyBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
todayLv = (ListView) findViewById(R.id.today_lv);
imgBtn = (ImageButton) findViewById(R.id.t_ImgBtn);
imgBtn.setOnClickListener(this);
mDatas=new ArrayList<>();
adapter = new HistoryAdapter(this, mDatas);
//获取日历对象
calendar = Calendar.getInstance();
date=new Date();
calendar.setTime(date);
int month=calendar.get(calendar.MONTH)+1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
//添加头布局和尾布局 addHeaderAndFooterView();
String todayHistoryURL = ContentURL.getTodayHistoryURL("1.0", month, day);
LoadData(todayHistoryURL);
todayLv.setAdapter(adapter);
todayLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, HistoryDescribeActivity.class);
//由于添加了头布局,位置要减1
//HistoryBean.ResultBean resultBean = datas.get(position - 1);
//或者使用下面的方法获取正确位置的数据
HistoryBean.ResultBean resultBean = (HistoryBean.ResultBean) parent.getAdapter().getItem(position);
String beanId = resultBean.get_id();
intent.putExtra("historyId", beanId);
startActivity(intent);
}
});
}
private void addHeaderAndFooterView() {
//将头布局和尾布局xml布局文件转换成View对象
View headerView = LayoutInflater.from(this).inflate(R.layout.main_headerview, null);
//初始化头布局和加载数据 initHeaderView(headerView);
View footerView = LayoutInflater.from(this).inflate(R.layout.main_footer, null);
//给ListView添加头布局和尾布局
// false代表头布局或者脚布局不可点击,第二个参数代表与listview绑定的数据可传null
//添加了头布局后头布局也算索引
todayLv.addHeaderView(headerView,null,false);
todayLv.addFooterView(footerView,null,false);
}
/**
* 获取老黄历接口的数据
*
* @param laoHaungLiURL
*/
private void loadHeaderData(String laoHaungLiURL) {
RequestParams params = new RequestParams(laoHaungLiURL);
x.http().get(params, new CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Log.i("laohuangli", "success");
LaoHuangLiBean laoHuangLiBean = new Gson().fromJson(result, LaoHuangLiBean.class);
LaoHuangLiBean.ResultBean resultBean = laoHuangLiBean.getResult();
//给TextView赋值
tvLunar.setText("农历" + resultBean.getYinli() + "(阴历)");
String[] yangliArr = resultBean.getYangli().split("-");
String week = getWeek(Integer.parseInt(yangliArr[0]), Integer.parseInt(yangliArr[1]), Integer.parseInt(yangliArr[2]));
tvSolar.setText("公历 " + yangliArr[0] + "年" + yangliArr[1] + "月" + yangliArr[2] + "日 " + week + "(阳历)");
tvDay.setText(yangliArr[2]);
tvWeek.setText(week);
tvBaiJi.setText("彭祖百忌:" + resultBean.getBaiji());
tvWuXin.setText("五行:" + resultBean.getWuxing());
tvChongSha.setText("冲煞:" + resultBean.getChongsha());
tvJiShen.setText("吉神宜趋:" + resultBean.getJishen());
tvXiongShen.setText("凶神宜忌:" + resultBean.getXiongshen());
tvYi.setText("宜:" + resultBean.getYi());
tvJi.setText("忌:" + resultBean.getJi());
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
private String getWeek(int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
//calender的月份用的是数组的下标
calendar.set(year, month - 1, day);
String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (index < 0) { //以防万一
index = 0;
}
return weeks[index];
}
/**
* 初始化头布局的控件和加载数据
*
* @param headerView
*/
private void initHeaderView(View headerView) {
tvSolar = headerView.findViewById(R.id.main_header_tvSolar);
tvDay = headerView.findViewById(R.id.main_header_tvDay);
tvWeek = headerView.findViewById(R.id.main_header_tvWeek);
tvLunar = headerView.findViewById(R.id.main_header_tvLunar);
tvBaiJi = headerView.findViewById(R.id.main_header_tvBaiJi);
tvWuXin = headerView.findViewById(R.id.main_header_tvWuXin);
tvChongSha = headerView.findViewById(R.id.main_header_tvChongSha);
tvJiShen = headerView.findViewById(R.id.main_header_tvJiShen);
tvXiongShen = headerView.findViewById(R.id.main_header_tvXiongShen);
tvYi = headerView.findViewById(R.id.main_header_tvYi);
tvJi = headerView.findViewById(R.id.main_header_tvJi);
//将日期对象格式化成指定对象的字符串
String pattern = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String time = sdf.format(date);
//获取老黄历URL
String laoHaungLiURL = ContentURL.getLaohuangliURL(time);
//获取老黄历接口数据 loadHeaderData(laoHaungLiURL);
}
@Override
public void onSuccess(String result) {
mDatas.clear();
historyBean = new Gson().fromJson(result, HistoryBean.class);
List<HistoryBean.ResultBean> list = historyBean.getResult();
for (int i = 0; i < 5; i++) {
mDatas.add(list.get(i));
}
//更新数据 adapter.notifyDataSetChanged();
}
public void foot1(View view){
Intent intent = new Intent(MainActivity.this, TestActivity.class);
startActivity(intent);
}
public void foot2(View view){
Intent intent = new Intent(MainActivity.this, HistoryActivity.class);
if (historyBean != null) {//历史上的今天不为空
Bundle bundle = new Bundle();//用Bundle存储数据
bundle.putSerializable("history", historyBean);
intent.putExtras(bundle);//把bundle添加到intent中 }
startActivity(intent);
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.t_ImgBtn) {
popCalendarDialog();
return;
}
}
private void popCalendarDialog() {
Calendar calendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
/**
* 选择日期,然后加载选择的日期的老黄历和历史上的今年的数据
* @param view
* @param year
* @param month
* @param dayOfMonth
*/
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String time = year + "-" + (month + 1) + "-" + dayOfMonth;
//更新老黄历显示内容
String laoHuangLiURL = ContentURL.getLaohuangliURL(time);
loadHeaderData(laoHuangLiURL);
//更新历史上的今天的数据
String todayHistoryURL = ContentURL.getTodayHistoryURL("1.0", month + 1, dayOfMonth);
LoadData(todayHistoryURL);
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
}
drawable中的calender.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false"
>
<!-- 透明红-->
<solid
android:color="#88F22222"/>
<padding
android:left="2dp"
android:right="2dp"
android:top="1dp"
android:bottom="1dp"/>
<size
android:width="30dp"
android:height="30dp"/></shape>
layout中的布局:
activity_history.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HistoryActivity">
<RelativeLayout
android:id="@+id/historyTitle"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/mainBg">
<TextView
android:id="@+id/history_tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="历史上的这一天"
android:layout_centerInParent="true"
android:textSize="18sp"
android:textStyle="bold"/>
<ImageView
android:id="@+id/history_imgBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/icon_back"
android:layout_centerVertical="true"/>
</RelativeLayout>
<View
android:id="@+id/historyLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/historyTitle"
android:background="@color/fireRed"/>
<ListView
android:id="@+id/historyLv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/historyLine"
android:divider="@null">
</ListView>
<TextView
android:id="@+id/history_tvEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:textStyle="bold"
android:text="暂无数据"
android:visibility="gone"/>
</RelativeLayout>
activity_history_describe.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mainBg"
android:orientation="vertical"
tools:context=".HistoryDescribeActivity">
<RelativeLayout
android:id="@+id/historyTitle"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/mainBg">
<TextView
android:id="@+id/history_tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="历史上的这一天"
android:layout_centerInParent="true"
android:textSize="18sp"
android:textStyle="bold"/>
<ImageView
android:id="@+id/historyDesc_imgBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/icon_back"
android:layout_centerVertical="true"/>
<ImageView
android:id="@+id/historyDesc_imgShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/icon_share"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"/>
</RelativeLayout>
<View
android:id="@+id/historyLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/historyTitle"
android:background="@color/fireRed"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/historyDesc_tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18sp"
android:textStyle="bold"
android:layout_margin="10dp"/>
<ImageView
android:id="@+id/historyDesc_imgDesc"
android:layout_width="match_parent"
android:layout_height="280dp"
android:scaleType="fitXY"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/historyDesc_tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_margin="10dp"/>
</LinearLayout>
</ScrollView></LinearLayout>
activity_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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/today_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"/>
<ImageButton
android:id="@+id/t_ImgBtn"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@mipmap/icon_calendar"
android:background="@drawable/calendar_bg"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="20dp"/></RelativeLayout>
item_main_timeline.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="@color/mainBg"
android:orientation="vertical">
<LinearLayout
android:id="@+id/item_main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="1dp"
android:src="@mipmap/timeline_group_divider" />
<View
android:layout_width="1dp"
android:layout_height="30dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="1dp"
android:background="@color/line" />
</LinearLayout>
<TextView
android:id="@+id/item_main_tvTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_weight="15"
android:text="2020-4-20"
android:textColor="@color/line" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@mipmap/template_time_spot" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:layout_gravity="center_horizontal"
android:background="@color/line" />
</LinearLayout>
</RelativeLayout>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="15"
android:background="#FFF"
android:elevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/item_main_pic"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="@mipmap/icon" />
<TextView
android:id="@+id/item_main_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="5dp"
android:textSize="16sp"
android:text="*********" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
main_footer.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="wrap_content"
android:gravity="center"
android:background="@color/mainBg"
android:orientation="horizontal">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="返回"
android:textSize="25sp"
android:button="@null"
android:gravity="center"
android:onClick="foot1"
/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="点击加载更多"
android:textSize="25sp"
android:button="@null"
android:gravity="center"
android:onClick="foot2"
/>
</RadioGroup>
</LinearLayout>
main_headerview.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:background="@color/mainBg">
<TextView
android:id="@+id/main_header_tvSolar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:gravity="center"
android:padding="3dp"
android:text="公历2020年4月20日 星期一(阳历)"/>
<TextView
android:id="@+id/main_header_tvDay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="80sp"
android:gravity="center"
android:textColor="@color/fireRed"
android:text="20"/>
<TextView
android:id="@+id/main_header_tvWeek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="26sp"
android:gravity="center"
android:textColor="@color/fireRed"
android:text="星期一"/>
<TextView
android:id="@+id/main_header_tvLunar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:gravity="center"
android:padding="3dp"
android:layout_marginTop="10dp"
android:text="农历***********"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp"
android:padding="10dp"
>
<TextView
android:id="@+id/main_header_tvBaiJi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:text=""/>
<TextView
android:id="@+id/main_header_tvWuXin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text=""/>
<TextView
android:id="@+id/main_header_tvChongSha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="14sp"
android:text=""/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/fireRed"/>
<TextView
android:id="@+id/main_header_tvJiShen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:text=""/>
<TextView
android:id="@+id/main_header_tvXiongShen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="14sp"
android:text=""/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/fireRed"/>
<TextView
android:id="@+id/main_header_tvYi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:text=""/>
<TextView
android:id="@+id/main_header_tvJi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="14sp"
android:text=""/>
</LinearLayout>
<TextView
android:id="@+id/main_header_tvHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:text="历史上的今天"/>
</LinearLayout>
项目所要添加的依赖有:
implementation 'org.xutils:xutils:3.7.6'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.code.gson:gson:2.2.4'