android (xutlis)

来源:互联网 发布:淘宝最好的饰品店 编辑:程序博客网 时间:2024/05/17 06:16
//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwie.week01.MainActivity">

<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="50dp"></android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>

</LinearLayout>

//item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image_one"
android:layout_width="100dp"
android:layout_height="80dp" />
</LinearLayout>

//myfragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>




//App.java
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG);
}
}


//MainActivity.java
//绑定布局
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {


//绑定控件
@ViewInject(R.id.tablayout)
private TabLayout tabLayout;
@ViewInject(R.id.viewpager)
private ViewPager viewPager;

private Gson gson;
private List<Databean.TngouBean> list;
private MyFragment[] fragments;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

x.view().inject(this);
gson = new Gson();
loadDataList();

}

private void loadDataList() {
RequestParams params = new RequestParams("http://www.tngou.net/api/top/classify");
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Databean datalist = gson.fromJson(result, Databean.class);
list = datalist.getTngou();
initTab();
}


@Override
public void onError(Throwable ex, boolean isOnCallback) {

}

@Override
public void onCancelled(CancelledException cex) {

}

@Override
public void onFinished() {

}
});
}

private void initTab() {
for (Databean.TngouBean bean : list) {
tabLayout.addTab(tabLayout.newTab().setText(bean.getTitle()));
}
fragments = new MyFragment[list.size()];
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
if (fragments[position] == null) {
MyFragment my = MyFragment.getInstance(list.get(position).getId());
fragments[position] = my;
}
return fragments[position];
}

@Override
public int getCount() {
return list.size();
}

@Override
public CharSequence getPageTitle(int position) {

return list.get(position).getName();
}
});
tabLayout.setupWithViewPager(viewPager);
}

}


//Myfragment.java
@ContentView(R.layout.myfragment)
public class MyFragment extends Fragment {
@ViewInject(R.id.listview)
private ListView listView;
private List<Databean02.TngouBean> list = new ArrayList<>();

private MyAdapter adapter;
private int id;


public static MyFragment getInstance(int id) {
MyFragment myfragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putInt("id", id);
myfragment.setArguments(bundle);
return myfragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = x.view().inject(this, inflater, container);
id = getArguments().getInt("id");
adapter = new MyAdapter();
listView.setAdapter(adapter);
loaddata();
return view;
}

private void loaddata() {
RequestParams params = new RequestParams("http://www.tngou.net/api/top/list");
params.addQueryStringParameter("page", "1");
params.addQueryStringParameter("raw", "10");
params.addQueryStringParameter("id", id + "");

x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Databean02 data = new Gson().fromJson(result, Databean02.class);
list = data.getTngou();
adapter.notifyDataSetChanged();
}

@Override
public void onError(Throwable ex, boolean isOnCallback) {

}

@Override
public void onCancelled(CancelledException cex) {

}

@Override
public void onFinished() {

}
});
}

class MyAdapter extends BaseAdapter {

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(getContext(), R.layout.item, null);
holder = new ViewHolder();
x.view().inject(holder,convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Databean02.TngouBean bean = list.get(position);
holder.name.setText(bean.getTitle());
x.image().bind(holder.image, "http://tnfs.tngou.net/image" + bean.getImg());
return convertView;
}
}

class ViewHolder {
@ViewInject(R.id.image_one)
ImageView image;

@ViewInject(R.id.name)
TextView name;
}
}



//两bean类
//1:Databean.java
public class Databean {

/**
* status : true
* tngou : [{"description":"天狗实时事件:民生热点事件,民生热词排行 做最好的民生热点、热词提取;推送最新的民生实时事件,挖掘最新的民生热词。","id":1,"keywords":"民生热点事件 民生热词排行 天狗实时事件","name":"民生热点","seq":1,"title":"民生热点事件_民生热词排行-天狗实时事件"},{"description":"天狗实时事件:娱乐热点事件,娱乐热词排行 做最好的娱乐热点、热词提取;推送最新的娱乐实时事件,挖掘最新的娱乐热词。","id":2,"keywords":"娱乐热点事件 娱乐热词排行 天狗实时事件","name":"娱乐热点","seq":2,"title":"娱乐热点事件_娱乐热词排行-天狗实时事件"},{"description":"天狗实时事件:财经热点事件,财经热词排行 做最好的财经热点、热词提取;推送最新的财经实时事件,挖掘最新的财经热词。","id":3,"keywords":"财经热点事件 财经热词排行 天狗实时事件","name":"财经热点","seq":3,"title":"财经热点事件_财经热词排行-天狗实时事件"},{"description":"天狗实时事件:体育热点事件,体育热词排行 做最好的体育热点、热词提取;推送最新的体育实时事件,挖掘最新的体育热词。","id":4,"keywords":"体育热点事件 体育热词排行 天狗实时事件","name":"体育热点","seq":4,"title":"体育热点事件_体育热词排行-天狗实时事件"},{"description":"天狗实时事件:教育热点事件,教育热词排行 做最好的教育热点、热词提取;推送最新的教育实时事件,挖掘最新的教育热词。","id":5,"keywords":"教育热点事件 教育热词排行 天狗实时事件","name":"教育热点","seq":5,"title":"教育热点事件_教育热词排行-天狗实时事件"},{"description":"天狗实时事件:社会热点事件,社会热词排行 做最好的社会热点、热词提取;推送最新的社会实时事件,挖掘最新的社会热词。","id":6,"keywords":"社会热点事件 社会热词排行 天狗实时事件","name":"社会热点","seq":6,"title":"社会热点事件_社会热词排行-天狗实时事件"}]
*/

private boolean status;
private List<TngouBean> tngou;

public boolean isStatus() {
return status;
}

public void setStatus(boolean status) {
this.status = status;
}

public List<TngouBean> getTngou() {
return tngou;
}

public void setTngou(List<TngouBean> tngou) {
this.tngou = tngou;
}

public static class TngouBean {
/**
* description : 天狗实时事件:民生热点事件,民生热词排行 做最好的民生热点、热词提取;推送最新的民生实时事件,挖掘最新的民生热词。
* id : 1
* keywords : 民生热点事件 民生热词排行 天狗实时事件
* name : 民生热点
* seq : 1
* title : 民生热点事件_民生热词排行-天狗实时事件
*/

private String description;
private int id;
private String keywords;
private String name;
private int seq;
private String title;

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getKeywords() {
return keywords;
}

public void setKeywords(String keywords) {
this.keywords = keywords;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSeq() {
return seq;
}

public void setSeq(int seq) {
this.seq = seq;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
}
//2:Databean02.java
public class Databean02 {

/**
* status : true
* total : 16004
* tngou : [{"count":593,"description":"漫画多次勒索他人财物但屡次被拒,而且又受到被勒索人和其朋友的责备心生怨恨,提出要与对方见面缓和关系,却在交谈中故意制造冲突再将人伤","fcount":0,"fromname":"齐鲁晚报","fromurl":"http://www.qlwb.com.cn/2017/0405/898961.shtml","id":16250,"img":"/top/170405/db6602fe0dae0e0b68d00b16d7009add.png","keywords":"嫌犯勒索未成反遭责备","rcount":0,"time":1491372765000,"title":"嫌犯勒索未成反遭责备 假装要缓和关系故意\"挑事\"砍伤人","topclass":0},{"count":387,"description":"4日援引叙利亚人权观察组织报告称,叙利亚西北部伊德利卜省汉谢洪市当地时间4日遭到疑似化学武器攻击,造成至少58人死亡,其中包括11名儿童,另有数十人受伤","fcount":0,"fromname":"东方头条","fromurl":"http://mil.eastday.com/a/170405073303113.html","id":16249,"img":"/top/default.jpg","keywords":"叙利亚遭战机投毒气","rcount":0,"time":1491365565000,"title":"叙利亚遭战机投毒气 惨案背后迷雾重重","topclass":0},{"count":418,"description":"p;央视网消息:当地时间3号,俄罗斯圣彼得堡地铁站发生爆炸,事故原因暂不清楚","fcount":0,"fromname":"新浪新闻","fromurl":"http://news.sina.com.cn/o/2017-04-03/doc-ifycwyxr9291899.shtml","id":16248,"img":"/top/170403/584560548dd6a085ca278a2480fccc07.jpg","keywords":"俄罗斯地铁站爆炸","rcount":0,"time":1491232365000,"title":"俄罗斯圣彼得堡地铁站发生爆炸","topclass":0},{"count":382,"description":"孕妇花5千请人为父扫墓清明扫墓是中华民族的传统习俗,现代人由于异地生活等原因无法亲自去扫墓,于是一种代替扫墓的业务抓住市场需求应运而生","fcount":0,"fromname":"中国青年网","fromurl":"http://minsheng.youth.cn/mszxgch/201704/t20170402_9408136.htm","id":16247,"img":"/top/default.jpg","keywords":"花5千请人为父亲扫墓","rcount":0,"time":1491131566000,"title":"孕妇花5千请人为父扫墓 网友对此褒贬不一各持己见","topclass":0},{"count":463,"description":"\u201c河北高院态度还可以\u201d\u201c谈得很艰难,有的时候连着谈两天,有的时候谈三天,但是谈得比较融洽,最后的结果双方都还能接受","fcount":0,"fromname":"中华网","fromurl":"http://news.china.com/socialgd/10000169/20170402/30383598.html","id":16246,"img":"/top/170402/7fb2aba0c05b8ea8f079751a23554772.jpg","keywords":"聂树斌案将拍电影系假新闻","rcount":0,"time":1491131565000,"title":"聂树斌案将拍电影 媒体称最高法正与张艺谋接洽","topclass":0},{"count":224,"description":"好汉哥持刀砸车窗路遇不平拿砍刀逼骗子还钱\u201c好汉哥\u201d持刀砸车窗逼骗子退钱违禁词已过滤现场视频可见,一左手撑蓝色伞,右手持柴刀的中年男子与一女子对峙,一装满货物的面包车玻璃被砸碎,大批市民围观","fcount":0,"fromname":"东方头条","fromurl":"http://mini.eastday.com/a/170402172115846.html","id":16245,"img":"/top/default.jpg","keywords":"好汉哥持刀砸车窗","rcount":0,"time":1491127967000,"title":"好汉哥持刀砸车窗 路遇不平拿砍刀逼骗子还钱","topclass":0},{"count":408,"description":"4月1日,高县人民检察院工作人员表示,郑元辉作案时系醉酒状态,处警民警在口头警告和拔枪警示无效后,共开枪6次,其中鸣枪示警一次","fcount":0,"fromname":"东方头条","fromurl":"http://mini.eastday.com/a/170402150908833.html","id":16244,"img":"/top/default.jpg","keywords":"持刀与警察对峙被击毙","rcount":0,"time":1491127966000,"title":"真是奇怪!男子持刀与警察对峙为什么被击毙?","topclass":0},{"count":366,"description":"女子被困电梯8个半小时,她说自己站着按警铃按一晚上,整整8个半小时,直到今天早上邻居听到动静自己才被发现","fcount":0,"fromname":"中国青年网","fromurl":"http://minsheng.youth.cn/mszxgch/201704/t20170402_9408081.htm","id":16243,"img":"/top/default.jpg","keywords":"女子被困电梯8个半小时","rcount":0,"time":1491127965000,"title":"女子被困电梯8个半小时 自己站着按警铃按一晚上","topclass":0},{"count":315,"description":"初步核查,疑似非法储存炸药导致爆炸,爆炸造成9间房屋倒塌、4间简易房倒塌,爆炸波冲击周围房屋5间受损","fcount":0,"fromname":"腾讯新闻","fromurl":"http://news.qq.com/a/20170402/009169.htm","id":16242,"img":"/top/default.jpg","keywords":"山西居民楼爆炸","rcount":0,"time":1491124367000,"title":"山西浮山县一居民楼发生爆炸 6人遇难3人仍被埋","topclass":0},{"count":770,"description":"李先生介绍,月初,他浏览网页时,看到\u201c白血病的哥哥文飞达急需50万治疗\u201d的项目,想贡献300元爱心,但切换微博手机端付款时,由于输入法的问题,没有看清楚小数点,也没有选对项目,鬼使神差的把30000元,错捐给了一个自称是河北慈善联合基金会支持的,关爱贫困残疾人陈兰堂的项目","fcount":0,"fromname":"中国青年网","fromurl":"http://minsheng.youth.cn/mszxgch/201704/t20170402_9407975.htm","id":16241,"img":"/top/default.jpg","keywords":"错将3百捐成3万","rcount":0,"time":1491124365000,"title":"将三百错捐成三万 无法验证项目真伪","topclass":0},{"count":534,"description":"<>中新网微博截图【河北雄县:中介及开发商即刻停止一切售楼行为】4月2日,河北雄县召开紧急会议,出席会议的雄县有关部门均在现场表态,副县长谢克庆表示,政府正在查封查处所有售楼门店,现在开始停止一切售楼行为","fcount":0,"fromname":"腾讯新闻","fromurl":"http://news.qq.com/a/20170402/013108.htm","id":16240,"img":"/top/170402/88ee02af5c3ff587c2baff801f76fe5e.jpg","keywords":"河北雄县停止一切售楼行为","rcount":0,"time":1491117166000,"title":"河北雄县宣布停止一切售楼行为 警方也放狠话","topclass":0},{"count":634,"description":"权某和这具女尸还在自己家里睡了一晚第二天早上权某却不见了踪影真是细思极恐随着权某被抓背后的故事浮出水面其中细节更是让人脊背发凉","fcount":0,"fromname":"中国青年网","fromurl":"http://minsheng.youth.cn/mszxgch/201704/t20170402_9407707.htm","id":16239,"img":"/top/default.jpg","keywords":"带尸体到朋友家借宿","rcount":0,"time":1491117165000,"title":"带尸体到朋友家借宿 其中细节更是让人脊背发凉!","topclass":0},{"count":272,"description":"围观的网友们也是rio嗨,都是隐藏在民间的\u201c神段手\u201d啊↓@第一眼的意中人:被交警耽误的奥运冠军[doge]@罗罗菇凉_:我怀疑他插上了隐形的翅膀[思考]@貓小柒家有七只小猫:比我开车都快[二哈]@猫哥是我啊:我开车才开60迈,好吧我是新手[二哈][二哈][二哈]@二楼的污师:我骑单车跑赢过的士,每次说到这我都激动的拍着轮椅@瓷娃娃呀2017:看完这个我在担心我会不会被抓","fcount":0,"fromname":"齐鲁晚报","fromurl":"http://www.qlwb.com.cn/2017/0402/898049.shtml","id":16238,"img":"/top/170402/bfa1fada6775805c6349b890016607bf.png","keywords":"骑自行车超速被抓","rcount":0,"time":1491113565000,"title":"男子骑自行车超速被抓!青岛超速小哥还真不是第一人(图)","topclass":0},{"count":301,"description":"不意外的话,教育和科研机构会优先搬迁,大型医院和非金融性的央企会随之跟进,然后剩余的土地向各大企业的研发中心、各大文化机构的创作中心、各大媒体的信息中心招商,从而培养一个生产非实体产品的超级新区","fcount":0,"fromname":"新浪新闻","fromurl":"http://news.sina.com.cn/c/nd/2017-04-02/doc-ifycwymx3357434.shtml","id":16237,"img":"/top/170402/9af255f514e5a7bd76f505a633bc11bc.jpg","keywords":"河北雄安新区设立","rcount":0,"time":1491106365000,"title":"媒体:河北雄安新区设立 清华大学要去河北了?","topclass":0},{"count":144,"description":"▲静静和洪双双为上百市民领跳广场舞▲领舞的小静静举手投足都十分认真每天晚上7时30分,在沙坪坝区金阳易城国际广场,伴随动感的音乐声,上百人踩着节拍舞动,人群前方的舞台上,一位领舞者格外引人注目","fcount":0,"fromname":"中华网","fromurl":"http://news.china.com/social/1007/20170402/30383177.html","id":16236,"img":"/top/170402/9598b7bb5bb86fef7c62cf8ba3e428d4.jpg","keywords":"5岁半女孩广场领舞","rcount":0,"time":1491102765000,"title":"5岁半女孩广场领舞 粉丝将家搬到附近与其跳舞","topclass":0},{"count":57,"description":"无论是观看视频,或是实际交流,语言的差异的确给人们的生活带来了一些不便,甚至同为中国人,不同地区之间的方言都会造成沟通障碍","fcount":0,"fromname":"中国青年网","fromurl":"http://minsheng.youth.cn/mszxgch/201704/t20170401_9404840.htm","id":16235,"img":"/top/default.jpg","keywords":"翻译界黑科技宙斯瞳","rcount":0,"time":1491070365000,"title":"翻译界黑科技宙斯瞳 还在后悔当年没有学好四六级吗?","topclass":0},{"count":76,"description":"\u201d照片中,朱茵搂着黄贯中献吻,十分恩爱,在集体留影中,二人女儿近照曝光,十分可爱","fcount":0,"fromname":"中华网","fromurl":"http://news.china.com/socialgd/10000169/20170401/30379452.html","id":16234,"img":"/top/170401/ac684daa30f28d8eb3ae4266f0ebff25.jpg","keywords":"黄贯中生日朱茵献吻","rcount":0,"time":1491045165000,"title":"黄贯中生日庆祝朱茵献吻 女儿可爱露面众星云集","topclass":0},{"count":84,"description":"王菲和窦唯李湘和李厚霖15年8月初,网上突然传出刘雨欣已经结婚三年,并且育有一女的消息,据了解,她的老公叫李濛,名副其实的富二代,目前在光大银行担任高层,婚内刘雨欣曾流产两次,可在她流产和生孩子期间,老公却四处寻花问柳,忍无可忍之下,刘雨欣只能提出离婚","fcount":0,"fromname":"中华网","fromurl":"http://news.china.com/socialgd/10000169/20170331/30376250_all.html","id":16233,"img":"/top/170331/5f0bb6874c0e4baf60668bbf3db99585.jpg","keywords":"马景涛宣布离婚","rcount":0,"time":1490958765000,"title":"马景涛宣布离婚,娱乐圈里那些离过婚的明星们","topclass":0},{"count":76,"description":"近日,一则\u201c老汉娶小22岁乞讨女\u201d引发网友热议,据悉:这位老汉今年60岁,有一天在路上看到一个沿街要饭的女子,之后便把她娶回家,并生下了1儿1女","fcount":0,"fromname":"中国青年网","fromurl":"http://minsheng.youth.cn/mszxgch/201703/t20170331_9396167.htm","id":16232,"img":"/top/default.jpg","keywords":"老汉娶小22岁乞讨女","rcount":0,"time":1490947965000,"title":"60岁老汉娶小22岁乞讨女 背后故事直戳泪点","topclass":0},{"count":77,"description":"未料,孩子的爸爸竟然拒收了\u2026\u2026怪事女子找摩的司机\u201c快递\u201d女婴老李是一名摩的司机,常年在金石滩附近拉客","fcount":0,"fromname":"中国青年网","fromurl":"https://news.youth.cn/jsxw/201703/t20170330_9389136.htm","id":16231,"img":"/top/default.jpg","keywords":"快递女儿丈夫拒收","rcount":0,"time":1490875965000,"title":"快递女儿丈夫拒收 送货司机懵了警方已经介入","topclass":0}]
*/

private boolean status;
private int total;
private List<TngouBean> tngou;

public boolean isStatus() {
return status;
}

public void setStatus(boolean status) {
this.status = status;
}

public int getTotal() {
return total;
}

public void setTotal(int total) {
this.total = total;
}

public List<TngouBean> getTngou() {
return tngou;
}

public void setTngou(List<TngouBean> tngou) {
this.tngou = tngou;
}

public static class TngouBean {
/**
* count : 593
* description : 漫画多次勒索他人财物但屡次被拒,而且又受到被勒索人和其朋友的责备心生怨恨,提出要与对方见面缓和关系,却在交谈中故意制造冲突再将人伤
* fcount : 0
* fromname : 齐鲁晚报
* fromurl : http://www.qlwb.com.cn/2017/0405/898961.shtml
* id : 16250
* img : /top/170405/db6602fe0dae0e0b68d00b16d7009add.png
* keywords : 嫌犯勒索未成反遭责备
* rcount : 0
* time : 1491372765000
* title : 嫌犯勒索未成反遭责备 假装要缓和关系故意"挑事"砍伤人
* topclass : 0
*/

private int count;
private String description;
private int fcount;
private String fromname;
private String fromurl;
private int id;
private String img;
private String keywords;
private int rcount;
private long time;
private String title;
private int topclass;

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public int getFcount() {
return fcount;
}

public void setFcount(int fcount) {
this.fcount = fcount;
}

public String getFromname() {
return fromname;
}

public void setFromname(String fromname) {
this.fromname = fromname;
}

public String getFromurl() {
return fromurl;
}

public void setFromurl(String fromurl) {
this.fromurl = fromurl;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getImg() {
return img;
}

public void setImg(String img) {
this.img = img;
}

public String getKeywords() {
return keywords;
}

public void setKeywords(String keywords) {
this.keywords = keywords;
}

public int getRcount() {
return rcount;
}

public void setRcount(int rcount) {
this.rcount = rcount;
}

public long getTime() {
return time;
}

public void setTime(long time) {
this.time = time;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getTopclass() {
return topclass;
}

public void setTopclass(int topclass) {
this.topclass = topclass;
}
}
}
原创粉丝点击