第一步
导入volley框架,以便进行网络请求,并添加依赖
第二步
添加网络权限,在AndroidManifest.xml中添加
<uses-permission android:name="android.permission.INTERNET" />
第三步
编写请求数据代码
URL可以自己填写
在JsonObjectRequest中的onResponse,它的参数response就是json数据了。
至此,通过网络请求到数据,下面写RecyclerView
第一步,导入支持包
在build.gardle文件中添加
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:recyclerview-v7:23.1.1'
(Android Studio)
第二步
写布局
在主布局中加入recyclerView
<android.support.v7.widget.RecyclerView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/recycler_view" />
再写item布局
第三步
编写Adapter和ViewHolder
public class RecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerViewAdapter.myViewHolder>{ private List<T> mDatas; //构造函数 public RecyclerViewAdapter(List<T> dataSet){ mDatas=dataSet; } @Override public myViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { myViewHolder holder=new myViewHolder(View.inflate(viewGroup.getContext(), R.layout.itemlayout, null)); return holder; } //绑定数据 //将数据模型的title和publishtime赋给两个TextView显示 @Override public void onBindViewHolder(myViewHolder holder, int i) { DataModel.data dataModel=(DataModel.data)mDatas.get(i); holder.tv.setText(dataModel.getTitle()); holder.tv02.setText(dataModel.getPublishtime()); } @Override public int getItemCount() { return mDatas.size(); } //ViewHolder类 public static class myViewHolder extends RecyclerView.ViewHolder{ TextView tv; TextView tv02; public myViewHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.recycler_item1_title); tv02=(TextView)itemView.findViewById(R.id.recycler_item2_author); } } }
此时在onCreate中需要的全局变量有
private RecyclerView recyclerView; private Context context; private RequestQueue mQueue;
同时Mainactivity要继承
AppCompatActivity
写到这里,下面需要写model,要根据json的格式写,可以在oncreate中写
context = getApplicationContext(); //这个一定顺序一定要对啊 initRequestQueue(); getDataByVolley();
然后在getDataByVolley中loge一下response到控制台,查看数据
第四步
写DataModel
public class DataModel { /*{ "data": [ { "createtime": "2016-01-12 16:00:07", "summary": "", "pageurl": "http:\/\/123.59.65.71\/lczx\/index.php\/news\/show\/qWPLivao", "id": "120447", "title": "“止不住”的油价逼近30美元大关 数个OPEC成员国要求开紧急会议", "thumbnail": "http:\/\/posts.cdn.wallstcn.com\/c3\/c7\/7a\/cfp470821056.jpg!index-news-cover", "source": "8", "publishtime": "2016-01-12 15:52:28", "sourcename": "华尔街见闻", "comments": "6" }, { "createtime": "2016-01-12 16:00:09", "summary": "", "pageurl": "http:\/\/123.59.65.71\/lczx\/index.php\/news\/show\/5AGuPKJm", "id": "120472", "title": "险守3000点沪指微涨0.2% 创业板涨2%", "thumbnail": "http:\/\/posts.cdn.wallstcn.com\/58\/51\/fd\/bigstock-stock-market-trading-43526122.jpg!index-news-cover", "source": "8", "publishtime": "2016-01-12 15:50:39", "sourcename": "华尔街见闻", "comments": "0" },*/ private List<data> data; public void setData(List<DataModel.data> data) { this.data = data; } public List<DataModel.data> getData() { return data; } public static class data { public String createtime; public String summary; public String pageurl; public String id; public String title; public String thumbnail; public String source; public String publishtime; public String sourcename; public String comments; public void setId(String id) { this.id = id; } public String getId() { return id; } public void setTitle(String title) { this.title = title; } public String getTitle() { return title; } public void setPublishtime(String publishtime) { this.publishtime = publishtime; } public String getPublishtime() { return publishtime; } @Override public String toString() { return "Person [id=" + id + ", title=" + title + ", publishtime=" + publishtime + "]"; } } }
然后在MainActivity中写
recyclerView=(RecyclerView)findViewById(R.id.recycler_view); //使recyclerview保持固定的大小 recyclerView.setHasFixedSize(true); //设置布局管理器,实现横向或数值滚动的列表布局 RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(context); recyclerView.setLayoutManager(layoutManager); //recyclerView的adapter,adapter要为全局的 mAdapter=new RecyclerViewAdapter(mDatas); recyclerView.setAdapter(mAdapter);
在输出json数据的地方解析数据并通知适配器更换数据
目前只是显示出来,没有好看的样子。