加载assets下的json串并用recyclerview显示

1.添加依赖

compile 'com.google.code.gson:gson:2.8.2'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.facebook.fresco:fresco:0.11.0'
compile 'com.facebook.fresco:animated-gif:0.12.0'
2.添加权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

3创建MyApplication继承Application

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}
4. HomeAdapter适配器

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>{
    Context context;
    List<Supperclass.NewslistBean> list;

    public HomeAdapter(Context context, List<Supperclass.NewslistBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public HomeAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_home,parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(HomeAdapter.MyViewHolder holder, int position) {
        holder.tv.setText(list.get(position).getTitle());
        Uri uri =  Uri.parse(list.get(position).getPicUrl());
        holder.sdv.setImageURI(uri);
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder
    {

        TextView tv;
        SimpleDraweeView sdv;
        public MyViewHolder(View view)
        {
            super(view);
            tv = (TextView) view.findViewById(R.id.id_num);
            sdv = (SimpleDraweeView) view.findViewById(R.id.main_simple_drawee_view);
        }
    }
}

5.mainactivity中读取本地json串并添加适配器

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private HomeAdapter mAdapter;
    List<Supperclass.NewslistBean> newslist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        InputStream inputStream = null;
        try {
             inputStream = getAssets().open("zzzz.json");
            String data = convertStreamToString(inputStream);
            Gson gson = new Gson();

            Supperclass supperclass = gson.fromJson(data, Supperclass.class);
                 newslist = supperclass.getNewslist();
                mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                mRecyclerView.setAdapter(mAdapter = new HomeAdapter(MainActivity.this,newslist));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public  String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

6.bean类

public class Supperclass {

    /**
     * code : 200
     * msg : success
     * newslist : [{"ctime":"2017-06-06 15:00","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/small8681bec69f9c1d6a43ef920082d04bf21496647674.jpg","title":"真是很少见!日本一家银行只存姑娘不存钱","url":"http://www.luanbian.com//lishi/yiyu/25329.html"},{"ctime":"2017-06-06 14:08","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/c7b6e163ab0cd2e73d73fa22d269cceb.jpg","title":"一曲成名红过刀郎,花200元赚得一亿利润!","url":"http://www.luanbian.com//lishi/yiyu/25331.html"},{"ctime":"2017-06-06 15:00","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/5da421cae36b825d7c19e00522c78654.jpg","title":"香港四大天王谁居首?年轻时张学友颜值最低,老了却成功逆袭","url":"http://www.luanbian.com//lishi/yiyu/25332.html"},{"ctime":"2017-06-06 14:14","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-06/smalle2a960a265d59f8786c9e2f713266dc91496717348.jpg","title":"安以轩嫁入千亿豪门,大S微博发酸这样说","url":"http://www.luanbian.com//lishi/yiyu/25333.html"},{"ctime":"2017-06-06 15:00","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-06/smalla64e8251141b3ca2ac158247952993721496719684.jpg","title":"\u201c国际明星\u201d英达父亲去世,不让亲儿子巴图参加","url":"http://www.luanbian.com//lishi/yiyu/25334.html"},{"ctime":"2017-06-06 14:39","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-06/small0ba83f51849d3c9fbd73cc99fa2017a11496721103.jpg","title":"村中寻一座明代古石桥,竟被告知在墙上?","url":"http://www.luanbian.com//lishi/yiyu/25335.html"},{"ctime":"2017-06-06 15:00","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-06/small7c1f47cd2a87c73bcf9ab9352e5d25fd1496729645.jpg","title":"流浪狗叼着刚出生的弃婴向路人求救,感动落泪","url":"http://www.luanbian.com//lishi/yiyu/25336.html"},{"ctime":"2017-06-06 14:41","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-02/fba8bd9a885bc13cfcfc3454f3d0889f.jpg","title":"入赘豪门做女婿的8大男星,刘德华周润发均在列","url":"http://www.luanbian.com//lishi/yiyu/25319.html"},{"ctime":"2017-06-06 15:00","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/d85c3437f009bee056a06a8a7bcd0fb9.jpg","title":"破洞牛仔裤隐藏巨大的危险,工人付出惨痛代价","url":"http://www.luanbian.com//lishi/yiyu/25328.html"},{"ctime":"2017-06-06 15:00","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/small535838c27374b6be43a7ec865d2974281496650726.jpg","title":"一条鱼抵一辆保时捷 标价268万元惊呆了网友","url":"http://www.luanbian.com//lishi/yiyu/25330.html"}]
     */

    private int code;
    private String msg;
    private List<NewslistBean> newslist;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<NewslistBean> getNewslist() {
        return newslist;
    }

    public void setNewslist(List<NewslistBean> newslist) {
        this.newslist = newslist;
    }

    public static class NewslistBean {
        /**
         * ctime : 2017-06-06 15:00
         * description : 明星花边
         * picUrl : http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/small8681bec69f9c1d6a43ef920082d04bf21496647674.jpg
         * title : 真是很少见!日本一家银行只存姑娘不存钱
         * url : http://www.luanbian.com//lishi/yiyu/25329.html
         */

        private String ctime;
        private String description;
        private String picUrl;
        private String title;
        private String url;

        public String getCtime() {
            return ctime;
        }

        public void setCtime(String ctime) {
            this.ctime = ctime;
        }

        public String getDescription() {
            return description;
        }

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

        public String getPicUrl() {
            return picUrl;
        }

        public void setPicUrl(String picUrl) {
            this.picUrl = picUrl;
        }

        public String getTitle() {
            return title;
        }

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

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}

7.布局

activity_main布局

<android.support.v7.widget.RecyclerView
    android:id="@+id/id_recyclerview"
    android:divider="#ffff0000"
    android:dividerHeight="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

item_home布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:fresco="http://schemas.android.com/apk/res-auto">
    <TextView
        android:id="@+id/id_num"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="1" />
    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/main_simple_drawee_view"
        fresco:placeholderImage="@mipmap/ic_launcher"/>
</LinearLayout>



最后附上json串,zzzz.json

{"code":200,"msg":"success","newslist":[{"ctime":"2017-06-06 15:00","title":"真是很少见!日本一家银行只存姑娘不存钱","description":"明星花边","picUrl":"http:\/\/www.luanbian.com\/d\/file\/lishi\/yiyu\/2017-06-05\/small8681bec69f9c1d6a43ef920082d04bf21496647674.jpg","url":"http:\/\/www.luanbian.com\/\/lishi\/yiyu\/25329.html"},{"ctime":"2017-06-06 14:08","title":"一曲成名红过刀郎,花200元赚得一亿利润!","description":"明星花边","picUrl":"http:\/\/www.luanbian.com\/d\/file\/lishi\/yiyu\/2017-06-05\/c7b6e163ab0cd2e73d73fa22d269cceb.jpg","url":"http:\/\/www.luanbian.com\/\/lishi\/yiyu\/25331.html"},{"ctime":"2017-06-06 15:00","title":"香港四大天王谁居首?年轻时张学友颜值最低,老了却成功逆袭","description":"明星花边","picUrl":"http:\/\/www.luanbian.com\/d\/file\/lishi\/yiyu\/2017-06-05\/5da421cae36b825d7c19e00522c78654.jpg","url":"http:\/\/www.luanbian.com\/\/lishi\/yiyu\/25332.html"},{"ctime":"2017-06-06 14:14","title":"安以轩嫁入千亿豪门,大S微博发酸这样说","description":"明星花边","picUrl":"http:\/\/www.luanbian.com\/d\/file\/lishi\/yiyu\/2017-06-06\/smalle2a960a265d59f8786c9e2f713266dc91496717348.jpg","url":"http:\/\/www.luanbian.com\/\/lishi\/yiyu\/25333.html"},{"ctime":"2017-06-06 15:00","title":"“国际明星”英达父亲去世,不让亲儿子巴图参加","description":"明星花边","picUrl":"http:\/\/www.luanbian.com\/d\/file\/lishi\/yiyu\/2017-06-06\/smalla64e8251141b3ca2ac158247952993721496719684.jpg","url":"http:\/\/www.luanbian.com\/\/lishi\/yiyu\/25334.html"},{"ctime":"2017-06-06 14:39","title":"村中寻一座明代古石桥,竟被告知在墙上?","description":"明星花边","picUrl":"http:\/\/www.luanbian.com\/d\/file\/lishi\/yiyu\/2017-06-06\/small0ba83f51849d3c9fbd73cc99fa2017a11496721103.jpg","url":"http:\/\/www.luanbian.com\/\/lishi\/yiyu\/25335.html"},{"ctime":"2017-06-06 15:00","title":"流浪狗叼着刚出生的弃婴向路人求救,感动落泪","description":"明星花边","picUrl":"http:\/\/www.luanbian.com\/d\/file\/lishi\/yiyu\/2017-06-06\/small7c1f47cd2a87c73bcf9ab9352e5d25fd1496729645.jpg","url":"http:\/\/www.luanbian.com\/\/lishi\/yiyu\/25336.html"},{"ctime":"2017-06-06 14:41","title":"入赘豪门做女婿的8大男星,刘德华周润发均在列","description":"明星花边","picUrl":"http:\/\/www.luanbian.com\/d\/file\/lishi\/yiyu\/2017-06-02\/fba8bd9a885bc13cfcfc3454f3d0889f.jpg","url":"http:\/\/www.luanbian.com\/\/lishi\/yiyu\/25319.html"},{"ctime":"2017-06-06 15:00","title":"破洞牛仔裤隐藏巨大的危险,工人付出惨痛代价","description":"明星花边","picUrl":"http:\/\/www.luanbian.com\/d\/file\/lishi\/yiyu\/2017-06-05\/d85c3437f009bee056a06a8a7bcd0fb9.jpg","url":"http:\/\/www.luanbian.com\/\/lishi\/yiyu\/25328.html"},{"ctime":"2017-06-06 15:00","title":"一条鱼抵一辆保时捷 标价268万元惊呆了网友","description":"明星花边","picUrl":"http:\/\/www.luanbian.com\/d\/file\/lishi\/yiyu\/2017-06-05\/small535838c27374b6be43a7ec865d2974281496650726.jpg","url":"http:\/\/www.luanbian.com\/\/lishi\/yiyu\/25330.html"}]}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值