1.主页面XML
<?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"
tools:context=".ui.MainActivity"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/mRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="9"
></android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/additem"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="添加item"
android:layout_weight="1"
/>
<Button
android:id="@+id/delitem"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="删除item"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
2.item布局
<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="66666"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
</RelativeLayout>
3.工具包
package com.bawie.www.a20181119_a.model;
import android.os.Handler;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* date:2018/11/19
* author:别来无恙(别来无恙)
* function:
*/
public class HttpModel {
public void request(String urlString,final HttpUtilsInterface httpUtilsInterface){
final Handler handler = new Handler();
OkHttpClient Client = new OkHttpClient.Builder()
.build();
Request request = new Request.Builder().get().url(urlString).build();
Call call = Client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
httpUtilsInterface.failure();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
httpUtilsInterface.success(string);
}
});
}
});
}
public interface HttpUtilsInterface{
void success(String json);
void failure();
}
}
4.P
package com.bawie.www.a20181119_a.presenrer;
import com.bawie.www.a20181119_a.model.HttpModel;
import com.bawie.www.a20181119_a.model.bean.News;
import com.google.gson.Gson;
import java.util.ArrayList;
/**
* date:2018/11/19
* author:别来无恙(别来无恙)
* function:
*/
public class HttpPresenter {
public void requestList(String urlString,final HttpPresenterListener httpPresenterListener){
HttpModel httpModel = new HttpModel();
httpModel.request(urlString, new HttpModel.HttpUtilsInterface() {
@Override
public void success(String json) {
News news = new Gson().fromJson(json, News.class);
ArrayList<News.DataBean> data = news.getData();
httpPresenterListener.success(data);
}
@Override
public void failure() {
httpPresenterListener.error();
}
});
}
public interface HttpPresenterListener{
void success(ArrayList<News.DataBean> data);
void error();
}
}
5. bean
package com.bawie.www.a20181119_a.model.bean;
import java.util.ArrayList;
/**
* date:2018/11/19
* author:别来无恙(别来无恙)
* function:
*/
public class News {
public ArrayList<DataBean> data;
public ArrayList<DataBean> getData() {
return data;
}
public static class DataBean{
private String title;
private String thumbnail_pic_s;
public String getTitle() {
return title;
}
public String getThumbnail_pic_s() {
return thumbnail_pic_s;
}
}
}
5.adapter
package com.bawie.www.a20181119_a.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.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import com.bawie.www.a20181119_a.R;
import com.bawie.www.a20181119_a.model.bean.News;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* date:2018/11/19
* author:别来无恙(别来无恙)
* function:
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHodler> {
private ArrayList<News .DataBean> list;
private Context mContext;
public RecyclerAdapter(ArrayList<News.DataBean> list, Context context) {
this.list = list;
mContext = context;
}
@NonNull
@Override
public ViewHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = View.inflate(mContext, R.layout.item, null);
ViewHodler viewHodler = new ViewHodler(inflate);
return viewHodler;
}
@Override
public void onBindViewHolder(@NonNull ViewHodler viewHodler,final int i) {
Picasso.with(mContext).load(list.get(i).getThumbnail_pic_s()).into(viewHodler.mImage);
viewHodler.title.setText(list.get(i).getTitle());
viewHodler.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recyclerAdapterListener.click(list.get(i).getTitle());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHodler extends RecyclerView.ViewHolder {
private final ImageView mImage;
private final TextView title;
public ViewHodler(@NonNull View itemView) {
super(itemView);
mImage = itemView.findViewById(R.id.image);
title = itemView.findViewById(R.id.title);
}
}
public void add(int i){
list.add(i,list.get(0));
notifyItemInserted(i);
notifyDataSetChanged();
}
public void del(int i){
list.remove(i);
notifyItemRemoved(i);
notifyDataSetChanged();
}
public interface RecyclerAdapterListener{
void click(String data);
}
private RecyclerAdapterListener recyclerAdapterListener;
public void setRecyclerAdapterListener(RecyclerAdapterListener recyclerAdapterListeners){
recyclerAdapterListener = recyclerAdapterListeners;
}
}
Main
package com.bawie.www.a20181119_a.ui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.bawie.www.a20181119_a.R;
import com.bawie.www.a20181119_a.adapter.RecyclerAdapter;
import com.bawie.www.a20181119_a.model.bean.News;
import com.bawie.www.a20181119_a.presenrer.HttpPresenter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public static final String Path = "http://www.xieast.com/api/news/news.php";
private RecyclerView mMRecyclerView;
private Button mAdditem;
private Button mDelitem;
private RecyclerAdapter mRecyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化视图
initView();
//初始化数据
initList();
mAdditem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
additem();
}
});
mDelitem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delitem();
}
});
}
private void additem() {
mRecyclerAdapter.add(0);
}
private void delitem() {
mRecyclerAdapter.del(0);
}
private void initList() {
mMRecyclerView.setLayoutManager(new LinearLayoutManager(this));
HttpPresenter httpPresenter = new HttpPresenter();
httpPresenter.requestList(Path, new HttpPresenter.HttpPresenterListener() {
@Override
public void success(ArrayList<News.DataBean> data) {
mRecyclerAdapter = new RecyclerAdapter(data, MainActivity.this);
mMRecyclerView.setAdapter(mRecyclerAdapter);
mRecyclerAdapter.setRecyclerAdapterListener(new RecyclerAdapter.RecyclerAdapterListener() {
@Override
public void click(String data) {
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void error() {
Toast.makeText(MainActivity.this, "请求数据失败", Toast.LENGTH_SHORT).show();
}
});
}
private void initView() {
mMRecyclerView = findViewById(R.id.mRecyclerView);
mAdditem = findViewById(R.id.additem);
mDelitem = findViewById(R.id.delitem);
}
}
注意:
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.picasso:picasso:2.3.2'
<uses-permission android:name="android.permission.INTERNET"/>