/**
* 分类
*/
public class ClassFragment extends Fragment implements CatagoryView, ProductCatagoryView {
private View view;
private ImageView mIvZxing;
private RecyclerView mRvLeft;
private ImageView mIv;
private ExpandableListView mElv;
private CatagoryPresenterImp catagoryPresenterImp;
private ProductCatagoryPresenterImp productCatagoryPresenterImp;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_class, null);
//初始化组件
initView(view);
//先绑定
catagoryPresenterImp = new CatagoryPresenterImp(this);
productCatagoryPresenterImp = new ProductCatagoryPresenterImp(this);
//请求数据
catagoryPresenterImp.getCatagory();
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
//解绑
catagoryPresenterImp.detach();
}
private void initView(View view) {
mIvZxing = (ImageView) view.findViewById(R.id.ivZxing);
mRvLeft = (RecyclerView) view.findViewById(R.id.rvLeft);
mIv = (ImageView) view.findViewById(R.id.iv);
mElv = (ExpandableListView) view.findViewById(R.id.elv);
mIv.setBackgroundResource(R.drawable.timg);
}
@Override
public void showData(final List<CatagoryBean.DataBean> data) {
//设置布局管理器
mRvLeft.setLayoutManager(new LinearLayoutManager(getContext()));
mRvLeft.addItemDecoration(new DividerItemDecoration(getContext(), RecyclerView.VERTICAL));
//创建适配器
final RvLeftAdapter adapter = new RvLeftAdapter(getContext(), data);
//显示左侧列表数据
mRvLeft.setAdapter(adapter);
//根据左侧列表第一项的cid去请求右侧的数据
productCatagoryPresenterImp.getProductCatagory(data.get(0).getCid() + "");
//设置第一个为默认选中
adapter.changeCheck(0, true);
//设置点击事件
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int position) {
//改变DataBean里面check的属性值
adapter.changeCheck(position, true);
//请求右侧对应的数据
productCatagoryPresenterImp.getProductCatagory(data.get(position).getCid() + "");
}
@Override
public void onLongItemClick(int position) {
}
});
}
@Override
public void showData(List<String> groupList, List<List<ProductCatagoryBean.DataBean.ListBean>> childList) {
//使用ExpandableListView展示数据
ElvAdapter elvAdapter = new ElvAdapter(getContext(), groupList, childList);
mElv.setAdapter(elvAdapter);
//默认展开列表
for (int i = 0; i < groupList.size(); i++) {
mElv.expandGroup(i);
}
}
}
首页
/**
* 首页
*/
public class HomePageFragment extends Fragment implements AdView, CatagoryView {
private Banner banner;
private AdPresenterImp adPresenterImp;
private CatagoryPresenterImp catagoryPresenterImp;
private RecyclerView rvClass;
private MarqueeView marqueeView;
private RecyclerView rvSecKill;
private RecyclerView rvRecommend;
private ImageView ivZxing;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
savedInstanceState) {
//绑定
adPresenterImp = new AdPresenterImp(this);
catagoryPresenterImp = new CatagoryPresenterImp(this);
//请求数据
adPresenterImp.getAd();
catagoryPresenterImp.getCatagory();
View view = inflater.inflate(R.layout.fragment_home, null);
initView(view);
setListener();
return view;
}
/**
* 设置监听事件
*/
private void setListener() {
ivZxing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), CaptureActivity.class);
startActivityForResult(intent, 1);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && data != null) {
Bundle bundle = data.getExtras();
if(bundle.getInt(CodeUtils.RESULT_TYPE)==CodeUtils.RESULT_SUCCESS){
String string = bundle.getString(CodeUtils.RESULT_STRING);
//拿到最终结果
//Intent intent = new Intent(getContext(),WebViewActivity.class);
}
}
}
/**
* 初始化组件
*
* @param view
*/
private void initView(View view) {
marqueeView = view.findViewById(R.id.marqueeView);
initMarqueeView();
banner = (Banner) view.findViewById(R.id.banner);
rvClass = view.findViewById(R.id.rvClass);
//设置布局管理器
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2, RecyclerView.HORIZONTAL, false);
rvClass.setLayoutManager(gridLayoutManager);
rvSecKill = view.findViewById(R.id.rvSecKill);
//设置布局管理器
GridLayoutManager gridLayoutManager1 = new GridLayoutManager(getContext(), 1, RecyclerView.HORIZONTAL, false);
rvSecKill.setLayoutManager(gridLayoutManager1);
//设置布局管理器
rvRecommend = view.findViewById(R.id.rvRecommend);
GridLayoutManager gridLayoutManager2 = new GridLayoutManager(getContext(), 2, RecyclerView.VERTICAL, false);
rvRecommend.setLayoutManager(gridLayoutManager2);
//设置图片加载器
banner.setImageLoader(new GlideImageLoader());
//二维码
ivZxing = view.findViewById(R.id.ivZxing);
}
@Override
public void onStop() {
super.onStop();
//结束轮播
banner.stopAutoPlay();
}
/**
* 初始化MarqueeView数据
*/
private void initMarqueeView() {
List<String> info = new ArrayList<>();
info.add("欢迎访问京东app");
info.add("大家有没有在 听课");
info.add("是不是还有人在睡觉");
info.add("你妈妈在旁边看着呢");
info.add("赶紧的好好学习吧 马上毕业了");
info.add("你没有时间睡觉了");
marqueeView.startWithList(info);
}
@Override
public void onDestroy() {
super.onDestroy();
//解绑
adPresenterImp.detach();
}
/**
* 显示banner轮播图
*/
@Override
public void show(AdBean adBean) {
List<AdBean.DataBean> data = adBean.getData();
List<String> list = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
list.add(data.get(i).getIcon());
}
//设置图片集合
banner.setImages(list);
//banner设置方法全部调用完毕时最后调用
banner.start();
//显示秒杀
showSeckill(adBean);
//显示为您推荐
showRecommend(adBean);
}
private void showRecommend(AdBean adBean) {
List<AdBean.TuijianBean.ListBean> list = adBean.getTuijian().getList();
RvRecommendAdapter rvRecommendAdapter = new RvRecommendAdapter(getContext(), list);
rvRecommend.setAdapter(rvRecommendAdapter);
}
/**
* 显示秒杀
*
* @param adBean
*/
private void showSeckill(AdBean adBean) {
final AdBean.MiaoshaBean miaosha = adBean.getMiaosha();
//给RecyclerView设置适配器
RvSecKillAdapter rvSecKillAdapter = new RvSecKillAdapter(getContext(), miaosha.getList());
rvSecKill.setAdapter(rvSecKillAdapter);
rvSecKillAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int position) {
//跳转显示详情
//获取地址
String detailUrl = miaosha.getList().get(position).getDetailUrl();
Intent intent = new Intent(getContext(), WebViewActivity.class);
intent.putExtra("detailUrl", detailUrl);
startActivity(intent);
}
@Override
public void onLongItemClick(int position) {
}
});
}
@Override
public void showError(Exception e) {
}
/**
* 显示商品分类,即banner下面的那一部分
*
* @param data
*/
@Override
public void showData(final List<CatagoryBean.DataBean> data) {
//把数据给RecyclerView显示
//给RecyclerView设置适配器
RvClassAdapter rvClassAdapter = new RvClassAdapter(getContext(), data);
rvClass.setAdapter(rvClassAdapter);
rvClassAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(getContext(), data.get(position).getName(), Toast.LENGTH_SHORT).show();
}
@Override
public void onLongItemClick(int position) {
}
});
}
}