分类左侧列表适配器:
public class LeftAdapter extends RecyclerView.Adapter{ public List list=new ArrayList<>(); public Context context; public LeftAdapter(List list, Context context) { this.list.addAll(list); this.context = context; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View inflate = LayoutInflater.from(context).inflate(R.layout.left_adapter, viewGroup, false); ViewHolder viewHolder = new ViewHolder(inflate); return viewHolder; } @Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) { final LeftDataBean leftDataBean = list.get(i); viewHolder.leftadapter_tx_.setText(leftDataBean.getName()); viewHolder.leftadapter_tx_.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().postSticky(leftDataBean); } }); } @Override public int getItemCount() { return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public TextView leftadapter_tx_; public ViewHolder(@NonNull View itemView) { super(itemView); leftadapter_tx_ = (TextView) itemView.findViewById(R.id.leftadapter_tx_); } } }
分类右侧列表适配器:
public class RightAdapter extends RecyclerView.Adapter{ public List list=new ArrayList<>(); public Context context; public void setClear(){ list.clear(); } public RightAdapter(List list, Context context) { this.list.addAll(list); this.context = context; } public void clear(){ list.clear(); } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View inflate = LayoutInflater.from(context).inflate(R.layout.item_right,viewGroup,false); ViewHolder viewHolder = new ViewHolder(inflate); return viewHolder; } @Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) { RightDataBean rightDataBean = list.get(i); String name = rightDataBean.getCommodityName(); viewHolder.itemRight_name.setText(name); int price = rightDataBean.getPrice(); viewHolder.itemRight_price.setText(price+""); String imgs = rightDataBean.getMasterPic(); Glide.with(context).load(imgs).into(viewHolder.itemRight_img); } @Override public int getItemCount() { return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ImageView itemRight_img; public TextView itemRight_name; public TextView itemRight_price; public ViewHolder(@NonNull View itemView) { super(itemView); itemRight_img = (ImageView) itemView.findViewById(R.id.itemRight_img); itemRight_name = (TextView) itemView.findViewById(R.id.itemRight_name); itemRight_price = (TextView) itemView.findViewById(R.id.itemRight_price); } } }
购物车适配器:
public class GouWuAdapter extends RecyclerView.Adapter {
public List list=new ArrayList<>();
public Context context;
public boolean isChecked;
public List getList() {
return list;
}
public void setChecked(boolean checked) {
isChecked = checked;
for (int i = 0; i < list.size(); i++) {
GouWuListBean gouWuListBean = list.get(i);
List shoppingCartList = gouWuListBean.getShoppingCartList();
for (int j = 0; j < shoppingCartList.size(); j++) {
GouWuDataBean gouWuDataBean = shoppingCartList.get(j);
gouWuDataBean.setChecked(isChecked);
}
}
notifyDataSetChanged();
}
public GouWuAdapter(List list, Context context) {
this.list.addAll(list);
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = LayoutInflater.from(context).inflate(R.layout.gouwu_name, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(inflate);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
GouWuListBean gouWuListBean = list.get(i);
viewHolder.gouwu_gouwuName.setText(gouWuListBean.getCategoryName());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
viewHolder.gouwu_recycles.setLayoutManager(linearLayoutManager);
GouWuListBean gouWuListBean1 = list.get(i);
List shoppingCartList = gouWuListBean1.getShoppingCartList();
GouWuFen_Adapter gouWuFen_adapter = new GouWuFen_Adapter(shoppingCartList, context);
viewHolder.gouwu_recycles.setAdapter(gouWuFen_adapter);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView gouwu_gouwuName;
public RecyclerView gouwu_recycles;
public ViewHolder(@NonNull View itemView) {
super(itemView);
gouwu_gouwuName = (TextView) itemView.findViewById(R.id.gouwu_gouwuName);
gouwu_recycles = (RecyclerView) itemView.findViewById(R.id.gouwu_recycles);
}
}
}
购物车分的适配器:
public class GouWuFen_Adapter extends RecyclerView.Adapter {
public List list=new ArrayList<>();
public Context context;
public GouWuFen_Adapter(List list, Context context) {
this.list.addAll(list);
this.context = context;
}
@NonNull
@Override
public GouWuFen_Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = LayoutInflater.from(context).inflate(R.layout.gouwu_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(inflate);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final GouWuFen_Adapter.ViewHolder viewHolder, int i) {
final GouWuDataBean gouWuDataBean = list.get(i);
viewHolder.gouwu_item_name.setText(gouWuDataBean.getCommodityName());
Glide.with(context).load(gouWuDataBean.getPic()).into(viewHolder.gouwu_item_imgs);
viewHolder.gouwu_item_price.setText(gouWuDataBean.getPrice()+"");
boolean checked = gouWuDataBean.isChecked();
viewHolder.gouwu_item_chenckbox.setChecked(checked);
int count = gouWuDataBean.getCount();
viewHolder.addView.setNum(count);
viewHolder.addView.setDataCallBack(new Asease.DataCallBack() {
@Override
public void back(int numa) {
gouWuDataBean.setCount(numa);
EventBus.getDefault().post(new EventBean());
}
});
viewHolder.gouwu_item_chenckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked1 = viewHolder.gouwu_item_chenckbox.isChecked();
gouWuDataBean.setChecked(checked1);
EventBus.getDefault().post(new EventBean());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public Asease addView;
public CheckBox gouwu_item_chenckbox;
public ImageView gouwu_item_imgs;
public TextView gouwu_item_name;
public TextView gouwu_item_price;
public ViewHolder(@NonNull View itemView) {
super(itemView);
addView = (Asease) itemView.findViewById(R.id.addView);
gouwu_item_imgs = (ImageView) itemView.findViewById(R.id.gouwu_item_imgs);
gouwu_item_name = (TextView) itemView.findViewById(R.id.gouwu_item_name);
gouwu_item_price = (TextView) itemView.findViewById(R.id.gouwu_item_price);
gouwu_item_chenckbox = (CheckBox) itemView.findViewById(R.id.gouwu_item_chenckbox);
}
}
}
分类fragment类:
public class FragmentFenlei extends BaseFragment implements LeftContract.LeftView,RightContract.RightView {
private RecyclerView left_recycles;
private RecyclerView right_recycles;
private RightPresenterImpl rightPresenter;
HashMap map = new HashMap<>();
private RightAdapter rightAdapter;
@Override
protected int initLayout() {
return R.layout.fragment_fenlei;
}
@Override
protected void initView(View view) {
left_recycles = (RecyclerView)view.findViewById(R.id.left_recycles);
right_recycles = (RecyclerView) view.findViewById(R.id.right_recycles);
}
@Override
protected LeftPresenter initPresnet() {
return new LeftPresenter();
}
@Override
protected void initData(Bundle savedInstanceState) {
presents.show();
// TODO: 2019/11/24 注册
EventBus.getDefault().register(this);
rightPresenter = new RightPresenterImpl();
// TODO: 2019/11/24 疑问
rightPresenter.attach(this);
map.put("page","1");
map.put("count","5");
map.put("categoryId","1001004002");
this.rightPresenter.Righthow(map);
}
public void initStart(){
map.put("page","1");
map.put("count","5");
map.put("categoryId",categoryId);
rightPresenter.Righthow(map);
}
private String categoryId;
@Subscribe
public void JieQu(LeftDataBean leftDataBean){
rightAdapter.clear();
categoryId = leftDataBean.getId();
initStart();
}
@Override
public void success(LeftResultBean leftResultBean) {
List result = leftResultBean.getResult();
LeftListBean leftListBean = result.get(0);
List secondCategoryVo = leftListBean.getSecondCategoryVo();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
left_recycles.setLayoutManager(linearLayoutManager);
LeftAdapter leftAdapter = new LeftAdapter(secondCategoryVo,getContext());
left_recycles.setAdapter(leftAdapter);
}
@Override
public void success(RightResultBean rightResultBean) {
List result = rightResultBean.getResult();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
right_recycles.setLayoutManager(linearLayoutManager);
rightAdapter = new RightAdapter(result, getContext());
right_recycles.setAdapter(rightAdapter);
rightAdapter.notifyDataSetChanged();
}
@Override
public void fainal(String fainal) {
}
}
购物车fragment类:
public class FragmentGouwu extends BaseFragment implements GouWuContract.GouWuView {
private RecyclerView gouwu_recycles;
private CheckBox gouwu_zong_chengckbox;
private TextView heji_price;
private TextView tijiao_nums;
private GouWuAdapter gouWuAdapter;
@Override
protected int initLayout() {
return R.layout.fragment_gouwu;
}
@Override
protected void initView(View view) {
EventBus.getDefault().register(this);
gouwu_recycles = (RecyclerView) view.findViewById(R.id.gouwu_recycles);
gouwu_zong_chengckbox = (CheckBox) view.findViewById(R.id.gouwu_zong_chengckbox);
heji_price = (TextView) view.findViewById(R.id.heji_price);
tijiao_nums = (TextView) view.findViewById(R.id.tijiao_nums);
}
@Override
protected GouWuPresenterImpl initPresnet() {
return new GouWuPresenterImpl();
}
@Override
protected void initData(Bundle savedInstanceState) {
presents.GouWushow();
gouwu_zong_chengckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = gouwu_zong_chengckbox.isChecked();
gouWuAdapter.setChecked(checked);
toPrice();
}
});
}
@Subscribe
public void JieShou(EventBean eventBean){
toPrice();
}
private void toPrice() {
double tob = 0;
List list = gouWuAdapter.getList();
for (int i = 0; i < list.size(); i++) {
GouWuListBean gouWuListBean = list.get(i);
List shoppingCartList = gouWuListBean.getShoppingCartList();
for (int j = 0; j < shoppingCartList.size(); j++) {
GouWuDataBean gouWuDataBean = shoppingCartList.get(j);
if (gouWuDataBean.isChecked()) {
int count = gouWuDataBean.getCount();
double price = gouWuDataBean.getPrice();
double temp = count * price;
tob += temp;
}
}
}
heji_price.setText(tob + "");
}
@Override
public void success(GouWuResult gouWuResult) {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
gouwu_recycles.setLayoutManager(linearLayoutManager);
List result = gouWuResult.getResult();
gouWuAdapter = new GouWuAdapter(result, getContext());
gouwu_recycles.setAdapter(gouWuAdapter);
}
@Override
public void fainal(String fainal) {
}
}
MVP契约类:
public interface LeftContract {
interface LeftView extends IBaseView{
void success(LeftResultBean leftResultBean);
void fainal(String fainal);
}
interface LeftModel{
void show(DataCallBack dataCallBack);
interface DataCallBack{
void success(LeftResultBean leftResultBean);
void fainals(String fainal);
}
}
interface LeftPresenter{
void show();
}
}
model层:
public class LeftModeImpl implements LeftContract.LeftModel{
@Override
public void show(final DataCallBack dataCallBack) {
Retrofit retrofit = RetrofitUtils.getIntance().getRetrofit();
ApiService apiService = retrofit.create(ApiService.class);
Observable show = apiService.show();
show.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(LeftResultBean leftResultBean) {
dataCallBack.success(leftResultBean);
}
@Override
public void onError(Throwable e) {
dataCallBack.fainals(e.getMessage());
}
@Override
public void onComplete() {
}
});
}
}
presenter层:
public class LeftPresenter extends BasePresenter implements LeftContract.LeftPresenter {
private LeftModeImpl leftMode;
@Override
protected void initModel() {
leftMode = new LeftModeImpl();
}
@Override
public void show() {
leftMode.show(new LeftContract.LeftModel.DataCallBack() {
@Override
public void success(LeftResultBean leftResultBean) {
getView().success(leftResultBean);
}
@Override
public void fainals(String fainal) {
getView().fainal(fainal);
}
});
}
}
自定义加减器:
public class Asease extends LinearLayout { private TextView aseas_add; private TextView aseas_jian; private TextView aseas_num; public DataCallBack dataCallBack; public Asease(Context context) { super(context); initContext(context); } public Asease(Context context, AttributeSet attrs) { super(context, attrs); initContext(context); } public Asease(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initContext(context); } private void initContext(Context context) { View inflate = LayoutInflater.from(context).inflate(R.layout.asease, this, true); aseas_add = (TextView) inflate.findViewById(R.id.aseas_add); aseas_jian = (TextView) inflate.findViewById(R.id.aseas_jian); aseas_num = (TextView) inflate.findViewById(R.id.aseas_num); initData(); } private void initData() { aseas_add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String s = aseas_num.getText().toString(); Integer nums = Integer.valueOf(s); nums++; aseas_num.setText(nums+""); dataCallBack.back(nums); } }); aseas_jian.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String ss = aseas_num.getText().toString(); Integer nums = Integer.valueOf(ss); nums--; if (nums<0){ nums=0; Toast.makeText(getContext(), "最小值", Toast.LENGTH_SHORT).show(); } aseas_num.setText(nums+""); dataCallBack.back(nums); } }); } public void setDataCallBack(DataCallBack dataCallBack){ this.dataCallBack = dataCallBack; } public interface DataCallBack{ void back(int numa); } public void setNum(int nums){ aseas_num.setText(nums+""); } }
分类以及购物车布局:
自定义加减器布局:
总的分类左边、右边布局:
总的购物车布局:
fragment_base.xml布局:
分类右边展示布局:
left_adapter左边布局:
购物车分页布局:
gouwu_name布局:
在strings.xml布局里边加:
Hello blank fragment
加greendao初始化:
public class App extends Application {
public static DaoSession daoSession;
public static Context context;
@Override
public void onCreate() {
super.onCreate();
context=this;
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "myapp.db");
SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(writableDatabase);
daoSession = daoMaster.newSession();
}
}