您的位置:首页 > 理论基础 > 计算机网络

okhttp + RecycleView + Glide 请求网络数据及图片

2017-09-20 14:14 375 查看


写在前面:很久没有更新博客了,今天更新这篇呢,旨在将知识点稍加糅合一下,综合运用

首先我们需要导入的依赖:
Recyclerview的依赖
compile 'com.android.support:recyclerview-v7:21.0.0'
Glide加载图片的依赖
compile 'com.github.bumptech.glide:glide:3.7.0'
Okhttp请求网络数据的依赖
compile 'net.qiujuer.common:okhttp:3.0.0'Gson解析数据
compile 'com.google.code.gson:gson:2.8.1'


XML布局:


MainActivity 布局:

<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.gaohuiquan20170918demo.MainActivity">

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

</RelativeLayout>


Item布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/iv"
android:layout_width="120dp"
android:layout_height="120dp"/>
<TextView
android:id="@+id/texts"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"

d161
android:textSize="20sp" />
</LinearLayout>

Recyclerview的Adapte适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder>{

private Context context;
private List<TestBean.StoriesBean> mBeanList;

public MyAdapter(Context context, List<TestBean.StoriesBean> BeanList) {
this.context = context;
this.mBeanList = BeanList;
}

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyHolder holder = new MyHolder(LayoutInflater.from(context).inflate(R.layout.item, parent, false));
return holder;
}

@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.texts.setText(mBeanList.get(position).title);
Glide.with(context).load(mBeanList.get(position).images.get(0)).into(holder.iv);
}

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

class MyHolder extends RecyclerView.ViewHolder {

TextView texts;
ImageView iv;

public MyHolder(View itemView) {
super(itemView);
texts = (TextView) itemView.findViewById(R.id.texts);
iv = (ImageView) itemView.findViewById(R.id.iv);
}

}
}

MainActivity :

public class MainActivity extends AppCompatActivity {

private String path = "https://news-at.zhihu.com/api/4/news/latest";
private RecyclerView recyclrview;
private MyAdapter myAdapter;
private List<TestBean.StoriesBean> mList = new ArrayList<>();
private Handler mHandler = new Handler();

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

initView();
setokhttp();
}

private void setokhttp() {

OkHttpClient client = new OkHttpClient();
Request request=new Request.Builder().url(path).build();
Call call=client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {

String result=response.body().string();
TestBean bean = TestBean.objectFromData(result);

mList = bean.stories;

mHandler.post(new Runnable() {
@Override
public void run() {
recyclrview.setAdapter(new MyAdapter(MainActivity.this,mList));
}
});
}
});

}

private void initView() {
recyclrview = (RecyclerView) findViewById(R.id.recyclerview);
recyclrview.setLayoutManager(new LinearLayoutManager(this));
}
}


Bean 类根据实际需求自己写

后记:看着来(!!!!!!)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  RecyclerView Okhttp
相关文章推荐