首先导入依赖
//Recyclerview是listview的升级版
implementation 'com.android.support:recyclerview-v7:28.0.0'
//okhttp
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
//请求网络图片
implementation 'com.github.bumptech.glide:glide:3.7.0'
//解析数据
implementation 'com.google.code.gson:gson:2.2.4'
//别忘添加权限
<uses-permission android:name="android.permission.INTERNET"/>
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除"
/>
<Button
android:id="@+id/btn3"
android:text="list"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn4"
android:text="grid"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn5"
android:text="flow"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/review"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
<?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="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="5dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:text="Content"
android:textAllCaps="false"
/>
</LinearLayout>
</RelativeLayout>
适配器
package com.example.day13;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.day13.bean.Person;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
Context mContext;
List<Person.ResultBean> list;
public MyAdapter(Context context) {
this.mContext = context;
list = new ArrayList<>();
}
//把解析出来的数据进行添加
public void setData(List<Person.ResultBean> dataBeans) {
for (int i = 0; i < dataBeans.size(); i++) {
list.add(dataBeans.get(i));
}
//更新
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(mContext,R.layout.item,null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.tv.setText(list.get(i).getName());
Glide.with(mContext).load(list.get(i).getImageUrl()).into(viewHolder.img);
}
@Override
public int getItemCount() {
return list.size();
}
//删除数据
public void delete(int i) {
if(list.size()>0){
list.remove(i);
notifyDataSetChanged();
}else{
Toast.makeText(mContext,"没有更多数据",Toast.LENGTH_SHORT).show();
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView img;
TextView tv;
public ViewHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
tv = itemView.findViewById(R.id.tv);
}
}
}
//单独封装一个网络请求类
package com.example.day13.http;
import android.content.Context;
import android.widget.Toast;
import com.example.day13.bean.Person;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Http {
Context mContext;
public void getData(String url){
final OkHttpClient okhttp = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
//构造一个Request对象
Request request = new Request.Builder().url(url).build();
//通过request的对象去构造得到一个Call对象
Call call = okhttp.newCall(request);
//调用的是call.enqueue,将call加入调度队列,然后等待任务执行完成,我们在Callback中即可得到结果。
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Toast.makeText(mContext,"失败噜噜噜噜",Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String s = response.body().string();
Gson gson = new Gson();
Person ladyBean = gson.fromJson(s, Person.class);
mOnhttp.getlist(ladyBean.getResult());
}
});
}
public interface Onhttp{
void getlist(List<Person.ResultBean> list);
}
Onhttp mOnhttp;
public void setOnhttp(Onhttp Onhttp){
this.mOnhttp = Onhttp;
}
}
package com.example.day13;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.day13.bean.Person;
import com.example.day13.http.Http;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener ,Http.Onhttp {
private Button delete;
private RecyclerView review;
List<Person.ResultBean> list = new ArrayList<>();
MyAdapter adapter;
private Button btn3;
private Button btn4;
private Button btn5;
Http http;
int page = 1;
private String url = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?page="+page+"&&count=10";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
adapter = new MyAdapter(this);
review.setAdapter(adapter);
}
private void initDate() {
http.getData(url);
http.setOnhttp(this);
}
private void initView() {
http = new Http();
delete = (Button) findViewById(R.id.delete);
review = (RecyclerView) findViewById(R.id.review);
delete.setOnClickListener(this);
btn3 = (Button) findViewById(R.id.btn3);
btn3.setOnClickListener(this);
btn4 = (Button) findViewById(R.id.btn4);
btn4.setOnClickListener(this);
btn5 = (Button) findViewById(R.id.btn5);
btn5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
break;
case R.id.delete:
adapter.delete(0);
break;
case R.id.btn3:
//listview视图
// Toast.makeText(MainActivity.this,"少时诵诗书",Toast.LENGTH_SHORT).show();
LinearLayoutManager maager = new LinearLayoutManager(this);
review.setLayoutManager(maager);
initDate();
break;
case R.id.btn4:
//网络视图
GridLayoutManager manager2 = new GridLayoutManager(this,4);
review.setLayoutManager(manager2);
break;
case R.id.btn5:
//瀑布视图
StaggeredGridLayoutManager manager1 = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); review.setLayoutManager(manager1);
break;
}
}
@Override
public void getlist(final List<Person.ResultBean> list) {
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.setData(list);
}
});
}
}