主界面布局
<?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:background="#babac4"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:background="@drawable/shape_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="请输入想要搜索的书籍" />
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/a_4"
android:id="@+id/loginbtn"/>
</LinearLayout>
<com.example.asus.week2demo.utils.LiuShi
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/liull"/>
</LinearLayout>
主界面代码
public class MainActivity extends AppCompatActivity {
@BindView(R.id.editText)
EditText editText;
@BindView(R.id.loginbtn)
Button loginbtn;
@BindView(R.id.liull)
LiuShi liull;
private List<String> list=new ArrayList<>();
private Unbinder bind;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bind = ButterKnife.bind(this);
initData();
}
private void initData() {
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String st = editText.getText().toString();
list.add(st);
liull.removeAllViews();
for (int i = 0; i < list.size(); i++) {
TextView tv = new TextView(MainActivity.this);
tv.setText(list.get(i));
liull.addView(tv);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,ShowActivity.class));
}
});
}
liull.setPadding(5, 5, 5, 5);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
bind.unbind();
}
}
列表的布局
<?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="horizontal"
tools:context=".ShowActivity">
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/xRcyclerView"/>
</LinearLayout>
列表的代码
public class ShowActivity extends AppCompatActivity implements IView{
@BindView(R.id.xRcyclerView)
XRecyclerView xRcyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
ButterKnife.bind(this);
IPresenter iPresenter = new IPresenter(this);
iPresenter.show();
}
@Override
public void success(final UserBean userBean) {
runOnUiThread(new Runnable() {
@Override
public void run() {
xRcyclerView.setLayoutManager(new LinearLayoutManager(ShowActivity.this));
MyAdapter adapter = new MyAdapter(ShowActivity.this,userBean.getBooks());
xRcyclerView.setAdapter(adapter);
}
});
}
@Override
public void failure(String msg) {
Log.i("ssss","dadadadadadad");
}
}
App
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
Constant
public class Constant {
public static final String fields ="https://api.douban.com/";
}
Api
public interface Api {
@FormUrlEncoded
@POST("v2/book/search")
Observable<UserBean> getShow(@Field("q")String q,@Field("start") String s,@Field("count") String x);
}
RetrofitUtils
public class HttpUtils {
public final Api api;
private HttpUtils(){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new LoggingInterceptor())
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.fields)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
api = retrofit.create(Api.class);
}
class LoggingInterceptor implements Interceptor{
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long l = System.nanoTime();
Response proceed = chain.proceed(request);
return proceed;
}
}
private static class GetHttpUtilsInstace{
private static HttpUtils httpUtils = new HttpUtils();
}
public static HttpUtils getHttpUtilsInsace(){
return GetHttpUtilsInstace.httpUtils;
}
}
流式布局
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;
}
}
}
IModel
public class IModel {
}
IPresenter
public class IPresenter {
IView iView;
public IPresenter(IView iView) {
this.iView = iView;
}
public void show() {
Observable<UserBean> apiShow = HttpUtils.getHttpUtilsInsace().api.getShow("小说","8","8");
apiShow.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<UserBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(UserBean userBean) {
iView.success(userBean);
}
@Override
public void onError(Throwable e) {
Log.i("xxxxxxxx","dadadadadadad");
}
@Override
public void onComplete() {
}
});
}
}
IView
public interface IView {
void success(UserBean userBean);
void failure(String msg);
}
Adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
Context context;
List<UserBean.BooksBean> list;
public MyAdapter(Context context, List<UserBean.BooksBean> list) {
this.context = context;
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Uri uri = Uri.parse(list.get(position).getImage().split("\\|")[0]);
holder.sim.setImageURI(uri);
holder.name.setText(list.get(position).getSubtitle());
holder.title.setText(list.get(position).getTags().get(position).getTitle());
holder.xiangqing.setText(list.get(position).getPubdate());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
SimpleDraweeView sim;
TextView title;
TextView name;
TextView xiangqing;
public ViewHolder(View itemView) {
super(itemView);
sim = itemView.findViewById(R.id.sim);
name = itemView.findViewById(R.id.name);
title = itemView.findViewById(R.id.title);
xiangqing = itemView.findViewById(R.id.xiangqing);
}
}
}
adapter的布局
<?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="horizontal"
android:padding="20dp">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sim"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<TextView
android:id="@+id/xiangqing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</LinearLayout>
</LinearLayout>
UserBean
package com.example.asus.week2demo.bean;
import java.util.List;
public class UserBean {
/**
* count : 20
* start : 0
* total : 139996
* books : [{"rating":{"max":10,"numRaters":24164,"average":"8.4","min":0},"subtitle":"卡夫卡小说","author":["[奥] 卡夫卡"],"pubdate":"2003-4","tags":[{"count":6375,"name":"卡夫卡","title":"卡夫卡"},{"count":2901,"name":"外国文学","title":"外国文学"},{"count":2723,"name":"变形记","title":"变形记"},{"count":2286,"name":"荒诞","title":"荒诞"},{"count":2155,"name":"小说","title":"小说"},{"count":1681,"name":"奥地利","title":"奥地利"},{"count":1303,"name":"经典","title":"经典"},{"count":896,"name":"文学","title":"文学"}],"origin_title":"","image":"https:集,全书主要记述狐鬼神怪故事,意在劝善惩恶,虽然不乏因果报应的说教,但是通过种种描写,折射出封建社会末世的腐朽和黑暗。他有意模仿亚宋笔记小说质朴简淡的文风,\"雍容淡雅,天趣盎然\",\"隽思妙语,时足解頣\"(鲁迅《中国小说史略》)。所以每脱稿一种,即被亲朋好友竞相传抄,展转刻印,一时享有同《红楼梦》、《聊斋志异》并行海内的盛誉。神怪小说,亦称神魔小说,是古代文化\u201c志怪\u201d传统在明清小说领域的酣满再现,鲁迅《中国小说史略》即将它作为明中叶后小说的两大主潮之一。神怪小说本质上是娱性文化的产物,而其所隶属于的中国文化整体却带有理性文化的特征,\u201c互相容爱\u201d,便建立了两者之间的契合点。在人世的生活空间中,加上一维神怪的空间;将超自然的神性,与社会化的人性巧妙地融合为一;驰骋无限制的想像,寄托理想和愿望,表达爱憎哀乐的思想与感情:这一切正是神怪小说特具的优势,也是其风靡城乡、深受读者喜爱的重要原因。\n明清长篇神怪小说的代表作品,除《西游记》(该书与《水浒传》、《三国演义》、《红楼梦》并称中国古典小说四大名著)外,首推《封神演义》和《镜花缘》,而《聊斋志异》和《阅微草堂笔记》则是短篇志怪小说的杰出代表。","price":"20.00元"}]
*/
private int count;
private int start;
private int total;
private List<BooksBean> books;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List<BooksBean> getBooks() {
return books;
}
public void setBooks(List<BooksBean> books) {
this.books = books;
}
public static class BooksBean {
/**
* rating : {"max":10,"numRaters":24164,"average":"8.4","min":0}
* subtitle : 卡夫卡小说
* author : ["[奥] 卡夫卡"]
* pubdate : 2003-4
* tags : [{"count":6375,"name":"卡夫卡","title":"卡夫卡"},{"count":2901,"name":"外国文学","title":"外国文学"},{"count":2723,"name":"变形记","title":"变形记"},{"count":2286,"name":"荒诞","title":"荒诞"},{"count":2155,"name":"小说","title":"小说"},{"count":1681,"name":"奥地利","title":"奥地利"},{"count":1303,"name":"经典","title":"经典"},{"count":896,"name":"文学","title":"文学"}]
* origin_title :
* image : https:集,全书主要记述狐鬼神怪故事,意在劝善惩恶,虽然不乏因果报应的说教,但是通过种种描写,折射出封建社会末世的腐朽和黑暗。他有意模仿亚宋笔记小说质朴简淡的文风,"雍容淡雅,天趣盎然","隽思妙语,时足解頣"(鲁迅《中国小说史略》)。所以每脱稿一种,即被亲朋好友竞相传抄,展转刻印,一时享有同《红楼梦》、《聊斋志异》并行海内的盛誉。神怪小说,亦称神魔小说,是古代文化“志怪”传统在明清小说领域的酣满再现,鲁迅《中国小说史略》即将它作为明中叶后小说的两大主潮之一。神怪小说本质上是娱性文化的产物,而其所隶属于的中国文化整体却带有理性文化的特征,“互相容爱”,便建立了两者之间的契合点。在人世的生活空间中,加上一维神怪的空间;将超自然的神性,与社会化的人性巧妙地融合为一;驰骋无限制的想像,寄托理想和愿望,表达爱憎哀乐的思想与感情:这一切正是神怪小说特具的优势,也是其风靡城乡、深受读者喜爱的重要原因。
明清长篇神怪小说的代表作品,除《西游记》(该书与《水浒传》、《三国演义》、《红楼梦》并称中国古典小说四大名著)外,首推《封神演义》和《镜花缘》,而《聊斋志异》和《阅微草堂笔记》则是短篇志怪小说的杰出代表。
* price : 20.00元
*/
private RatingBean rating;
private String subtitle;
private String pubdate;
private String origin_title;
private String image;
private String price;
private List<String> author;
private List<TagsBean> tags;
public RatingBean getRating() {
return rating;
}
public void setRating(RatingBean rating) {
this.rating = rating;
}
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public String getPubdate() {
return pubdate;
}
public void setPubdate(String pubdate) {
this.pubdate = pubdate;
}
public String getOrigin_title() {
return origin_title;
}
public void setOrigin_title(String origin_title) {
this.origin_title = origin_title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public List<String> getAuthor() {
return author;
}
public void setAuthor(List<String> author) {
this.author = author;
}
public List<TagsBean> getTags() {
return tags;
}
public void setTags(List<TagsBean> tags) {
this.tags = tags;
}
public static class RatingBean {
/**
* max : 10
* numRaters : 24164
* average : 8.4
* min : 0
*/
private int max;
private int numRaters;
private String average;
private int min;
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public int getNumRaters() {
return numRaters;
}
public void setNumRaters(int numRaters) {
this.numRaters = numRaters;
}
public String getAverage() {
return average;
}
public void setAverage(String average) {
this.average = average;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
}
public static class TagsBean {
/**
* count : 6375
* name : 卡夫卡
* title : 卡夫卡
*/
private int count;
private String name;
private String title;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}