首先导入jar包
project 的 repositories 添加一句 maven { url 'https://jitpack.io' }
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
Module 的
//第一个是recyclerview的jar包
implementation 'com.android.support:recyclerview-v7:28.0.0-alpha1'
//OKhttp的包
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
//gson解析
implementation 'com.google.code.gson:gson:2.8.5'
//glide 图片处理
implementation 'com.github.bumptech.glide:glide:4.8.0'
//第三方的一个瀑布流
implementation 'com.github.1002326270xc:LayoutManager-FlowLayout:v1.6'
第二步 添加权限,因为要网络请求
<uses-permission android:name="android.permission.INTERNET"/>
Main的布局
<?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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/add"
android:text="添加"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/delete"
android:text="删除"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/list"
android:text="list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/grid"
android:text="grid"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/pbl"
android:text="瀑布流"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>```
MainActivity
package com.example.recyclerview;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
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.recyclerview.adapter.RecyclerViewAdapter;
import com.example.recyclerview.bean.Bean;
import com.example.recyclerview.net.OKHTTP;
import com.library.flowlayout.FlowLayoutManager;
import com.library.flowlayout.SpaceItemDecoration;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, OKHTTP.onLoadData {
private Button add;
private Button delete;
private Button list;
private Button grid;
private Button pbl;
private RecyclerView recyclerview;
private OKHTTP mOKHTTP;
private RecyclerViewAdapter mRecyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//实例化网络类对象
mOKHTTP = new OKHTTP(this);
//调用http里面的网络请求
mOKHTTP.getHttp();
//实例化适配器
mRecyclerViewAdapter = new RecyclerViewAdapter(this);
//把适配器设置给recyclerview
recyclerview.setAdapter(mRecyclerViewAdapter);
//布局管理器 默认布局listview 线性布局
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerview.setLayoutManager(linearLayoutManager);
// recyclerview.addItemDecoration(new DividerItemDecoration());
//添加分割线
DividerItemDecoration divi = new DividerItemDecoration(this,1);
divi.setDrawable(ContextCompat.getDrawable(this,R.drawable.shape));
recyclerview.addItemDecoration(divi);
//点击时间的监听
mRecyclerViewAdapter.setOnClickListener(new RecyclerViewAdapter.OnClickListener() {
@Override
public void onClick(View view, int position) {
Toast.makeText(MainActivity.this, mRecyclerViewAdapter.list.get(position).getName()+"",
Toast.LENGTH_SHORT).show();
Log.e("zhx",mRecyclerViewAdapter.list.get(position).getName()+"");
}
});
//长按事件的监听
mRecyclerViewAdapter.setOnLongClickListener(new RecyclerViewAdapter.OnLongClickListener() {
@Override
public void onlongClick(View view, int position) {
Log.e("zhx",mRecyclerViewAdapter.list.get(position).getId()+"");
}
});
}
private void initView() {
add = findViewById(R.id.add);
delete = findViewById(R.id.delete);
list = findViewById(R.id.list);
grid = findViewById(R.id.grid);
pbl = findViewById(R.id.pbl);
recyclerview = findViewById(R.id.recyclerview);
add.setOnClickListener(this);
delete.setOnClickListener(this);
list.setOnClickListener(this);
grid.setOnClickListener(this);
pbl.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
//调用添加方法
mRecyclerViewAdapter.addData();
break;
case R.id.delete:
//删除方法
mRecyclerViewAdapter.delete(mRecyclerViewAdapter.getItemCount());
break;
case R.id.list:
//线性布局
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerview.setLayoutManager(linearLayoutManager);
break;
case R.id.grid:
//网格布局
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
recyclerview.setLayoutManager(gridLayoutManager);
break;
case R.id.pbl:
//瀑布流
//StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
recyclerview.addItemDecoration(new SpaceItemDecoration(1));
recyclerview.setLayoutManager(flowLayoutManager);
break;
}
}
@Override
public void success(final List<Bean.DataBean> data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//把返回的数据设置给Adapter
mRecyclerViewAdapter.setData(data);
}
});
}
@Override
public void failure() {
runOnUiThread(new Runnable() {
@Override
public void run() {
//吐司提示没有网络
Toast.makeText(MainActivity.this, "没有网络", Toast.LENGTH_SHORT).show();
}
});
}
}
网络请求的实体类
package com.example.recyclerview.bean;
import java.util.List;
/**
* date:2018/11/14
* author:
* function:
*/
public class Bean {
private int status;
private String message;
private List<DataBean> data;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private int id;
private String name;
private String pic_url;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPic_url() {
return pic_url;
}
public void setPic_url(String pic_url) {
this.pic_url = pic_url;
}
}
}
OkHttp
package com.example.recyclerview.net;
import com.example.recyclerview.bean.Bean;
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;
/**
* date:2018/11/14
* author:
* function:
*/
public class OKHTTP {
String path="http://www.wanandroid.com/tools/mockapi/6523/restaurants_offset_0_limit_4";
public void getHttp(){
//创建OK的对象
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
//创建request对象
final Request request = new Request.Builder()
.url(path)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//失败调用接口
mOnLoadData.failure();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//成功解析成对象,调用接口把集合返回
String ss = response.body().string();
Bean bean = new Gson().fromJson(ss,Bean.class);
List<Bean.DataBean> data = bean.getData();
mOnLoadData.success(data);
}
});
}
//数据的获取接口
public interface onLoadData{
void success(List<Bean.DataBean> data);
void failure();
}
private onLoadData mOnLoadData;
public OKHTTP(onLoadData onLoadData) {
mOnLoadData = onLoadData;
}
}
Adapter
package com.example.recyclerview.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.recyclerview.R;
import com.example.recyclerview.bean.Bean;
import java.util.ArrayList;
import java.util.List;
/**
* date:2018/11/14
* author:
* function:
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
Context context;
public List<Bean.DataBean> list;
//有参构造
public RecyclerViewAdapter(Context context) {
this.context = context;
list = new ArrayList<>();
}
//设置数据的方法
public void setData(List<Bean.DataBean> list){
this.list.clear();
this.list.addAll(list);
notifyDataSetChanged();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//获取布局
View inflate = View.inflate(context, R.layout.item_v, null);
//实例化ViewHolder
MyViewHolder myViewHolder = new MyViewHolder(inflate);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
holder.text.setText(list.get(position).getName());
//用glide设置图片
Glide.with(context).load(list.get(position).getPic_url()).into(holder.img);
//点击事件
holder.line.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mOnClickListener.onClick(view,position);
}
});
//长按事件
holder.line.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mOnLongClickListener.onlongClick(view,position);
return true;
}
});
}
@Override
public int getItemCount() {
return list.size();
}
//添加方法
public void addData() {
Bean.DataBean dataBean = new Bean.DataBean();
dataBean.setName("");
dataBean.setPic_url("http://p0.meituan.net/xianfu/fdd295e7650587269e0f1d7c35ba180c114177.jpg");
list.add(dataBean);
notifyDataSetChanged();
}
//删除方法,从最后一个开始删
public void delete(int itemCount) {
if (itemCount == 1){
Toast.makeText(context, "只剩下一条数据了", Toast.LENGTH_SHORT).show();
}else{
list.remove(itemCount-1);
notifyDataSetChanged();
}
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView img;
TextView text;
RelativeLayout line;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
text = itemView.findViewById(R.id.text);
line = itemView.findViewById(R.id.line);
}
}
//点击事件的
public interface OnClickListener{
void onClick(View view,int position);
}
private OnClickListener mOnClickListener;
public void setOnClickListener(OnClickListener onClickListener){
mOnClickListener = onClickListener;
}
//长按事件
public interface OnLongClickListener{
void onlongClick(View view,int position);
}
private OnLongClickListener mOnLongClickListener;
public void setOnLongClickListener( OnLongClickListener onLongClickListener){
mOnLongClickListener = onLongClickListener;
}
}
条目布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/line"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img"
android:text="text" />
</RelativeLayout>
shape文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffff0000"
android:centerColor="#ff00ff"
android:endColor="#fff000"
android:type="linear"/>
<size android:height="3dp"/>
</shape>