https://www.zhaoapi.cn/quarter/getJokes?source=android&appVersion=101&page=1
新添加依赖:
//okHttp 2个
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.squareup.okio:okio:1.11.0'
//recycleview
compile 'com.android.support:recyclerview-v7:27.+'
//Fresco,无论使用哪个模块的功能,都必须要添加的基础依赖
compile 'com.facebook.fresco:fresco:0.14.1'
//butterknife 黄油刀
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
//rxjava
compile 'io.reactivex.rxjava2:rxjava:2.0.7'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
//retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
//一个刷新的依赖
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1'
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Api
public class Api {
// https://www.zhaoapi.cn/quarter/getJokes?source=android&appVersion=101&page=1
public static final String BASE_API = "https://www.zhaoapi.cn/";
public static final String DUANZI_API = "quarter/getJokes";
}
ApiService
public interface ApiService {
@GET
Observable<ResponseBody> doGet(@Url String url, @QueryMap Map<String, String> map);
}
RetrofitHelper
public class RetrofitHelper {
public static OkHttpClient okHttpClient;
public static ApiService apiService;
static {
getOkHttpClient();
}
private static OkHttpClient getOkHttpClient() {
if (okHttpClient == null){
synchronized (OkHttpClient.class){
if (okHttpClient == null){
File file = new File(Environment.getExternalStorageDirectory(),"cahce");
long fileSize = 10*1024*1024;
okHttpClient = new OkHttpClient.Builder()
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15,TimeUnit.SECONDS)
.connectTimeout(15,TimeUnit.SECONDS)
.cache(new Cache(file,fileSize))
.build();
}
}
}
return okHttpClient;
}
public static ApiService getApiService(String url){
if (apiService == null){
synchronized (OkHttpClient.class){
apiService = createApiService(ApiService.class,url);
}
}
return apiService;
}
private static <T>T createApiService(Class<T> tClass, String url) {
T t = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
.create(tClass);
return t;
}
}
DuanZiBean
Bean类,网址:https://www.zhaoapi.cn/quarter/getJokes?source=android&appVersion=101&page=1
IDuanZiP
public interface IDuanZiP {
void onSuccess(ResponseBody responseBody);
}
IDuanZiView
public interface IDuanZiView {
void onSuccess(ResponseBody responseBody);
}
DuanZiModel
public class DuanZiModel {
private IDuanZiP iDuanZiP;
public DuanZiModel(IDuanZiP iDuanZiP){
this.iDuanZiP = iDuanZiP;
}
public void getData(String url,int page){
Map<String,String> parmars = new HashMap<>();
parmars.put("source","android");
parmars.put("appVersion","101");
parmars.put("page", String.valueOf(page));
RetrofitHelper.getApiService(Api.BASE_API).doGet(url,parmars)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseBody responseBody) {
iDuanZiP.onSuccess(responseBody);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
DuanZiPresenter
public class DuanZiPresenter implements IDuanZiP {
private DuanZiModel duanZiModel;
private IDuanZiView iDuanZiView;
public DuanZiPresenter(){
duanZiModel = new DuanZiModel(this);
}
public void attachView(IDuanZiView iDuanZiView){
this.iDuanZiView = iDuanZiView;
}
public void dettachView(){
if (iDuanZiView != null){
iDuanZiView = null;
}
}
public void getData(String url,int page){
duanZiModel.getData(url,page);
}
@Override
public void onSuccess(ResponseBody responseBody) {
iDuanZiView.onSuccess(responseBody);
}
}
MyApplication
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化Fresco
Fresco.initialize(this);
}
}
权限+name
MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<DuanZiBean.DataBean> list;
private PopupWindow popupWindow;
private int mShowMorePopupWindowWidth;
private int mShowMorePopupWindowHeight;
public MyAdapter(Context context, List<DuanZiBean.DataBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ViewHolder holder;
View view = View.inflate(context, R.layout.recycle_item, null);
holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.tv1.setText(list.get(position).getUser().getNickname());
holder.tv2.setText(list.get(position).getCreateTime());
holder.tv3.setText(list.get(position).getContent());
String icon = (String) list.get(position).getUser().getIcon();
holder.touXiang.setImageURI(icon);
//点击弹出框
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showMore(view);
}
});
}
/**
* 点击弹出框popupwindow
*
* @param view
*/
private void showMore(View view) {
if (popupWindow == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View content = li.inflate(R.layout.layout_more, null, false);
popupWindow = new PopupWindow(content, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
content.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
mShowMorePopupWindowWidth = content.getMeasuredWidth();
mShowMorePopupWindowHeight = content.getMeasuredHeight();
final View parent = popupWindow.getContentView();
ImageView dianzan = parent.findViewById(R.id.dianzan_item);
ImageView pinglun = parent.findViewById(R.id.pinglun_item);
ImageView zhaunfa = parent.findViewById(R.id.zhaunfa_item);
// 点赞的监听器
dianzan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("点赞");
alert.setNegativeButton("取消", null);
alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
popupWindow.dismiss();
}
});
alert.show();
}
});
// 评论的监听器
pinglun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("评论");
alert.setNegativeButton("取消", null);
alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
popupWindow.dismiss();
}
});
alert.show();
}
});
// 转发的监听器
zhaunfa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("转发");
alert.setNegativeButton("取消", null);
alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
popupWindow.dismiss();
}
});
alert.show();
}
});
}
if (popupWindow.isShowing()) {
popupWindow.dismiss();
} else {
int heightMoreBtnView = view.getHeight();
popupWindow.showAsDropDown(view, -mShowMorePopupWindowWidth,
-(mShowMorePopupWindowHeight + heightMoreBtnView) / 2);
}
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
SimpleDraweeView touXiang;
TextView tv1;
TextView tv2;
TextView tv3;
ImageButton btn;
public ViewHolder(View itemView) {
super(itemView);
touXiang = itemView.findViewById(R.id.touXiang_item);
tv1 = itemView.findViewById(R.id.tv1_item);
tv2 = itemView.findViewById(R.id.tv2_item);
tv3 = itemView.findViewById(R.id.tv3_item);
btn = itemView.findViewById(R.id.btn_item);
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements IDuanZiView {
@BindView(R.id.recycleView)
RecyclerView recycleView;
@BindView(R.id.refreshLayout)
SmartRefreshLayout refreshLayout;
private DuanZiPresenter duanZiPresenter;
private List<DuanZiBean.DataBean> list;
int page = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
duanZiPresenter = new DuanZiPresenter();
duanZiPresenter.attachView(this);
duanZiPresenter.getData(Api.DUANZI_API, page);
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(RefreshLayout refreshLayout) {
page = page + 10;
duanZiPresenter.getData(Api.DUANZI_API, page);
refreshLayout.finishLoadMore(2000);
}
});
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshLayout) {
page = 1;
duanZiPresenter.getData(Api.DUANZI_API, page);
refreshLayout.finishRefresh(2000);
}
});
}
/**
* @param responseBody
*/
@Override
public void onSuccess(ResponseBody responseBody) {
try {
String string = responseBody.string();
DuanZiBean duanZiBean = new Gson().fromJson(string, DuanZiBean.class);
list = duanZiBean.getData();
MyAdapter adapter = new MyAdapter(this, list);
recycleView.setAdapter(adapter);
recycleView.setLayoutManager(new LinearLayoutManager(this));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 销毁
*/
@Override
protected void onDestroy() {
super.onDestroy();
if (duanZiPresenter == null) {
duanZiPresenter.dettachView();
}
}
}
activity_main.xml
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="段子"/>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
layout_more.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/pinglun_item"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/pinglun"/>
<ImageView
android:id="@+id/zhaunfa_item"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/zhuanfa"/>
<ImageView
android:id="@+id/dianzan_item"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/dianzan"/>
</LinearLayout>
recycler_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="400dp"
android:layout_height="55dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/touXiang_item"
android:layout_width="50dp"
android:layout_height="50dp"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:roundAsCircle="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="5"
android:orientation="vertical">
<TextView
android:id="@+id/tv1_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一条"
android:textColor="#f00" />
<TextView
android:id="@+id/tv2_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二条" />
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageButton
android:id="@+id/btn_item"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginRight="2dp"
android:src="@drawable/dianmore" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@+id/tv3_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="第三条" />
</LinearLayout>