package zhangyanran20181013.bwie.com.zhoukaomoni.adapter;
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 java.util.List;
import zhangyanran20181013.bwie.com.zhoukaomoni.R;
import zhangyanran20181013.bwie.com.zhoukaomoni.bean.User;
import zhangyanran20181013.bwie.com.zhoukaomoni.holder.MyViewHolder;
/**
* Created by 匹诺曹 on 2018/10/13.
*/
public class MyAdapter extends RecyclerView.Adapter{
Context context;
List list;
public MyAdapter(Context context, List list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false));
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.simp.setImageURI(list.get(position).getImages().getSmall());
holder.tv1.setText(list.get(position).getTitle());
holder.tv2.setText(list.get(position).getAuthor_intro());
}
@Override
public int getItemCount() {
return list.size();
}
}
public interface Api {
@GET("v2/book/search?q=小说&start=2&count=20")
Observable toget();
}
//
public static final String BASE_URL = "https://api.douban.com/";
//
package com.bawei.moni2.http;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class HttpUtils {
public final Api apiclient;
private HttpUtils(){
//实例化
OkHttpClient okHttpClient = new OkHttpClient.Builder()
//添加拦截器
.addNetworkInterceptor(new LoggingInterceptor())
.build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
apiclient = retrofit.create(Api.class);
}
private class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Response proceed = chain.proceed(request);
long t2 = System.nanoTime();
return proceed;
}
}
private static class GetHttpUtilsInstance{
private static HttpUtils httpUtils=new HttpUtils();
}
public static HttpUtils getHttpUtilsInstance(){
return GetHttpUtilsInstance.httpUtils;
}
}
//package com.bawei.moni2.model;
import com.bawei.moni2.bean.UserBean;
import com.bawei.moni2.http.HttpUtils;
import com.bawei.moni2.view.IView;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class IModel {
public void getProduct(final IView iView){
Observable observable = HttpUtils.getHttpUtilsInstance().apiclient.toget();
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(UserBean userBean) {
List books = userBean.getBooks();
iView.Success(books);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
public interface Model {
void Success(List mbean);
void Error(int code);
}
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.0.2'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'com.jakewharton:butterknife:8.8.1'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'com.facebook.fresco:fresco:1.10.0'
implementation 'com.android.support:recyclerview-v7:27.1.1'
/*自定义流式布局*/
implementation 'com.fyales.android:library:1.0.2'
package zhangyanran20181013.bwie.com.zhoukaomoni.activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.fyales.tagcloud.library.TagBaseAdapter;
import com.fyales.tagcloud.library.TagCloudLayout;
import java.util.ArrayList;
import java.util.List;
import zhangyanran20181013.bwie.com.zhoukaomoni.R;
import zhangyanran20181013.bwie.com.zhoukaomoni.mvp.view.ShowActivity;
public class MainActivity extends AppCompatActivity {
private Button but;
private List list = new ArrayList<>();
private TagCloudLayout mContainer;
private ArrayList mList;
private TagBaseAdapter mAdapter;
private EditText edt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt = findViewById(R.id.edt);
mContainer = findViewById(R.id.container);
mList = new ArrayList<>();
mAdapter = new TagBaseAdapter(this, mList);
findViewById(R.id.add_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = edt.getText().toString();
mList.add(s);
mAdapter.notifyDataSetChanged();
}
});
mContainer.setAdapter(mAdapter);
mContainer.setItemClickListener(new TagCloudLayout.TagItemClickListener() {
@Override
public void itemClick(int position) {
Intent intent = new Intent(MainActivity.this, ShowActivity.class);
startActivity(intent);
}
});
}
}