implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' implementation 'com.facebook.fresco:fresco:+' implementation 'com.facebook.fresco:animated-gif:+' implementation 'org.greenrobot:eventbus:3.1.1' implementation 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
adapter
package dongyushan.bwie.com.zhoukao2lianxi.adapter; import android.content.Context; import android.net.Uri; 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.TextView; import com.facebook.drawee.view.SimpleDraweeView; import java.util.List; import dongyushan.bwie.com.zhoukao2lianxi.R; import dongyushan.bwie.com.zhoukao2lianxi.model.bean.UserInfo; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{ private List<UserInfo.BooksBean> data; private Context context; public MyAdapter(List<UserInfo.BooksBean> data, Context context) { this.data = data; this.context = context; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view= LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false); MyViewHolder myViewHolder=new MyViewHolder(view); return myViewHolder; } @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) { myViewHolder.author.setText(data.get(i).getAuthor().get(0)); myViewHolder.pubdate.setText(data.get(i).getPubdate()); myViewHolder.subtitle.setText(data.get(i).getPublisher()); myViewHolder.title.setText(data.get(i).getTitle()); Uri uri=Uri.parse(data.get(i).getImage().split("\\|")[0]); myViewHolder.img.setImageURI(uri); } @Override public int getItemCount() { return data.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ private final TextView author; private final SimpleDraweeView img; private final TextView subtitle; private final TextView pubdate; private final TextView title; public MyViewHolder(View itemView) { super(itemView); author = itemView.findViewById(R.id.author); img = itemView.findViewById(R.id.img); subtitle = itemView.findViewById(R.id.subtitle); pubdate = itemView.findViewById(R.id.pubdate); title = itemView.findViewById(R.id.title); } } }
MyApplication
package dongyushan.bwie.com.zhoukao2lianxi.application; import android.app.Application; import com.facebook.drawee.backends.pipeline.Fresco; public class MyApplication extends Application{ @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
liushi
package dongyushan.bwie.com.zhoukao2lianxi.liushi; import android.content.Context; import android.util.AttributeSet; import android.view.ViewGroup; public class LiuShi extends ViewGroup { private int mLeftMargin = 20; private int mTopMargin = 20; public LiuShi(Context context) { this(context, null); } public LiuShi(Context context, AttributeSet attrs) { this(context, attrs, 0); } public LiuShi(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); measureChildren(widthMeasureSpec, heightMeasureSpec); int leftMargin = mLeftMargin; int topMargin = mTopMargin; int ViewWidth = 0; int ViewHeight = 0; int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); int modelWidth = MeasureSpec.getMode(widthMeasureSpec); int modelHeight = MeasureSpec.getMode(heightMeasureSpec); switch (modelHeight) { case MeasureSpec.AT_MOST: int measuredHeight = 0; for (int i = 0; i < getChildCount(); i++) { int measuredWidth = getChildAt(i).getMeasuredWidth(); measuredHeight = getChildAt(i).getMeasuredHeight(); if (leftMargin + measuredWidth + mLeftMargin > getWidth()) { leftMargin = mLeftMargin; topMargin += measuredHeight + mTopMargin; } leftMargin += measuredWidth + mLeftMargin; } topMargin += measuredHeight + mTopMargin; break; } setMeasuredDimension(sizeWidth, topMargin); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int leftMargin = mLeftMargin; int topMargin = mTopMargin; for (int i = 0; i < getChildCount(); i++) { int measuredWidth = getChildAt(i).getMeasuredWidth(); int measuredHeight = getChildAt(i).getMeasuredHeight(); if (leftMargin + measuredWidth + mLeftMargin > getWidth()) { leftMargin = mLeftMargin; topMargin += measuredHeight + mTopMargin; getChildAt(i).layout(leftMargin, topMargin, leftMargin + measuredWidth, topMargin + measuredHeight); } else { getChildAt(i).layout(leftMargin, topMargin, leftMargin + measuredWidth, topMargin + measuredHeight); } leftMargin += measuredWidth + mLeftMargin; } } }
Api
package dongyushan.bwie.com.zhoukao2lianxi.model.http; import dongyushan.bwie.com.zhoukao2lianxi.model.bean.UserInfo; import io.reactivex.Observable; import retrofit2.http.Field; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.Query; public interface Api { @GET("v2/book/search") Observable<UserInfo> getRespBody(@Query("q") String clothese); }
contant
package dongyushan.bwie.com.zhoukao2lianxi.model.http; public class Constant { public static final String BASE_URL="https://api.douban.com/"; }
HttpUtils
package dongyushan.bwie.com.zhoukao2lianxi.model.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 LoggingIntercepter()).build(); Retrofit retrofit=new Retrofit.Builder().baseUrl(Constant.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client(okHttpClient) .build(); apiClient = retrofit.create(Api.class); } class LoggingIntercepter implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request=chain.request(); long t1=System.nanoTime(); Response response=chain.proceed(request); long t2=System.nanoTime(); return response; } } private static class GetHttpUtilsInstance{ private static HttpUtils httpUtils=new HttpUtils(); } public static HttpUtils getHttpUtilsInstance(){ return GetHttpUtilsInstance.httpUtils; } }
ModelTest
package dongyushan.bwie.com.zhoukao2lianxi.model.http; public class ModelTest { public String getData(){ return "server data ok"; } }
BasePresenter
package dongyushan.bwie.com.zhoukao2lianxi.presenter; import dongyushan.bwie.com.zhoukao2lianxi.view.IVew.IBaseView; public class BasePresenter<V extends IBaseView>{ private V iv; public void attachView(V v) { this.iv = v; } public void dettachView() { this.iv = null; } public V getView(){ return iv; } }
MainPersent
package dongyushan.bwie.com.zhoukao2lianxi.presenter; import java.util.List; import dongyushan.bwie.com.zhoukao2lianxi.model.bean.UserInfo; import dongyushan.bwie.com.zhoukao2lianxi.model.http.HttpUtils; import dongyushan.bwie.com.zhoukao2lianxi.model.http.ModelTest; import dongyushan.bwie.com.zhoukao2lianxi.view.IVew.IMainView; 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 MainPresenter extends BasePresenter<IMainView> { public void loadDataFromNet(){ Observable<UserInfo> body = HttpUtils.getHttpUtilsInstance().apiClient.getRespBody("衣服"); body.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<UserInfo>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(UserInfo userInfo) { List<UserInfo.BooksBean> data = userInfo.getBooks(); getView().onSuccess(data); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } }
SecondPr
package dongyushan.bwie.com.zhoukao2lianxi.presenter; import dongyushan.bwie.com.zhoukao2lianxi.model.http.ModelTest; import dongyushan.bwie.com.zhoukao2lianxi.view.IVew.ISecondView; public class SecondPresenter extends BasePresenter<ISecondView>{ private final ModelTest modelTest; public SecondPresenter(){ modelTest = new ModelTest(); } public void loadDataFromNet2(){ String data=modelTest.getData(); getView().onSuccess(data,0); } }
BaseActivity
package dongyushan.bwie.com.zhoukao2lianxi; import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; public class BasaActivity extends AppCompatActivity{ @Override public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); } }
MainActivity
package dongyushan.bwie.com.zhoukao2lianxi; 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.EditText; import android.widget.LinearLayout; import android.widget.TextView; import com.facebook.drawee.view.SimpleDraweeView; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import dongyushan.bwie.com.zhoukao2lianxi.adapter.MyAdapter; import dongyushan.bwie.com.zhoukao2lianxi.liushi.LiuShi; import dongyushan.bwie.com.zhoukao2lianxi.model.bean.UserInfo; import dongyushan.bwie.com.zhoukao2lianxi.view.IVew.IMainView; public class MainActivity extends BasaActivity implements IMainView { @BindView(R.id.editText) EditText editText; @BindView(R.id.sousuo) Button sousuo; @BindView(R.id.linear) LinearLayout linear; @BindView(R.id.qingkong) TextView qingkong; @BindView(R.id.liushi) LiuShi liushi; @BindView(R.id.image_view) SimpleDraweeView imageView; @BindView(R.id.recyclerview) RecyclerView recyclerview; private List<String> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick({R.id.sousuo, R.id.qingkong}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.sousuo: String st = sousuo.getText().toString(); list.add(st); liushi.removeAllViews(); for (int i = 0; i < list.size(); i++) { TextView tv = new TextView(MainActivity.this); tv.setText(list.get(i)); liushi.addView(tv); } liushi.setPadding(5, 5, 5, 5); break; case R.id.qingkong: list.clear(); liushi.removeAllViews(); break; } } @Override public void onSuccess(List<UserInfo.BooksBean> data) { recyclerview.setLayoutManager(new LinearLayoutManager(MainActivity.this)); MyAdapter myAdapter = new MyAdapter(data, MainActivity.this); recyclerview.setAdapter(myAdapter); } @Override public void onErr(int errCode, String errMsg) { } }
activity-layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:id="@+id/linear" > <EditText android:id="@+id/editText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="9" android:hint="请输入想要搜索的书籍" /> <Button android:id="@+id/sousuo" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="搜索" /> </LinearLayout> <TextView android:id="@+id/qingkong" android:layout_width="15dp" android:layout_height="15dp" android:layout_below="@id/linear" android:background="@drawable/ic_launcher_background" /> <dongyushan.bwie.com.zhoukao2lianxi.liushi.LiuShi android:id="@+id/liushi" android:layout_width="wrap_content" android:layout_height="wrap_content"> </dongyushan.bwie.com.zhoukao2lianxi.liushi.LiuShi> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </RelativeLayout>
item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.facebook.drawee.view.SimpleDraweeView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/img" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/title" android:text="title" android:textSize="20sp" android:padding="5dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/author" android:text="author" android:textSize="12sp" android:padding="5dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/subtitle" android:text="subtitle" android:textSize="12sp" android:padding="5dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pubdate" android:text="pubdate" android:textSize="12sp" android:padding="5dp" /> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout>