Api
HomeFragment
package com.bwie.jd.view.fragment;
public class HomeFragment extends Fragment implements IMainView {
@BindView(R.id.banner)
FlyBanner banner;
Unbinder unbinder;
@BindView(R.id.rec)
RecyclerView rec;
@BindView(R.id.sao)
ImageView sao;
@BindView(R.id.notice_view)
NoticeView noticeView;
@BindView(R.id.miaosha)
TextView mMssss;
@BindView(R.id.MiaoRec)
RecyclerView MiaoRec;
@BindView(R.id.TuiRec)
RecyclerView TuiRec;
@BindView(R.id.sou)
ImageView sou;
private MainPresenter mainPresenter;
private List images = new ArrayList<>();
private int REQUEST_CODE = 200;
int miaosha = 1000000;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
mainPresenter = new MainPresenter();
mainPresenter.attachView(this);
mainPresenter.loadDataFromNet();
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onSuccess(HomeBean.DataBean data) {
for (int i = 0; i < data.getBanner().size(); i++) {
// Log.e("tag", data.getFenlei().get(i).getName());
images.add(data.getBanner().get(i).getIcon());
}
banner.setImagesUrl(images);
banner.setOnItemClickListener(new FlyBanner.OnItemClickListener() {
@Override
public void onItemClick(int position) {
}
});
//九宫格
List<HomeBean.DataBean.FenleiBean> fenlei = data.getFenlei();
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false);
rec.setLayoutManager(layoutManager);
rec.setAdapter(new JiuAdapter(getActivity(), fenlei));
//推送
init();
//秒杀
miao();
List<HomeBean.DataBean.MiaoshaBean.ListBean> list = data.getMiaosha().getList();
GridLayoutManager manager = new GridLayoutManager(getActivity(), 1, GridLayoutManager.HORIZONTAL, false);
MiaoRec.setLayoutManager(manager);
MiaoRec.setAdapter(new MiaoAdapter(getActivity(), list));
//推荐
List<HomeBean.DataBean.TuijianBean.ListBeanX> list1 = data.getTuijian().getList();
GridLayoutManager manager1 = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
TuiRec.setLayoutManager(manager1);
TuiRec.setAdapter(new TuiAdapter(getActivity(), list1));
}
//秒杀
private void miao() {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
CountDownTimer countDownTimer = new CountDownTimer(miaosha, 1000) {
@Override
public void onTick(long l) {
String format = simpleDateFormat.format(new Date(l));
mMssss.setText("京东秒杀:" + format);
}
@Override
public void onFinish() {
}
}.start();
}
//推送
private void init() {
List<String> notices = new ArrayList<>();
notices.add("董雨杉大促销下单拆福袋,亿万新年红包随便拿");
notices.add("董雨杉家电五折团,抢十亿无门槛现金红包");
notices.add("星球大战董雨杉剃须刀首发送200元代金券");
noticeView.addNotice(notices);
noticeView.startFlipping();
}
@Override
public void onErr(int errCode, String errMsg) {
}
@Override
public void rightSuccess(List<FenRightBean.DataBean> msg) {
}
//销毁
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
mainPresenter.dettachView();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/**
* 处理二维码扫描结果
*/
if (requestCode == REQUEST_CODE) {
//处理扫描结果(在界面上显示)
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();
}
}
}
}
@OnClick({R.id.sao, R.id.sou})
public void onViewClicked(View view) {
switch (view.getId()) {
//扫一扫点击事件
case R.id.sao:
Intent intent = new Intent(getActivity(), CaptureActivity.class);
startActivityForResult(intent, REQUEST_CODE);
break;
//搜索
case R.id.sou:
Intent intent2 = new Intent(getActivity(), SouActivity.class);
startActivity(intent2);
break;
}
}
}
ImainView
package com.bwie.jd.view.IView;
import com.bwie.jd.model.bean.FenRightBean;
import com.bwie.jd.model.bean.HomeBean;
import java.util.List;
public interface IMainView extends IBaseView{
void onSuccess(HomeBean.DataBean data);
void onErr(int errCode,String errMsg);
void rightSuccess(List<FenRightBean.DataBean> msg);
}
App
package com.bwie.jd.view.util;
import android.app.Application;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
ZXingLibrary.initDisplayOpinion(this);
UMConfigure.init(this,“5a12384aa40fa3551f0001d1”
,“umeng”,UMConfigure.DEVICE_TYPE_PHONE,"");
PlatformConfig.setQQZone(“100424468”, “c7394704798a158208a74ab60104f0ba”);
UMConfigure.setLogEnabled(true);
}
}
IMainPresenter
package com.bwie.jd.presenter;
import android.util.Log;
import com.bwie.jd.model.bean.HomeBean;
import com.bwie.jd.model.bean.UserInfo;
import com.bwie.jd.model.http.HttpUtils;
import com.bwie.jd.view.IView.IMainView;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class MainPresenter extends BasePresenter{
public void loadDataFromNet() {
Observable body =HttpUtils.getHttpUtilsInstance().apiClient.getHome();
body.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(HomeBean homeBean) {
HomeBean.DataBean data = homeBean.getData();
getView().onSuccess(data);
}
@Override
public void onError(Throwable e) {
Log.i("eee",""+e);
}
@Override
public void onComplete() {
}
});
}
}
TuiAdapter
package com.bwie.jd.view.adapter;
import java.util.List;
public class TuiAdapter extends RecyclerView.Adapter<TuiAdapter.ListHolder>{
private Context context;
private List<HomeBean.DataBean.TuijianBean.ListBeanX> data;
public TuiAdapter(Context context, List<HomeBean.DataBean.TuijianBean.ListBeanX> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public ListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item3, parent, false);
ListHolder holder = new ListHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ListHolder holder, final int position) {
Uri uri = Uri.parse(data.get(position).getImages().split("\\|")[0]);
holder.img.setImageURI(uri);
holder.text01.setText(data.get(position).getTitle());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, InfoActivity.class);
intent.putExtra("pid",data.get(position).getPid());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return data.size();
}
class ListHolder extends RecyclerView.ViewHolder{
private final SimpleDraweeView img;
private final TextView text01;
public ListHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
text01 = itemView.findViewById(R.id.text01);
}
}
}
布局