一
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/scan"
android:layout_width="@dimen/dp_0"
android:layout_weight="1"
android:paddingTop="@dimen/dp_5"
android:layout_height="wrap_content"
android:src="@mipmap/sao_hei"/>
<SearchView
android:layout_width="@dimen/dp_0"
android:layout_weight="10"
android:background="@drawable/redio"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/message"
android:layout_width="@dimen/dp_0"
android:layout_weight="1"
android:paddingTop="@dimen/dp_5"
android:layout_height="wrap_content"
android:src="@mipmap/msg_hei"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview_mainclass"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content"
android:layout_weight="2"></android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="@dimen/dp_0"
android:layout_weight="5"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.recker.flybanner.FlyBanner
android:id="@+id/banner_1"
android:layout_margin="@dimen/dp_5"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_100" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/liner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
二封装bean类
三
package com.example.jingdong.api;
import com.example.jingdong.bean.GoodsBean;
import com.example.jingdong.bean.LoginBean;
import com.example.jingdong.bean.LogonBean;
import com.example.jingdong.bean.SubClassBean;
import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Query;
/**
* Created by 。。。。 on 2018/11/7.
*/
public interface APIService {
//九宫格
@GET("home/getHome")
Observable<GoodsBean> getgoods();
//登录
@GET("user/login")
Observable<LoginBean> getlogin(@Query("mobile") String mobile,@Query("password") String password);
//注册
@GET("user/reg")
Observable<LogonBean> getlogon(@Query("mobile") String mobile, @Query("password") String password);
//子分类
@GET("product/getProductCatagory")
Observable<SubClassBean> getsubclass(@Query("cid") int cid);
}
四
package com.example.jingdong.view;
import com.example.jingdong.bean.GoodsBean;
import com.example.jingdong.bean.LoginBean;
import com.example.jingdong.bean.SubClassBean;
/**
* Created by 。。。。 on 2018/11/7.
*/
public interface GoodsIView {
void OnSuccess(GoodsBean goodsBean);
void OnSuccess(SubClassBean subClassBean);
void OnFailed(Throwable t);
}
五
package com.example.jingdong.model;
import com.example.jingdong.api.APIService;
import com.example.jingdong.bean.GoodsBean;
import com.example.jingdong.bean.LoginBean;
import com.example.jingdong.bean.LogonBean;
import com.example.jingdong.bean.SubClassBean;
import com.example.jingdong.httputils.HttpUtils;
import io.reactivex.Observable;
/**
* Created by 。。。。 on 2018/11/7.
*/
public class GoodsModel {
public Observable<GoodsBean> getgoods(){
APIService apiService= HttpUtils.getinstance().create(APIService.class);
Observable<GoodsBean> goodsBeanObservable=apiService.getgoods();
return goodsBeanObservable;
}
public Observable<LoginBean> getlogin(String mobile,String password){
APIService apiService = HttpUtils.getinstance().create(APIService.class);
Observable<LoginBean> loginBeanObservable = apiService.getlogin(mobile, password);
return loginBeanObservable;
}
public Observable<LogonBean> getlogon(String mobile, String password){
APIService apiService = HttpUtils.getinstance().create(APIService.class);
Observable<LogonBean> logonBeanObservable = apiService.getlogon(mobile, password);
return logonBeanObservable;
}
public Observable<SubClassBean> getsubclass(int cid){
APIService apiService= HttpUtils.getinstance().create(APIService.class);
Observable<SubClassBean> subClassBeanObservable=apiService.getsubclass(cid);
return subClassBeanObservable;
}
}
六
package com.example.jingdong.presenter;
import com.example.jingdong.bean.GoodsBean;
import com.example.jingdong.bean.LoginBean;
import com.example.jingdong.bean.LogonBean;
import com.example.jingdong.bean.SubClassBean;
import com.example.jingdong.model.GoodsModel;
import com.example.jingdong.view.GoodsIView;
import com.example.jingdong.view.LoginIView;
import com.example.jingdong.view.LogonIView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
/**
* Created by 。。。。 on 2018/11/7.
*/
public class GoodsPresenter {
private GoodsIView iv;
private GoodsModel model;
private LoginIView loginIView;
private LogonIView logonIView;
public void attach(LoginIView loginIView) {
this.loginIView = loginIView;
}
public void attach(LogonIView logonIView) {
this.logonIView = logonIView;
}
public void attach(GoodsIView iv) {
this.iv = iv;
}
public GoodsPresenter() {
model = new GoodsModel();
}
public void dettach() {
iv = null;
}
public void getgoods() {
model.getgoods()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<GoodsBean>() {
@Override
public void accept(GoodsBean goodsBean) throws Exception {
iv.OnSuccess(goodsBean);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
iv.OnFailed(throwable);
}
});
}
public void getlogin(String mobile, String password) {
model.getlogin(mobile, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<LoginBean>() {
@Override
public void accept(LoginBean loginBean) throws Exception {
if (loginBean != null & "0".equals(loginBean.getCode())) {
if (loginIView != null) {
loginIView.OnSuccess(loginBean);
return;
}
}
if (loginIView != null) {
loginIView.OnFailed(new Throwable("请求失败"));
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
if (loginIView != null) {
loginIView.OnFailed(new Throwable("网络异常"));
}
}
});
}
public void getlogon(String mobile, String password) {
model.getlogon(mobile, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<LogonBean>() {
@Override
public void accept(LogonBean logonBean) throws Exception {
if (logonBean != null & "0".equals(logonBean.getCode())) {
if (logonIView != null) {
logonIView.OnSuccess(logonBean);
return;
}
}
if (logonIView != null) {
logonIView.OnFailed(new Throwable("请求失败"));
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
if (logonIView != null) {
logonIView.OnFailed(new Throwable("网络异常"));
}
}
});
}
public void getsubclass(int cid){
model.getsubclass(cid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<SubClassBean>() {
@Override
public void accept(SubClassBean subClassBean) throws Exception {
iv.OnSuccess(subClassBean);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
iv.OnFailed(throwable);
}
});
}
}
七
package com.example.liuhuan20181112.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.example.liuhuan20181112.R; import com.example.liuhuan20181112.bean.MainclassBean; import java.util.List; /** * Created by 。。。。 on 2018/11/12. */ //主类适配器 public class MainclassAdapter extends RecyclerView.Adapter<MainclassAdapter.ViewHolder> { private Context context; private List<MainclassBean.DataBean> list; public MainclassAdapter(Context context, List<MainclassBean.DataBean> list) { this.context = context; this.list = list; } public interface OnItemClickListener{ void OnItemClickListener(View view,int postion); } private OnItemClickListener listener; public void setOnItemClickListener(OnItemClickListener listener){ this.listener=listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v=View.inflate(context, R.layout.item_mainclass,null); ViewHolder holder=new ViewHolder(v); return holder; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { holder.txtname.setText(list.get(position).getName()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(listener!=null){ listener.OnItemClickListener(v,position); } } }); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder { private final TextView txtname; public ViewHolder(View itemView) { super(itemView); txtname = itemView.findViewById(R.id.name); } } }
八
package com.example.jingdong.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.jingdong.R;
import com.example.jingdong.bean.SubClassBean;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
/**
* Created by 。。。。 on 2018/11/9.
*/
public class SubClassAdapter extends RecyclerView.Adapter<SubClassAdapter.ViewHolder> {
private Context context;
private List<SubClassBean.DataBean.ListBean> list;
public SubClassAdapter(Context context, List<SubClassBean.DataBean.ListBean> list) {
this.context = context;
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v=View.inflate(context, R.layout.item_subclass,null);
ViewHolder holder=new ViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.imageView.setImageURI(list.get(position).getIcon());
holder.textView.setText(list.get(position).getName());
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private SimpleDraweeView imageView;
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.img_subclass);
textView = itemView.findViewById(R.id.txt_subclassname);
}
}
}
九
package com.example.jingdong.fenlei;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.jingdong.R;
import com.example.jingdong.adapter.MainClassAdapter;
import com.example.jingdong.adapter.SubClassAdapter;
import com.example.jingdong.bean.GoodsBean;
import com.example.jingdong.bean.SubClassBean;
import com.example.jingdong.presenter.GoodsPresenter;
import com.example.jingdong.view.GoodsIView;
import com.recker.flybanner.FlyBanner;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class FenLeiFragment extends Fragment implements GoodsIView {
@BindView(R.id.scan)
ImageView scan;
@BindView(R.id.message)
ImageView message;
@BindView(R.id.recyclerview_mainclass)
RecyclerView recyclerviewMainclass;
@BindView(R.id.liner)
LinearLayout liner;
Unbinder unbinder;
@BindView(R.id.banner_1)
FlyBanner banner1;
private List<GoodsBean.DataBean.FenleiBean> list;
private MainClassAdapter adapter;
private SubClassAdapter classAdapter;
private GoodsPresenter presenter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fen_lei, container, false);
unbinder = ButterKnife.bind(this, v);
//二维码
QR();
List<String> images = new ArrayList<>();
images.add("http://www.zhaoapi.cn/images/quarter/ad1.png");
images.add("http://www.zhaoapi.cn/images/quarter/ad2.png");
images.add("http://www.zhaoapi.cn/images/quarter/ad3.png");
images.add("http://www.zhaoapi.cn/images/quarter/ad4.png");
banner1.setImagesUrl(images);
list = new ArrayList<>();
adapter = new MainClassAdapter(getActivity(), list);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerviewMainclass.setLayoutManager(layoutManager);
recyclerviewMainclass.setAdapter(adapter);
presenter = new GoodsPresenter();
presenter.attach(this);
presenter.getgoods();
adapter.setOnItemClickListener(new MainClassAdapter.OnItemClickListener() {
@Override
public void OnItemClick(View view, int postion) {
GoodsBean.DataBean.FenleiBean bean = list.get(postion);
presenter.getsubclass(bean.getCid());
}
});
return v;
}
private void QR() {
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), CaptureActivity.class);
startActivityForResult(intent, 1000);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000) {
//处理扫描结果(在界面上显示)
if (null != data) {
Bundle bundle = data.getExtras();
if (bundle == null) {
return;
}
if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
String result = bundle.getString(CodeUtils.RESULT_STRING);
Toast.makeText(getActivity(), "解析结果:" + result, Toast.LENGTH_LONG).show();
} else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
Toast.makeText(getActivity(), "解析二维码失败", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
public void OnSuccess(GoodsBean goodsBean) {
list.clear();
list.addAll(goodsBean.getData().getFenlei());
adapter.notifyDataSetChanged();
}
@Override
public void OnSuccess(SubClassBean subClassBean) {
if (subClassBean != null) {
liner.removeAllViews();
for (int i = 0; i < subClassBean.getData().size(); i++) {
TextView textView = new TextView(getActivity());
textView.setText(subClassBean.getData().get(i).getName());
RecyclerView recyclerView = new RecyclerView(getActivity());
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
List<SubClassBean.DataBean.ListBean> subclasslist = new ArrayList<>();
classAdapter = new SubClassAdapter(getActivity(), subclasslist);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(classAdapter);
liner.addView(textView);
liner.addView(recyclerView);
subclasslist.clear();
subclasslist.addAll(subClassBean.getData().get(i).getList());
classAdapter.notifyDataSetChanged();
}
}
}
@Override
public void OnFailed(Throwable t) {
Toast.makeText(getActivity(), "请求失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void onResume() {
super.onResume();
presenter.getsubclass(1);
}
}