《项目实战与拆解》部分技能综合案例

技能要求:

1、初始化UI相关
2、使用Retrofit+RxJava下载网络数据
3、使用手动解析json方式
4、将全部数据存入数据库中
5、点击一个按钮,读取数据库中的数据
6、并且展示在RecyclerView上:SectionedRecyclerViewAdapter
7、点击item,弹出对话框,确认是否删除
8、长点击item,将数据的图片url传给第二个界面


首先为保证Android Studio能够正常运行在gradle.properties中添加:android.enableAapt2 = false
之后在主目录下的build.gradle中央部分添加:
//    greenDAO
//    整个工程中的build.gradle中添加:
      classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'

在APP目录下的
build.gradle
顶部添加:apply plugin: 'org.greenrobot.greendao'
底部添加依赖:
    //    Glide
    compile 'com.github.bumptech.glide:glide:3.8.0'
    //    Retrofit
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    //    RxJava+Retrofit
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    compile 'io.reactivex.rxjava2:rxjava:2.1.5'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    //    greenDAO
    //    app模块下的build.gradle中添加:
    compile 'org.greenrobot:greendao:3.2.2'
    //    ButterKnife
    //    在app文件夹的build.gradle中一起导入:
    compile 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    //    SectionedRecyclerViewAdapter
    compile 'com.truizlop.sectionedrecyclerview:library:1.2.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'

不要忘记在清单文件中添加网络权限:<uses-permission android:name="android.permission.INTERNET" />


下面就可以开始做技能了,首先是项目中的布局文件.xml
activity_main.xml
<?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.example.day10_zuoye.MainActivity">

    <Button
        android:id="@+id/show_Data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询数据库并展示数据" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.day10_zuoye.activity.SecondActivity">

    <ImageView
        android:id="@+id/cover_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

item_footer.xml
<?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="wrap_content"
    android:padding="16dp">

    <TextView
        android:id="@+id/footer_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="查看更多" />

</RelativeLayout>

item_header.xml
<?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="wrap_content">

    <TextView
        android:id="@+id/header_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="头"
        android:textColor="#ff0000"
        android:textSize="28sp" />

</RelativeLayout>

item_news.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_horizontal"
    android:orientation="vertical"
    android:padding="16dp">

    <ImageView
        android:id="@+id/cover_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/subject_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题" />

</LinearLayout>


布局代码敲完了,可以写.java文件了,为了明确区分.java文件的用途,分别创建了activity、application、bean、converter、httpservice、viewholder六个文件夹,如下:
activity文件夹下的   SecondActivity.java
package com.example.day10_zuoye.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.day10_zuoye.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SecondActivity extends AppCompatActivity {

    @BindView(R.id.cover_iv)
    ImageView mCoverImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.activity_second);

        ButterKnife.bind(this);

        String imgUrl = getIntent()
                .getStringExtra("cover");

        Glide.with(this)
                .load("http://litchiapi.jstv.com" + imgUrl)
                .into(mCoverImageView);


    }
}


application文件夹下的   MyApplication.java
package com.example.day10_zuoye.application;


import android.app.Application;

import com.example.day10_zuoye.bean.DaoMaster;
import com.example.day10_zuoye.bean.DaoSession;
import com.example.day10_zuoye.converter.MyJSONConverter;

import org.greenrobot.greendao.database.Database;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

// http://litchiapi.jstv.com/api/GetFeeds?column=5&PageSize=30&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41
public class MyApplication extends Application {

    private static Retrofit sRetrofit;
    private static DaoSession sDaoSession;

    @Override
    public void onCreate() {
        super.onCreate();

        initRetrofit();
        initGreenDAO();
    }

    private void initGreenDAO() {
        // 创建数据库助手,并指定数据库文件名
        DaoMaster.DevOpenHelper helper =
                new DaoMaster.DevOpenHelper(this, "news.db");
        // 使用数据库助手获取数据库对应的对象
        Database db = helper.getWritableDb();
        // 数据库开启一个会话(操作数据库的环境)
        sDaoSession = new DaoMaster(db).newSession();
    }

    private void initRetrofit() {
        sRetrofit = new Retrofit.Builder()
                .baseUrl("http://litchiapi.jstv.com/")
                .addConverterFactory(new MyJSONConverter())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    public static Retrofit getRetrofit() {
        return sRetrofit;
    }

    public static DaoSession getDaoSession() {
        return sDaoSession;
    }
}


bean文件夹下的   DataBean.java
package com.example.day10_zuoye.bean;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

@Entity
public class DataBean {
    /**
     * subject : 俄战略轰炸机进驻克里米亚
     * summary : 9月初,隶属俄空天军的远程航空兵开始在克里米亚近卫军村派驻轰炸机中队。
     * cover : /Attachs/Article/288179/14123536f2824ba6ad7f32771a9419dc_padmini.JPG
     * pic :
     * format : txt
     * changed : 2015-09-19 10:55:52
     */

    @Id(autoincrement = true)
    private long _id;

    private String subject;
    private String summary;
    private String cover;
    private transient String pic;// transient修饰的属性不会映射到数据库表中
    private String format;
    private String changed;

    @Generated(hash = 538342040)
    public DataBean(long _id, String subject, String summary, String cover,
                    String format, String changed) {
        this._id = _id;
        this.subject = subject;
        this.summary = summary;
        this.cover = cover;
        this.format = format;
        this.changed = changed;
    }

    @Generated(hash = 908697775)
    public DataBean() {
    }

    public long get_id() {
        return this._id;
    }

    public void set_id(long _id) {
        this._id = _id;
    }

    public String getSubject() {
        return this.subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getSummary() {
        return this.summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getCover() {
        return this.cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public String getFormat() {
        return this.format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getChanged() {
        return this.changed;
    }

    public void setChanged(String changed) {
        this.changed = changed;
    }

}

NewsBean.java
package com.example.day10_zuoye.bean;

import java.util.List;

/**
 * Created by estyle on 2018/1/5.
 */

public class NewsBean {
    /**
     * status : ok
     * paramz : {"feeds":[{"id":298847,"oid":288179,"category":"article","data":{"subject":"俄战略轰炸机进驻克里米亚  ","summary":"9月初,隶属俄空天军的远程航空兵开始在克里米亚近卫军村派驻轰炸机中队。","cover":"/Attachs/Article/288179/14123536f2824ba6ad7f32771a9419dc_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 10:55:52"}},{"id":298846,"oid":288178,"category":"article","data":{"subject":"中国新型高超音速飞机首飞 ","summary":"该机具备超高空超高速、飞行方式独特等特点。","cover":"/Attachs/Article/288178/62dfa9c9676e4aa2b4a8687e45cda02f_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 10:48:48"}},{"id":298844,"oid":288176,"category":"article","data":{"subject":"中国天河二号获世界排名五连冠","summary":"\u201c天河\u201d二号超级计算机,连续5次在国际超算500强排行榜上位居榜首。","cover":"/Attachs/Article/288176/4618428152aa4a3e856046295ebb41df_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 10:44:50"}},{"id":298816,"oid":288158,"category":"article","data":{"subject":"美俄军事交往中断后首次通话 ","summary":"美防长卡特与俄防长绍伊古通电话,讨论叙利亚当前局势。 ","cover":"/Attachs/Article/288158/4e6cf9d659a14e84851422ccfd331ef8_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 09:48:24"}},{"id":298792,"oid":288131,"category":"article","data":{"subject":"巴基斯坦空军基地遇袭致42人死","summary":"13名身着准军事部队制服的武装人员在清晨从两个地点对空军基地发动袭击。","cover":"/Attachs/Article/288131/cfebf69be9414a41bd35fe55934f80cb_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 08:18:20"}},{"id":298790,"oid":288128,"category":"article","data":{"subject":"委内瑞拉战斗机在委哥边境坠毁 ","summary":"失事的苏-30战斗机坠毁前在边界地区执行巡逻任务。","cover":"/Attachs/Article/288128/9b66eee3dee949909238a80b0afe9180_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 08:14:45"}},{"id":298435,"oid":287781,"category":"article","data":{"subject":"美军首批实战型F35A战机交付","summary":"美国空军首批实战型F-35A已交付位于犹他州的希尔(Hill)空军基地。","cover":"/Attachs/Article/287781/5f97ab1c7e9a4c6286257bc59d91b341_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:59:24"}},{"id":298428,"oid":287775,"category":"article","data":{"subject":"未亮相武器:DN2可令美军瘫痪","summary":"美国记者比尔-格茨撰写文章,盘点那些没在胜利日阅兵中展示的解放军武器。","cover":"/Attachs/Article/287775/d17862d72c19415e83c40de340b2cc50_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:56:06"}},{"id":298422,"oid":287770,"category":"article","data":{"subject":"泰将斥资9千万引进韩T50教练机","summary":"泰国政府选定韩国产超音速教练机T-50作为泰国空军的教练机机型。","cover":"/Attachs/Article/287770/74b5dd8d620c4308b9ae011db503242f_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:53:28"}},{"id":298416,"oid":287765,"category":"article","data":{"subject":"济州基地针对中日应对潜在冲突","summary":"韩国最先进的宙斯盾驱逐舰停靠基地码头,标志该海军基地开始投入试运营","cover":"/Attachs/Article/287765/c0e57005e0bc468cb94ba18dce1414fd_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:51:12"}},{"id":298411,"oid":287760,"category":"article","data":{"subject":"日本派飞机监视中国东海油气田","summary":"围绕中国在东海的油气田开发,确认到其中4座设施正在进行天然气开采作业。","cover":"/Attachs/Article/287760/5eb587b5d42b4632ba7bd71afd4e4bd7_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:46:48"}},{"id":298406,"oid":287755,"category":"article","data":{"subject":"叙为何青睐中国老红箭73D导弹","summary":"在外国军网站上,有网友发布了几张分体式的反坦克导弹在叙利亚作战的画面。","cover":"/Attachs/Article/287755/e05f1255f33947989a6ab6a8fa63e205_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:43:01"}},{"id":298402,"oid":287752,"category":"article","data":{"subject":"印媒称中国重型直升机战力强悍","summary":"中国人民解放军很可能将把重点放在西藏高原和新疆的直升机与机械化作战上。","cover":"/Attachs/Article/287752/d163f95c571f41738e248891859e6ea3_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:40:34"}},{"id":298380,"oid":287731,"category":"article","data":{"subject":"俄罗斯向中国兜售改进版伊尔76","summary":"俄方将在中国市场推广经过改进的伊尔-76运输机。","cover":"/Attachs/Article/287731/2635d01a44924557ac6a0fb504dc6faf_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:28:22"}},{"id":298374,"oid":287726,"category":"article","data":{"subject":"印尼建基地 要中国变更九段线","summary":"印尼呼吁中国变更海上领土主张标记,同时计划在南海建设新的海军基地。","cover":"/Attachs/Article/287726/e9d09ae373d8432e8a0c26fceb38eef1_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:23:22"}},{"id":298367,"oid":287721,"category":"article","data":{"subject":"日向越提供巡逻船针对南海问题","summary":"日越两方还将进一步确认\u201c海上法治\u201d\u201c航行自由\u201d等的重要性。","cover":"/Attachs/Article/287721/2228ae7e4b5d48279d7a13b4628cfe8e_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:17:51"}},{"id":298048,"oid":287397,"category":"article","data":{"subject":"中国长征六号新型火箭即将首飞","summary":"中国在酒泉卫星发射中心用\u201c长征二号丁\u201d运载火箭以\u201c一箭多星\u201d发式。","cover":"/Attachs/Article/287397/6e553acd110a46519d5c25135dc5ef99_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:36:21"}},{"id":298047,"oid":287395,"category":"article","data":{"subject":"中国C919试飞 最快2017年交付","summary":"C919大飞机模型的一侧都是采用透明材料制作,安装168个乘客座位。","cover":"/Attachs/Article/287395/db37b824ae724a0989c3038871471c4f_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:34:02"}},{"id":298041,"oid":287389,"category":"article","data":{"subject":"中俄签署远程大型客机合作协议","summary":"俄方希望在年底前同中国签署关于双方联合研制远程宽体客机的政府间协议。","cover":"/Attachs/Article/287389/1dc038335f1048f2914c54b071fef9a5_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:27:20"}},{"id":298036,"oid":287386,"category":"article","data":{"subject":"外媒称美俄中成世界军力三强","summary":"世界军力排名,2015年,美国、俄罗斯和中国的军事力量组成世界三强。","cover":"/Attachs/Article/287386/f1dc790ed6d94594b87d98259b4292b8_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:23:28"}},{"id":298035,"oid":287385,"category":"article","data":{"subject":"中国南海铁三角机场即将建成","summary":"在南沙问题上,中国的对外行动和声音,都给国人以温和低调的印象。","cover":"/Attachs/Article/287385/4920d3c510fa414f87323186cfae1ff2_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:21:32"}},{"id":298009,"oid":287367,"category":"article","data":{"subject":"美5亿培训叙士兵 仅数人可抗IS","summary":"美国耗资5亿美元在叙利亚训练大约5400名反对派士兵的项目近日宣告失败。","cover":"/Attachs/Article/287367/aa488209fe4243d2948dd21cc35cbaf5_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:01:25"}},{"id":297965,"oid":287329,"category":"article","data":{"subject":"世宗大王号入港济州海军基地","summary":"进入济州海军基地的第一艘舰艇是前一天从釜山作战基地出发的世宗大王号。","cover":"/Attachs/Article/287329/bb7c8f96362f4f11950e7fa2329ae996_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 10:35:56"}},{"id":297928,"oid":287295,"category":"article","data":{"subject":"新安保法让日重走军国老路","summary":"执政联盟计划在16日当夜强推法案在特别委员会表决通过,17日递交参议院大。","cover":"/Attachs/Article/287295/f6a2269f80a945118748335f1aca47f0_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 09:56:14"}},{"id":297924,"oid":287292,"category":"article","data":{"subject":"外媒:美俄中成世界军力三强","summary":"2015年,美国、俄罗斯和中国的军事力量组成世界三强。","cover":"/Attachs/Article/287292/fa02644ccb1340818006eb3af64ef69d_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 09:51:39"}},{"id":297871,"oid":287251,"category":"article","data":{"subject":"叙受训人员仅四五名参与打击IS","summary":"目前美军在研究更安全的部署方案以安置受训完毕的叙利亚武装人员。","cover":"/Attachs/Article/287251/7112cf3a01e646478a3a109ddeb983ca_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 07:57:44"}},{"id":297797,"oid":287202,"category":"article","data":{"subject":"中马首次大规模实兵联演今举行","summary":"中国与马来西亚今日将在马六甲海峡及其附近海域举行大规模实兵联合军演。","cover":"/Attachs/Article/287202/e196fe7586684a8fb2d82835dd6c6178_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 06:55:15"}},{"id":297700,"oid":287128,"category":"article","data":{"subject":"中国换班巡航常态化巡逻钓鱼岛","summary":"3艘中国海监船进入钓鱼岛海域,替换已经在此巡逻21天的另3艘中国海监船。","cover":"/Attachs/Article/287128/19d15a9ad9644ed4bca7cf49fd3ba50d_padmini.JPG","pic":"","format":"txt","changed":"2015-09-16 15:40:47"}},{"id":297697,"oid":287121,"category":"article","data":{"subject":"探测技术突破歼20可先发现F-22","summary":"一直以来,空战的原则都是谁先发现对方的位置,谁就能够占据先机。","cover":"/Attachs/Article/287121/b9e11abf9ed84699b54504275caf4975_padmini.JPG","pic":"","format":"txt","changed":"2015-09-16 15:27:43"}},{"id":297690,"oid":287115,"category":"article","data":{"subject":"网曝歼31最大速度仅1.8倍音速","summary":"近日网上开始流传一份歼31战斗机的介绍和性能指标,推测歼31具体情况。","cover":"/Attachs/Article/287115/26bd04b47d44472f824ef264a0e26127_padmini.JPG","pic":"","format":"txt","changed":"2015-09-16 15:24:27"}}],"PageIndex":1,"PageSize":30,"TotalCount":11149,"TotalPage":372}
     */

    private String status;
    private ParamzBean paramz;

    public String getStatus() {
        return status;
    }

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

    public ParamzBean getParamz() {
        return paramz;
    }

    public void setParamz(ParamzBean paramz) {
        this.paramz = paramz;
    }

    public static class ParamzBean {
        /**
         * feeds : [{"id":298847,"oid":288179,"category":"article","data":{"subject":"俄战略轰炸机进驻克里米亚  ","summary":"9月初,隶属俄空天军的远程航空兵开始在克里米亚近卫军村派驻轰炸机中队。","cover":"/Attachs/Article/288179/14123536f2824ba6ad7f32771a9419dc_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 10:55:52"}},{"id":298846,"oid":288178,"category":"article","data":{"subject":"中国新型高超音速飞机首飞 ","summary":"该机具备超高空超高速、飞行方式独特等特点。","cover":"/Attachs/Article/288178/62dfa9c9676e4aa2b4a8687e45cda02f_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 10:48:48"}},{"id":298844,"oid":288176,"category":"article","data":{"subject":"中国天河二号获世界排名五连冠","summary":"\u201c天河\u201d二号超级计算机,连续5次在国际超算500强排行榜上位居榜首。","cover":"/Attachs/Article/288176/4618428152aa4a3e856046295ebb41df_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 10:44:50"}},{"id":298816,"oid":288158,"category":"article","data":{"subject":"美俄军事交往中断后首次通话 ","summary":"美防长卡特与俄防长绍伊古通电话,讨论叙利亚当前局势。 ","cover":"/Attachs/Article/288158/4e6cf9d659a14e84851422ccfd331ef8_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 09:48:24"}},{"id":298792,"oid":288131,"category":"article","data":{"subject":"巴基斯坦空军基地遇袭致42人死","summary":"13名身着准军事部队制服的武装人员在清晨从两个地点对空军基地发动袭击。","cover":"/Attachs/Article/288131/cfebf69be9414a41bd35fe55934f80cb_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 08:18:20"}},{"id":298790,"oid":288128,"category":"article","data":{"subject":"委内瑞拉战斗机在委哥边境坠毁 ","summary":"失事的苏-30战斗机坠毁前在边界地区执行巡逻任务。","cover":"/Attachs/Article/288128/9b66eee3dee949909238a80b0afe9180_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 08:14:45"}},{"id":298435,"oid":287781,"category":"article","data":{"subject":"美军首批实战型F35A战机交付","summary":"美国空军首批实战型F-35A已交付位于犹他州的希尔(Hill)空军基地。","cover":"/Attachs/Article/287781/5f97ab1c7e9a4c6286257bc59d91b341_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:59:24"}},{"id":298428,"oid":287775,"category":"article","data":{"subject":"未亮相武器:DN2可令美军瘫痪","summary":"美国记者比尔-格茨撰写文章,盘点那些没在胜利日阅兵中展示的解放军武器。","cover":"/Attachs/Article/287775/d17862d72c19415e83c40de340b2cc50_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:56:06"}},{"id":298422,"oid":287770,"category":"article","data":{"subject":"泰将斥资9千万引进韩T50教练机","summary":"泰国政府选定韩国产超音速教练机T-50作为泰国空军的教练机机型。","cover":"/Attachs/Article/287770/74b5dd8d620c4308b9ae011db503242f_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:53:28"}},{"id":298416,"oid":287765,"category":"article","data":{"subject":"济州基地针对中日应对潜在冲突","summary":"韩国最先进的宙斯盾驱逐舰停靠基地码头,标志该海军基地开始投入试运营","cover":"/Attachs/Article/287765/c0e57005e0bc468cb94ba18dce1414fd_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:51:12"}},{"id":298411,"oid":287760,"category":"article","data":{"subject":"日本派飞机监视中国东海油气田","summary":"围绕中国在东海的油气田开发,确认到其中4座设施正在进行天然气开采作业。","cover":"/Attachs/Article/287760/5eb587b5d42b4632ba7bd71afd4e4bd7_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:46:48"}},{"id":298406,"oid":287755,"category":"article","data":{"subject":"叙为何青睐中国老红箭73D导弹","summary":"在外国军网站上,有网友发布了几张分体式的反坦克导弹在叙利亚作战的画面。","cover":"/Attachs/Article/287755/e05f1255f33947989a6ab6a8fa63e205_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:43:01"}},{"id":298402,"oid":287752,"category":"article","data":{"subject":"印媒称中国重型直升机战力强悍","summary":"中国人民解放军很可能将把重点放在西藏高原和新疆的直升机与机械化作战上。","cover":"/Attachs/Article/287752/d163f95c571f41738e248891859e6ea3_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:40:34"}},{"id":298380,"oid":287731,"category":"article","data":{"subject":"俄罗斯向中国兜售改进版伊尔76","summary":"俄方将在中国市场推广经过改进的伊尔-76运输机。","cover":"/Attachs/Article/287731/2635d01a44924557ac6a0fb504dc6faf_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:28:22"}},{"id":298374,"oid":287726,"category":"article","data":{"subject":"印尼建基地 要中国变更九段线","summary":"印尼呼吁中国变更海上领土主张标记,同时计划在南海建设新的海军基地。","cover":"/Attachs/Article/287726/e9d09ae373d8432e8a0c26fceb38eef1_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:23:22"}},{"id":298367,"oid":287721,"category":"article","data":{"subject":"日向越提供巡逻船针对南海问题","summary":"日越两方还将进一步确认\u201c海上法治\u201d\u201c航行自由\u201d等的重要性。","cover":"/Attachs/Article/287721/2228ae7e4b5d48279d7a13b4628cfe8e_padmini.JPG","pic":"","format":"txt","changed":"2015-09-18 10:17:51"}},{"id":298048,"oid":287397,"category":"article","data":{"subject":"中国长征六号新型火箭即将首飞","summary":"中国在酒泉卫星发射中心用\u201c长征二号丁\u201d运载火箭以\u201c一箭多星\u201d发式。","cover":"/Attachs/Article/287397/6e553acd110a46519d5c25135dc5ef99_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:36:21"}},{"id":298047,"oid":287395,"category":"article","data":{"subject":"中国C919试飞 最快2017年交付","summary":"C919大飞机模型的一侧都是采用透明材料制作,安装168个乘客座位。","cover":"/Attachs/Article/287395/db37b824ae724a0989c3038871471c4f_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:34:02"}},{"id":298041,"oid":287389,"category":"article","data":{"subject":"中俄签署远程大型客机合作协议","summary":"俄方希望在年底前同中国签署关于双方联合研制远程宽体客机的政府间协议。","cover":"/Attachs/Article/287389/1dc038335f1048f2914c54b071fef9a5_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:27:20"}},{"id":298036,"oid":287386,"category":"article","data":{"subject":"外媒称美俄中成世界军力三强","summary":"世界军力排名,2015年,美国、俄罗斯和中国的军事力量组成世界三强。","cover":"/Attachs/Article/287386/f1dc790ed6d94594b87d98259b4292b8_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:23:28"}},{"id":298035,"oid":287385,"category":"article","data":{"subject":"中国南海铁三角机场即将建成","summary":"在南沙问题上,中国的对外行动和声音,都给国人以温和低调的印象。","cover":"/Attachs/Article/287385/4920d3c510fa414f87323186cfae1ff2_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:21:32"}},{"id":298009,"oid":287367,"category":"article","data":{"subject":"美5亿培训叙士兵 仅数人可抗IS","summary":"美国耗资5亿美元在叙利亚训练大约5400名反对派士兵的项目近日宣告失败。","cover":"/Attachs/Article/287367/aa488209fe4243d2948dd21cc35cbaf5_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 11:01:25"}},{"id":297965,"oid":287329,"category":"article","data":{"subject":"世宗大王号入港济州海军基地","summary":"进入济州海军基地的第一艘舰艇是前一天从釜山作战基地出发的世宗大王号。","cover":"/Attachs/Article/287329/bb7c8f96362f4f11950e7fa2329ae996_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 10:35:56"}},{"id":297928,"oid":287295,"category":"article","data":{"subject":"新安保法让日重走军国老路","summary":"执政联盟计划在16日当夜强推法案在特别委员会表决通过,17日递交参议院大。","cover":"/Attachs/Article/287295/f6a2269f80a945118748335f1aca47f0_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 09:56:14"}},{"id":297924,"oid":287292,"category":"article","data":{"subject":"外媒:美俄中成世界军力三强","summary":"2015年,美国、俄罗斯和中国的军事力量组成世界三强。","cover":"/Attachs/Article/287292/fa02644ccb1340818006eb3af64ef69d_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 09:51:39"}},{"id":297871,"oid":287251,"category":"article","data":{"subject":"叙受训人员仅四五名参与打击IS","summary":"目前美军在研究更安全的部署方案以安置受训完毕的叙利亚武装人员。","cover":"/Attachs/Article/287251/7112cf3a01e646478a3a109ddeb983ca_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 07:57:44"}},{"id":297797,"oid":287202,"category":"article","data":{"subject":"中马首次大规模实兵联演今举行","summary":"中国与马来西亚今日将在马六甲海峡及其附近海域举行大规模实兵联合军演。","cover":"/Attachs/Article/287202/e196fe7586684a8fb2d82835dd6c6178_padmini.JPG","pic":"","format":"txt","changed":"2015-09-17 06:55:15"}},{"id":297700,"oid":287128,"category":"article","data":{"subject":"中国换班巡航常态化巡逻钓鱼岛","summary":"3艘中国海监船进入钓鱼岛海域,替换已经在此巡逻21天的另3艘中国海监船。","cover":"/Attachs/Article/287128/19d15a9ad9644ed4bca7cf49fd3ba50d_padmini.JPG","pic":"","format":"txt","changed":"2015-09-16 15:40:47"}},{"id":297697,"oid":287121,"category":"article","data":{"subject":"探测技术突破歼20可先发现F-22","summary":"一直以来,空战的原则都是谁先发现对方的位置,谁就能够占据先机。","cover":"/Attachs/Article/287121/b9e11abf9ed84699b54504275caf4975_padmini.JPG","pic":"","format":"txt","changed":"2015-09-16 15:27:43"}},{"id":297690,"oid":287115,"category":"article","data":{"subject":"网曝歼31最大速度仅1.8倍音速","summary":"近日网上开始流传一份歼31战斗机的介绍和性能指标,推测歼31具体情况。","cover":"/Attachs/Article/287115/26bd04b47d44472f824ef264a0e26127_padmini.JPG","pic":"","format":"txt","changed":"2015-09-16 15:24:27"}}]
         * PageIndex : 1
         * PageSize : 30
         * TotalCount : 11149
         * TotalPage : 372
         */

        private int PageIndex;
        private int PageSize;
        private int TotalCount;
        private int TotalPage;
        private List<FeedsBean> feeds;

        public int getPageIndex() {
            return PageIndex;
        }

        public void setPageIndex(int PageIndex) {
            this.PageIndex = PageIndex;
        }

        public int getPageSize() {
            return PageSize;
        }

        public void setPageSize(int PageSize) {
            this.PageSize = PageSize;
        }

        public int getTotalCount() {
            return TotalCount;
        }

        public void setTotalCount(int TotalCount) {
            this.TotalCount = TotalCount;
        }

        public int getTotalPage() {
            return TotalPage;
        }

        public void setTotalPage(int TotalPage) {
            this.TotalPage = TotalPage;
        }

        public List<FeedsBean> getFeeds() {
            return feeds;
        }

        public void setFeeds(List<FeedsBean> feeds) {
            this.feeds = feeds;
        }

        public static class FeedsBean {
            /**
             * id : 298847
             * oid : 288179
             * category : article
             * data : {"subject":"俄战略轰炸机进驻克里米亚  ","summary":"9月初,隶属俄空天军的远程航空兵开始在克里米亚近卫军村派驻轰炸机中队。","cover":"/Attachs/Article/288179/14123536f2824ba6ad7f32771a9419dc_padmini.JPG","pic":"","format":"txt","changed":"2015-09-19 10:55:52"}
             */

            private int id;
            private int oid;
            private String category;
            private DataBean data;

            public int getId() {
                return id;
            }

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

            public int getOid() {
                return oid;
            }

            public void setOid(int oid) {
                this.oid = oid;
            }

            public String getCategory() {
                return category;
            }

            public void setCategory(String category) {
                this.category = category;
            }

            public DataBean getData() {
                return data;
            }

            public void setData(DataBean data) {
                this.data = data;
            }


        }
    }
}


converter文件夹下的   MyJSONConverter.java
package com.example.day10_zuoye.converter;

import android.support.annotation.Nullable;

import com.example.day10_zuoye.bean.NewsBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * 手动解析json数据为bean
 */

public class MyJSONConverter extends Converter.Factory {

    // 重写响应装换器。json数据就是从服务器向手机返回的响应数据
    @Nullable
    @Override
    public Converter<ResponseBody, NewsBean> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        return new Converter<ResponseBody, NewsBean>() {
            @Override
            public NewsBean convert(ResponseBody responseBody) throws IOException {
                // 未解析之前,响应体中包含着json字符串
                // 从响应体中获取json字符串,手动使用Gson解析
                // 解析后得到的bean作为返回值
                String json = responseBody.string();
                NewsBean newsBean = new Gson().fromJson(json, NewsBean.class);
                return newsBean;
            }
        };
    }
}


httpservice
文件夹下的   NewsHttpService.java
package com.example.day10_zuoye.httpservice;


import com.example.day10_zuoye.bean.NewsBean;

import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface NewsHttpService {

    @GET("api/GetFeeds?column=5&PageSize=30&val=100511D3BE5301280E0992C73A9DEC41")
    Observable<NewsBean> getObservable(@Query("pageIndex") int pageIndex);

}


viewholder
文件夹下的   FooterViewHolder.java
package com.example.day10_zuoye.viewholder;

import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by estyle on 2018/1/5.
 */

public class FooterViewHolder extends RecyclerView.ViewHolder {
    public FooterViewHolder(View itemView) {
        super(itemView);
    }
}

HeaderViewHolder.java
package com.example.day10_zuoye.viewholder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.day10_zuoye.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by estyle on 2018/1/5.
 */

public class HeaderViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.header_tv)
    TextView mHeaderTextView;

    public HeaderViewHolder(View itemView) {
        super(itemView);

        ButterKnife.bind(this, itemView);
    }

    // 绑定数据
    public void bind(String title) {
        mHeaderTextView.setText(title);
    }

}

NewsViewHolder.java
package com.example.day10_zuoye.viewholder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.day10_zuoye.R;
import com.example.day10_zuoye.bean.DataBean;

import butterknife.BindView;
import butterknife.ButterKnife;


public class NewsViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.subject_tv)
    TextView mSubjectTextView;
    @BindView(R.id.cover_iv)
    ImageView mCoverImageView;

    public NewsViewHolder(View itemView) {
        super(itemView);

        ButterKnife.bind(this, itemView);
    }

    public void bind(DataBean data) {
        mSubjectTextView.setText(data.getSubject());

        Glide.with(mCoverImageView.getContext())
                .load("http://litchiapi.jstv.com" + data.getCover())
                .into(mCoverImageView);
    }

}

最后是.java文件的布局管理器:MainActivity.java
package com.example.day10_zuoye;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.truizlop.sectionedrecyclerview.SectionedRecyclerViewAdapter;
import com.truizlop.sectionedrecyclerview.SectionedSpanSizeLookup;

import com.example.day10_zuoye.activity.SecondActivity;
import com.example.day10_zuoye.application.MyApplication;
import com.example.day10_zuoye.bean.DataBean;
import com.example.day10_zuoye.bean.DataBeanDao;
import com.example.day10_zuoye.bean.NewsBean;
import com.example.day10_zuoye.httpservice.NewsHttpService;
import com.example.day10_zuoye.viewholder.FooterViewHolder;
import com.example.day10_zuoye.viewholder.HeaderViewHolder;
import com.example.day10_zuoye.viewholder.NewsViewHolder;

import org.greenrobot.greendao.query.Query;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 刘重阳 on 2018/1/6.
 */
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    @BindView(R.id.show_Data)
    Button mShowDataBtn;

    private DataBeanDao mDao;
    private List<DataBean> mDatas;
    private NewsAdapter mAdapter;

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

        ButterKnife.bind(this);

        mDao = MyApplication.getDaoSession()
                .getDataBeanDao();

        initView();
        loadData();
    }

    // 初始化UI相关
    private void initView() {
        mAdapter = new NewsAdapter();
        mRecyclerView.setAdapter(mAdapter);

        // 列表
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        // 网格
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        // Section框架自带一个处理网格间隔的类
        SectionedSpanSizeLookup lookup = new SectionedSpanSizeLookup(mAdapter, layoutManager);
        // 将间隔对象设置给网格布局管理者才能生效!
        layoutManager.setSpanSizeLookup(lookup);

        mRecyclerView.setLayoutManager(layoutManager);
    }

    private void loadData() {
        NewsHttpService httpService = MyApplication.getRetrofit()
                .create(NewsHttpService.class);
        Observable<NewsBean> observable = httpService.getObservable(1);

        observable.subscribeOn( Schedulers.io())
                .map(new Function<NewsBean, List<NewsBean.ParamzBean.FeedsBean>>() {
                    @Override
                    public List<NewsBean.ParamzBean.FeedsBean> apply(NewsBean newsBean) throws Exception {
                        return newsBean.getParamz().getFeeds();
                    }
                })
                .observeOn( AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<NewsBean.ParamzBean.FeedsBean>>() {
                    @Override
                    public void accept(List<NewsBean.ParamzBean.FeedsBean> feedsBeans) throws Exception {
                        Log.d("1510", feedsBeans.size() + " size");

                        for (NewsBean.ParamzBean.FeedsBean feeds : feedsBeans) {
                            DataBean data = feeds.getData();

                            // 先给对象设置主键:时间戳
                            data.set_id(System.currentTimeMillis());
                            mDao.insert(data);
                        }
                        Toast.makeText(MainActivity.this, "成功插入数据", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    // 使用ButterKnife直接创建按钮点击监听的方法
    @OnClick(R.id.show_Data)
    public void showData(View view) {
        Query<DataBean> query = mDao.queryBuilder()
                .build();

        mDatas = query.list();
        mAdapter.addDatas(mDatas);
    }

    // MainActivity中实现两个点击方法
    // item点击监听 展示对话框
    public void onItemClick(int position, final DataBean data) {
//        Toast.makeText(this, "position: " + position, Toast.LENGTH_SHORT).show();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(data.getSubject());
        builder.setMessage(data.getSummary());
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, data.getSubject(), Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }
    // item长点击监听 携带数据跳转界面
    public void onItemLongClick(int position, DataBean data) {
//        Toast.makeText(this, "position: " + position, Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra("cover", data.getCover());
        startActivity(intent);
    }


    class NewsAdapter extends SectionedRecyclerViewAdapter<HeaderViewHolder, NewsViewHolder, FooterViewHolder> implements View.OnClickListener, View.OnLongClickListener {

        // 适配器中原始数据
        private List<DataBean> mDatas;

        // 每段落包含一个集合,用于管理每段的item
        private List<DataBean> mDataList;
        // 还得有一个集合管理所有段落
        private List<List<DataBean>> mSectionList;

        public NewsAdapter() {
            mDatas = new ArrayList<> ();
            mSectionList = new ArrayList<>();
        }

        // 添加数据
        public void addDatas(List<DataBean> datas) {
            mDatas.addAll(datas);
            // 适配器中的原始数据集合变换成新的数据格式
            changeList(mDatas);

            notifyDataSetChanged();
        }

        // 变化数据格式
        private void changeList(List<DataBean> datas) {
            for (int i = 0; i < datas.size(); i++) {
                // 每段item数据都是以偶数位开始
                if (i % 2 == 0) {
                    mDataList = new ArrayList<>();// 创建一个段落
                    mDataList.add(datas.get(i));// 每段第一个数据
                    mDataList.add(datas.get(i + 1));// 每段第二个数据
                    mSectionList.add(mDataList);// 将新创建的段加入集合中
                }
            }
        }

        @Override
        protected int getSectionCount() {
            return mDatas.size() / 2;
        }

        @Override
        protected int getItemCountForSection(int section) {
            return 2;
        }

        @Override
        protected boolean hasFooterInSection(int section) {
            return true;
        }

        @Override
        protected HeaderViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(MainActivity.this)
                    .inflate(R.layout.item_header, parent, false);
            return new HeaderViewHolder(itemView);
        }

        @Override
        protected FooterViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(MainActivity.this)
                    .inflate(R.layout.item_footer, parent, false);
            return new FooterViewHolder(itemView);
        }

        @Override
        protected NewsViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(MainActivity.this)
                    .inflate(R.layout.item_news, parent, false);

            // 给itemView绑定点击/长点击
            setItemListener(itemView);
            return new NewsViewHolder(itemView);
        }

        private void setItemListener(View itemView) {
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        protected void onBindSectionHeaderViewHolder(HeaderViewHolder holder, int section) {
            String title = "标题" + section;

            holder.bind(title);
        }

        @Override
        protected void onBindSectionFooterViewHolder(FooterViewHolder holder, int section) {

        }

        @Override
        protected void onBindItemViewHolder(NewsViewHolder holder, int section, int position) {
            // 获取section个段中的position个item
            DataBean data = mSectionList.get(section).get(position);

            holder.bind(data);
        }

        @Override
        public void onClick(View v) {
            // 获取被点击item的位置
            int position = mRecyclerView.getChildLayoutPosition(v);

            onItemClick(position, mDatas.get(position));
        }


        @Override
        public boolean onLongClick(View v) {
            int position = mRecyclerView.getChildLayoutPosition(v);

            onItemLongClick(position, mDatas.get(position));
            return true;
        }
    }

}



最终的结果也就出来了,下面是本次技能的运行效果,看看你写的对不对:





            
阅读更多
换一批

没有更多推荐了,返回首页