//可能需要的依赖
//配置retrofit2.0
implementation 'com.squareup.retrofit2:retrofit:+'
implementation 'com.squareup.retrofit2:converter-gson:+'
//Rxjava2需要依赖
implementation 'io.reactivex.rxjava2:rxjava:+'
implementation 'io.reactivex.rxjava2:rxandroid:+'
//让retrofit支持Rxjava2
implementation 'com.squareup.retrofit2:adapter-rxjava2:+'
//reclerview的依赖
implementation 'com.android.support:recyclerview-v7:27.1.1'
//xRecyclerView的依赖
implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
implementation 'com.android.support:design:27.1.1'
//fresco依赖
implementation 'com.facebook.fresco:fresco:1.9.0'
//banner轮播图
implementation 'com.youth.banner:banner:1.4.10'
//1.1.0 API改动过大,老用户升级需谨慎
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-11'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-11'
//拦截器的依赖
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
//butterknife插件依赖
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//跑马灯
implementation 'com.sunfusheng:marqueeview:1.3.3'
//Glide 加载图片
implementation 'com.github.bumptech.glide:glide:4.7.1'
implementation 'org.greenrobot:eventbus:3.0.0'
//接口类
public static final String BASE_URL = "https://www.zhaoapi.cn/";
//分类的左边 public static final String JD_CLASSES_LEFT = "product/getCatagory"; //分类的右边 public static final String JD_CLASSES_RIGHT = "product/getProductCatagory";\
//搜索 public static final String JD_SEARCH="product/searchProducts";
//分类点击子条目进入详情 public static final String JD_CLASSES_PRODUCT="product/getProducts";
//Api页面
package com.bw.my_jingdong.mvp.classes.model.api;
import com.bw.my_jingdong.mvp.classes.model.bean.ClassesBeanLeft;
import com.bw.my_jingdong.mvp.classes.model.bean.ClassesBeanRight;
import com.bw.my_jingdong.mvp.classes.model.bean.ProductBean;
import com.bw.my_jingdong.mvp.classes.model.bean.SearchBean;
import com.bw.my_jingdong.utils.HttpConfig;
import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface IClassesApi {
//分类页面的左边
@GET(HttpConfig.JD_CLASSES_LEFT)
Observable<ClassesBeanLeft> doLeft();
//分类页面的右边
@GET(HttpConfig.JD_CLASSES_RIGHT)
Observable<ClassesBeanRight> doRight(@Query("cid") int cid);
//搜索
@GET(HttpConfig.JD_SEARCH)
Observable<SearchBean> doSearch(@Query("keywords")String keywords,@Query("sort")int sort);
//商品详情
@GET(HttpConfig.JD_CLASSES_PRODUCT)
Observable<ProductBean> doProduct(@Query("pscid")int pscid,@Query("sort")int sort);
}
//model层
package com.bw.my_jingdong.mvp.classes.model.classesmodel;
import com.bw.my_jingdong.mvp.classes.model.api.IClassesApi;
import com.bw.my_jingdong.mvp.classes.model.bean.ClassesBeanLeft;
import com.bw.my_jingdong.mvp.classes.model.bean.ClassesBeanRight;
import com.bw.my_jingdong.utils.RetrofitManager;
import io.reactivex.Observable;
//分类左边listview+分类右边
public class ClassesModel {
public Observable<ClassesBeanLeft> getLeft(){
return RetrofitManager.getDefault().create(IClassesApi.class).doLeft();
}
public Observable<ClassesBeanRight> getRight(int cid){
return RetrofitManager.getDefault().create(IClassesApi.class).doRight(cid);
}
}
//分类搜索
package com.bw.my_jingdong.mvp.classes.model.classesmodel;
import com.bw.my_jingdong.mvp.classes.model.api.IClassesApi;
import com.bw.my_jingdong.mvp.classes.model.bean.SearchBean;
import com.bw.my_jingdong.utils.RetrofitManager;
import io.reactivex.Observable;
public class SearchModel {
public Observable<SearchBean> getSearch(String keywords,int sort){
return RetrofitManager.getDefault().create(IClassesApi.class).doSearch(keywords,sort);
}
}
//分类点击进入商品详情
package com.bw.my_jingdong.mvp.classes.model.classesmodel;
import com.bw.my_jingdong.mvp.classes.model.api.IClassesApi;
import com.bw.my_jingdong.mvp.classes.model.bean.ProductBean;
import com.bw.my_jingdong.utils.RetrofitManager;
import io.reactivex.Observable;
public class ProductModel {
public Observable<ProductBean> getProduct(int pscid,int sort) {
return RetrofitManager.getDefault().create(IClassesApi.class).doProduct(pscid,sort);
}
}
//Presenter层
public class ClassesPresenter extends BasePresenter<ClassesView> {
private ClassesModel classesModel;
public ClassesPresenter(ClassesView view) {
super(view);
}
@Override
protected void initModel() {
classesModel = new ClassesModel();
}
public void getLeftGoods() {
classesModel.getLeft()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ClassesBeanLeft>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ClassesBeanLeft classesBeanLeft) {
if (view != null) {
view.onLeftSuccess(classesBeanLeft);
}
}
@Override
public void onError(Throwable e) {
if (view != null) {
view.onRightFaild(e.toString());
}
}
@Override
public void onComplete() {
}
});
}
public void getRightGoods(int cid){
classesModel.getRight(cid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ClassesBeanRight>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ClassesBeanRight classesBeanRight) {
if (view!=null){
view.onRightSuccess(classesBeanRight);
}
}
@Override
public void onError(Throwable e) {
if (view!=null){
view.onRightFaild(e.toString());
}
}
@Override
public void onComplete() {
}
});
}
}
//搜索的presenter'
public class SearchPresenter extends BasePresenter<SearchView> {
private SearchModel searchModel;
public SearchPresenter(SearchView view) {
super(view);
}
@Override
protected void initModel() {
searchModel = new SearchModel();
}
public void Search(String keywords,int sort) {
searchModel.getSearch(keywords,sort)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<SearchBean>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onNext(SearchBean searchBean) {
if (view != null) {
view.onSuccess(searchBean);
}
}
@Override
public void onError(Throwable e) {
if (view != null) {
view.onFaild(e.toString());
}
}
@Override
public void onComplete() {
}
});
}
}
//点击进入商品详情的presenter
public class ProductPresenter extends BasePresenter<ProductView> {
private ProductModel productModel;
public ProductPresenter(ProductView view) {
super(view);
}
@Override
protected void initModel() {
productModel = new ProductModel();
}
public void getProduct(int pscid, int sort) {
productModel.getProduct(pscid, sort)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ProductBean>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onNext(ProductBean productBean) {
if (view != null) {
view.onSuccess(productBean);
}
}
@Override
public void onError(Throwable e) {
if (view != null) {
view.onFaild(e.toString());
}
}
@Override
public void onComplete() {
}
});
}
}
//分类主页面布局
<?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="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#ff5656"
android:orientation="horizontal">
<Button
android:id="@+id/classes_btn_sao"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/sao_kind" />
<EditText
android:id="@+id/classes_ed_text"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_weight="1"
android:background="#fff"
android:focusable="false"
android:hint=" 618京东购物狂欢" />
<Button
android:id="@+id/classes_btn_msg"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/xinxi" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:id="@+id/class_listview"
android:layout_width="120dp"
android:layout_height="match_parent"></ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="@drawable/timg"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/class_recyler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
//分类右边适配器布局(大的)
<?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="match_parent">
<TextView
android:id="@+id/base01_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="666"
android:textSize="30dp"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/base01_recyler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/base01_tv"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
//分类右边适配器布局(小的)
<?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="match_parent"
android:orientation="vertical"
>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/small_img"
android:layout_width="80dp"
android:layout_height="80dp"
/>
<TextView
android:id="@+id/small_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="656"
/>
</LinearLayout>
//分类右边适配器
public class MyRightBigAdapter extends RecyclerView.Adapter {
private RecyclerView recyclerView;
private List<ClassesBeanRight.DataBean> list;
private Context context;
private MyRightSmallAdapter myRightSmallAdapter;
public MyRightBigAdapter(List<ClassesBeanRight.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), R.layout.classes_base1, null);
return new MyBigHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
((MyBigHolder) holder).textView.setText(list.get(position).getName());
List<ClassesBeanRight.DataBean.ListBean> list1 = this.list.get(position).getList();
myRightSmallAdapter = new MyRightSmallAdapter(list1);
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(myRightSmallAdapter);
myRightSmallAdapter.setOnClickListener(new MyRightSmallAdapter.onClickListener() {
@Override
public void onClick(View v, int position) {
Intent it = new Intent(context, ClssesProducts.class);
int pscid = MyRightBigAdapter.this.list.get(position).getList().get(position).getPscid();
it.putExtra("pscid",pscid);
context.startActivity(it);
}
});
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
public class MyBigHolder extends RecyclerView.ViewHolder {
private final TextView textView;
public MyBigHolder(View itemView) {
super(itemView);
recyclerView = itemView.findViewById(R.id.base01_recyler);
textView = itemView.findViewById(R.id.base01_tv);
}
}
}
//小适配器
package com.bw.my_jingdong.mvp.classes.view.adapter;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.bw.my_jingdong.R;
import com.bw.my_jingdong.mvp.classes.model.bean.ClassesBeanRight;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
public class MyRightSmallAdapter extends RecyclerView.Adapter {
private List<ClassesBeanRight.DataBean.ListBean> list;
public MyRightSmallAdapter(List<ClassesBeanRight.DataBean.ListBean> list) {
this.list = list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), R.layout.class_small, null);
return new MySmallHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
//Log.d("tag", "onBindViewHolder:88888888888888 "+list.get(position).getList().get(position).getName());
((MySmallHolder) holder).tv_name.setText(list.get(position).getName());
String pic = list.get(position).getIcon();
Uri uri = Uri.parse(pic);
((MySmallHolder) holder).small_img.setImageURI(uri);
((MySmallHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClickListener != null) {
onClickListener.onClick(v, position);
}
}
});
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
public class MySmallHolder extends RecyclerView.ViewHolder {
private final SimpleDraweeView small_img;
private final TextView tv_name;
public MySmallHolder(View itemView) {
super(itemView);
small_img = itemView.findViewById(R.id.small_img);
tv_name = itemView.findViewById(R.id.small_tv);
}
}
onClickListener onClickListener;
public void setOnClickListener(MyRightSmallAdapter.onClickListener onClickListener) {
this.onClickListener = onClickListener;
}
public interface onClickListener {
void onClick(View v, int position);
}
}
//主页面的完成
package com.bw.my_jingdong.mvp.classes.view.fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.bw.my_jingdong.R;
import com.bw.my_jingdong.base.BaseFragment;
import com.bw.my_jingdong.mvp.classes.model.bean.ClassesBeanLeft;
import com.bw.my_jingdong.mvp.classes.model.bean.ClassesBeanRight;
import com.bw.my_jingdong.mvp.classes.persenter.ClassesPresenter;
import com.bw.my_jingdong.mvp.classes.view.activity.SearchActivity;
import com.bw.my_jingdong.mvp.classes.view.adapter.MyLeftAdapter;
import com.bw.my_jingdong.mvp.classes.view.adapter.MyRightBigAdapter;
import com.bw.my_jingdong.mvp.classes.view.view.ClassesView;
import com.xys.libzxing.zxing.activity.CaptureActivity;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.List;
public class ClassesFragment extends BaseFragment<ClassesPresenter> implements ClassesView, View.OnClickListener {
private ListView listView;
private RecyclerView recyclerView;
int cid = 1;
private Button btn_sao;
private EditText editText;
private MyRightBigAdapter myRightBigAdapter;
@Override
protected void initViews(View view) {
listView = view.findViewById(R.id.class_listview);
recyclerView = view.findViewById(R.id.class_recyler);
btn_sao = view.findViewById(R.id.classes_btn_sao);
editText = view.findViewById(R.id.classes_ed_text);
editText.setOnClickListener(this);
btn_sao.setOnClickListener(this);
}
@Override
protected void initData() {
presenter.getLeftGoods();
presenter.getRightGoods(1);
}
@Override
protected void initListener() {
}
@Override
protected ClassesPresenter provide() {
return new ClassesPresenter((ClassesView) this);
}
@Override
protected int provId() {
return R.layout.classesfragment;
}
@Override
public void onLeftSuccess(ClassesBeanLeft classesBeanLeft) {
final List<ClassesBeanLeft.DataBean> data = classesBeanLeft.getData();
MyLeftAdapter myLeftAdapter = new MyLeftAdapter(data);
listView.setAdapter(myLeftAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int cid = data.get(position).getCid();
presenter.getRightGoods(cid);
}
});
}
@Override
public void onLeftFaild(String error) {
}
@Override
public void onRightSuccess(ClassesBeanRight classesBeanRight) {
List<ClassesBeanRight.DataBean> data = classesBeanRight.getData();
myRightBigAdapter = new MyRightBigAdapter(data, getContext());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(myRightBigAdapter);
}
@Override
public void onRightFaild(String error) {
}
@Override
public Context cotext() {
return getContext();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.home_btn_sao:
Intent intent = new Intent(getContext(), CaptureActivity.class);
startActivityForResult(intent, 0);
break;
case R.id.classes_ed_text:
Intent it = new Intent(getContext(), SearchActivity.class);
startActivity(it);
break;
}
}
}
//点击搜索框进入搜索
case R.id.classes_ed_text:
Intent it = new Intent(getContext(), SearchActivity.class);
startActivity(it);
break;
//搜索页面需要流失布局
package com.bw.my_jingdong.mvp.classes.view.activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class FlowLayout extends ViewGroup {
public FlowLayout(Context context) {
super(context);
}
public FlowLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FlowLayout(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 widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//自定义属性
int width = 0;
int height = 0;
int childWidth = 0;
int childHeight = 0;
int linWidth = 0;
int linHeight = 0;
int totalHeight = 0;
View viewChild;
//遍历
for (int i = 0; i < getChildCount(); i++) {
//获取子布局宽高
viewChild = getChildAt(i);
//获取子布局宽高
childWidth = viewChild.getMeasuredWidth();
childHeight = viewChild.getMeasuredHeight();
if (childWidth > widthSize) {
throw new IllegalArgumentException("长度过大");
}
if (linWidth > widthSize) {
//换行
width = widthSize;
totalHeight += linHeight;
linWidth = childWidth;
linHeight = childHeight;
} else {
//否则不换行
linWidth += childWidth;
linHeight = Math.max(linHeight, childHeight);
width = Math.max(linWidth, width);
}
if (i == getChildCount() - 1) {
totalHeight = totalHeight + linHeight;
height = totalHeight;
}
width = widthMode == MeasureSpec.EXACTLY ? widthSize : width;
height = heightMode == MeasureSpec.EXACTLY ? heightSize : height;
setMeasuredDimension(width, height);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//自定义属性
int width = 0;
int height = 0;
int childWidth = 0;
int childHeight = 0;
int linWidth = 0;
int linHeight = 0;
int totalHeight = 0;
View viewChild;
for (int i = 0; i < getChildCount(); i++) {
viewChild = getChildAt(i);
childWidth = viewChild.getMeasuredWidth();
childHeight = viewChild.getMeasuredHeight();
if (linWidth + childWidth > getMeasuredWidth()) {
//换行
linWidth = 0;
totalHeight += linHeight;
layoutView(viewChild, linWidth, totalHeight, linWidth + childWidth, totalHeight + childHeight);
linHeight = childHeight;
linWidth = childWidth;
} else {
//不换行
layoutView(viewChild, linWidth, totalHeight, linWidth + childWidth, totalHeight + childHeight);
linWidth += childWidth;
linHeight = Math.max(linHeight, childHeight);
}
}
}
private void layoutView(View viewChild, int linWidth, int totalHeight, int i, int i1) {
linWidth += getPaddingLeft();
linWidth += getPaddingTop();
viewChild.layout(linWidth, totalHeight, i, i1);
}
}
//搜搜页面布局
<?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="vertical"
tools:context=".mvp.classes.view.activity.SearchActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ff5656"
>
<Button
android:id="@+id/home_btn_sao"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/sao_kind" />
<EditText
android:id="@+id/search_sousuo"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:background="#fff"
android:hint="请输入搜索的内容"
/>
<Button
android:id="@+id/btn_sousuo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
/>
</LinearLayout>
<com.bw.my_jingdong.mvp.classes.view.activity.FlowLayout
android:id="@+id/flow"
android:layout_width="match_parent"
android:layout_height="100sp"
>
</com.bw.my_jingdong.mvp.classes.view.activity.FlowLayout>
<Button
android:id="@+id/serch_btn_remove"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清空浏览记录"
/>
</LinearLayout>
//点击搜索两字进入商品详情
package com.bw.my_jingdong.mvp.classes.view.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 android.widget.TextView;
import com.bw.my_jingdong.R;
public class SearchActivity extends AppCompatActivity {
private EditText editText;
private Button btn_sousuo;
private Button btn_remove;
private FlowLayout flowLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
initViews();
}
private void initViews() {
editText = findViewById(R.id.search_sousuo);
btn_sousuo = findViewById(R.id.btn_sousuo);
btn_remove = findViewById(R.id.serch_btn_remove);
flowLayout = findViewById(R.id.flow);
btn_sousuo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editText.getText().toString();
TextView textView = new TextView(SearchActivity.this);
textView.setText(name);
flowLayout.addView(textView);
Intent it = new Intent(SearchActivity.this, ShowSuccessActivity.class);
it.putExtra("name", name);
startActivityForResult(it, 100);
}
});
btn_remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
flowLayout.removeAllViews();
}
});
}
}
//商品详情布局
<?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="vertical"
tools:context=".mvp.classes.view.activity.ShowSuccessActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#ff5656"
android:orientation="horizontal">
<Button
android:id="@+id/home_btn_sao"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/sao_kind" />
<EditText
android:id="@+id/home_ed_text"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_weight="1"
android:background="#fff"
android:focusable="false"
android:hint=" 618京东购物狂欢" />
<Button
android:id="@+id/home_btn_msg"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/xinxi" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/show_success_moren"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认"
android:layout_weight="1"
/>
<Button
android:id="@+id/show_success_show_seller"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="销量"
android:layout_weight="1"
/>
<Button
android:id="@+id/show_success_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="价格"
android:layout_weight="1"
/>
</LinearLayout>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/show_success_xrecyler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.jcodecraeer.xrecyclerview.XRecyclerView>
</LinearLayout>
//商品详情适配器布局
<?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="match_parent"
android:orientation="vertical"
>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/show_success_item_img"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/show_success_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
/>
<TextView
android:id="@+id/show_success_item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="价格¥"
android:textColor="#f00"
/>
</LinearLayout>
</LinearLayout>
//商品详情适配器
package com.bw.my_jingdong.mvp.classes.view.adapter;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.bw.my_jingdong.R;
import com.bw.my_jingdong.mvp.classes.model.bean.SearchBean;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
public class MySearchAdapter extends RecyclerView.Adapter {
private List<SearchBean.DataBean> list;
public MySearchAdapter(List<SearchBean.DataBean> list) {
this.list = list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), R.layout.showsuccess_item, null);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((MyHolder) holder).tv_title.setText(list.get(position).getTitle());
((MyHolder) holder).tv_price.setText(list.get(position).getPrice() + "");
String[] pic = list.get(position).getImages().split("\\|");
Uri uri = Uri.parse(pic[0]);
((MyHolder) holder).img.setImageURI(uri);
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
private final SimpleDraweeView img;
private final TextView tv_title;
private final TextView tv_price;
public MyHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.show_success_item_img);
tv_title = itemView.findViewById(R.id.show_success_item_title);
tv_price = itemView.findViewById(R.id.show_success_item_price);
}
}
}
//展示商品信息页面
package com.bw.my_jingdong.mvp.classes.view.activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.bw.my_jingdong.R;
import com.bw.my_jingdong.base.BaseActivity;
import com.bw.my_jingdong.mvp.classes.model.bean.SearchBean;
import com.bw.my_jingdong.mvp.classes.persenter.SearchPresenter;
import com.bw.my_jingdong.mvp.classes.view.adapter.MySearchAdapter;
import com.bw.my_jingdong.mvp.classes.view.view.SearchView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.List;
public class ShowSuccessActivity extends BaseActivity<SearchPresenter> implements SearchView, View.OnClickListener {
private XRecyclerView xRecyclerView;
int sort = 0;
private Button btn_moren;
private Button btn_seller;
private Button btn_price;
@Override
protected void initListener() {
}
@Override
protected void initData() {
Intent it = getIntent();
String name = it.getStringExtra("name");
presenter.Search(name, sort);
}
@Override
protected void initViews() {
xRecyclerView = findViewById(R.id.show_success_xrecyler);
btn_moren = findViewById(R.id.show_success_moren);
btn_seller = findViewById(R.id.show_success_show_seller);
btn_price = findViewById(R.id.show_success_price);
btn_moren.setOnClickListener(this);
btn_price.setOnClickListener(this);
btn_seller.setOnClickListener(this);
}
@Override
protected SearchPresenter provide() {
return new SearchPresenter((SearchView) this);
}
@Override
protected int provId() {
return R.layout.activity_show_success;
}
@Override
public void onSuccess(SearchBean searchBean) {
List<SearchBean.DataBean> data = searchBean.getData();
GridLayoutManager gridLayoutManager = new GridLayoutManager(ShowSuccessActivity.this, 2);
gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
xRecyclerView.setLayoutManager(gridLayoutManager);
MySearchAdapter mySearchAdapter = new MySearchAdapter(data);
xRecyclerView.setAdapter(mySearchAdapter);
}
@Override
public void onFaild(String error) {
}
@Override
public Context cotext() {
return ShowSuccessActivity.this;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.show_success_moren:
sort = 0;
initData();
break;
case R.id.show_success_show_seller:
sort = 1;
Toast.makeText(ShowSuccessActivity.this,"dddd",Toast.LENGTH_SHORT).show();
initData();
break;
case R.id.show_success_price:
sort = 2;
initData();
break;
}
}
}
本文介绍了一个基于Retrofit 2.0和RxJava 2的Android应用案例,展示了如何实现一个类似京东商城APP的分类页面和搜索功能。通过使用依赖管理和模块化的架构,实现了分类页面的数据加载、搜索功能及商品详情展示。文章覆盖了从网络请求到UI展示的完整流程。
3503

被折叠的 条评论
为什么被折叠?



