activity_main
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.bwie.test.myyuekaoshi.MainActivity"> <TextView android:id="@+id/t" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:textSize="20sp" android:text="首页"/> <ScrollView android:layout_below="@id/t" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.bwie.test.zidingyi.MyCreateLayout android:id="@+id/myCreate_layout" android:layout_width="match_parent" android:layout_height="200dp"> </com.bwie.test.zidingyi.MyCreateLayout> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:textSize="20sp" android:text="京东秒杀"/> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_miaosha" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:textSize="20sp" android:text="为你推荐"/> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_tuijian" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout> </ScrollView> </RelativeLayout>
activity_main2
<?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="com.bwie.test.myyuekaoshi.Main2Activity"> <com.bwie.test.zidingyi.GramophoneView android:id="@+id/gramophone" app:src="@drawable/ic_launcher_background" app:picture_radiu="80dp" app:disk_rotate_speed="1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:layout_marginTop="60dp" android:onClick="pauseOrstart" android:text="切换播放状态" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
activity_gou_wu_che
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.bwie.test.myyuekaoshi.GouWuCheActivity"> <RelativeLayout android:id="@+id/re" android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:layout_centerVertical="true" android:background="@drawable/leftjiantou"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="20sp" android:text="购物车"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:textSize="16sp" android:text="编辑"/> </RelativeLayout> <ScrollView android:layout_below="@id/re" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> <TextView android:layout_below="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="100dp" /> </RelativeLayout> </ScrollView> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:background="#fff"> <CheckBox android:id="@+id/che_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/che_all" android:text="全选" android:textSize="25sp" /> <TextView android:id="@+id/num_all" android:layout_width="110dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:background="#ff0000" android:gravity="center" android:text="去支付(1)" android:textColor="#fff" android:textSize="16sp" /> <TextView android:id="@+id/sum_all" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/num_all" android:gravity="center" android:text="¥0.00" android:textSize="16sp" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_toLeftOf="@+id/sum_all" android:gravity="center" android:text="合计:" android:textSize="20sp" /> </RelativeLayout> </RelativeLayout>
item_banner
<?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"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="220dp"> </android.support.v4.view.ViewPager> //实现小圆点 <LinearLayout android:id="@+id/linear_layout" android:layout_alignBottom="@+id/vp" android:layout_marginBottom="10dp" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> </RelativeLayout>
item_child
<?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="120dp" android:background="#fff" android:padding="10dp"> <CheckBox android:id="@+id/che_child" android:layout_marginLeft="5dp" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/image_child" android:layout_toRightOf="@+id/che_child" android:layout_marginLeft="15dp" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:id="@+id/text_childtitle" android:layout_toRightOf="@+id/image_child" android:layout_marginLeft="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="50dp" android:maxLines="2" android:text="1111"/> <TextView android:id="@+id/text_chidprice" android:layout_toRightOf="@+id/image_child" android:layout_alignParentBottom="true" android:layout_marginLeft="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#ff0000" android:text="000"/> <LinearLayout android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_width="100dp" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/text_jian" android:layout_width="0dp" android:layout_weight="2" android:paddingTop="2dp" android:paddingBottom="2dp" android:paddingLeft="6dp" android:paddingRight="6dp" android:layout_height="wrap_content" android:background="@drawable/shape_kuang" android:textSize="20sp" android:text="-"/> <TextView android:id="@+id/text_childsum" android:layout_width="0dp" android:layout_weight="3" android:paddingTop="2dp" android:paddingBottom="2dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:gravity="center" android:layout_height="wrap_content" android:textSize="20sp" android:background="@drawable/shape_kuang" android:text="1"/> <TextView android:id="@+id/text_jia" android:layout_width="0dp" android:layout_weight="2" android:paddingTop="2dp" android:paddingBottom="2dp" android:paddingLeft="6dp" android:paddingRight="6dp" android:layout_height="wrap_content" android:background="@drawable/shape_kuang" android:textSize="20sp" android:text="+"/> </LinearLayout> </RelativeLayout>
item_group
<?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="wrap_content"> <RelativeLayout android:id="@+id/r" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/che_group" android:layout_marginLeft="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_group" android:layout_marginLeft="15dp" android:layout_toRightOf="@+id/che_group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp"/> <TextView android:layout_alignParentRight="true" android:textColor="#ff0000" android:layout_marginRight="15dp" android:textSize="20dp" android:text="优惠券" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_child" android:layout_below="@+id/r" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </RelativeLayout>
item_miaosha
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/sdv_miao" android:layout_width="90dp" android:layout_height="90dp" app:actualImageScaleType="centerCrop"/> </RelativeLayout>
item_tuijian
<?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="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/sdv_tuijian" android:layout_width="match_parent" android:layout_height="230dp" android:padding="5dp" app:actualImageScaleType="centerCrop"/> <TextView android:id="@+id/text_tite" android:layout_below="@id/sdv_tuijian" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
item_view
<?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"> <LinearLayout android:layout_width="200dp" android:layout_centerInParent="true" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/textjian" android:layout_width="0dp" android:layout_weight="1" android:paddingTop="2dp" android:paddingBottom="2dp" android:gravity="center" android:layout_height="wrap_content" android:background="@drawable/shape_kuang" android:textSize="20sp" android:text="-"/> <EditText android:id="@+id/edit_childsum" android:layout_width="0dp" android:layout_weight="3" android:paddingTop="2dp" android:paddingBottom="2dp" android:gravity="center" android:layout_height="wrap_content" android:textSize="20sp" android:background="@drawable/shape_kuang"/> <TextView android:id="@+id/textjia" android:layout_width="0dp" android:layout_weight="1" android:paddingTop="2dp" android:paddingBottom="2dp" android:gravity="center" android:layout_height="wrap_content" android:background="@drawable/shape_kuang" android:textSize="20sp" android:text="+"/> </LinearLayout> </RelativeLayout>
drawable文件夹下shape_01
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="10dp"/> <solid android:color="#00ff00"/> <size android:width="10dp" android:height="10dp"/> </shape>
shape_02
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="10dp"/> <solid android:color="#ff0000"/> <size android:width="10dp" android:height="10dp"/> </shape>
shape_kuang
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#fff"/> <stroke android:color="#000" android:width="0.1dp"/> </shape>
values文件夹下attrs
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="GramophoneView"> <attr name="picture_radiu" format="dimension"/> //中间图片的半径 <attr name="src" format="reference"/> //图片 <attr name="disk_rotate_speed" format="float"/> //唱片旋转的速度 </declare-styleable> </resources>
MainActivity
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.Toast; import com.bwie.test.adapter.MyMiaoShaAdapter; import com.bwie.test.adapter.MyTuiJianAdapter; import com.bwie.test.bean.ShouYeBean; import com.bwie.test.presenter.Presenter; import com.bwie.test.util.ApiUrl; import com.bwie.test.util.RetrofitUtil; import com.bwie.test.view.MyView; import com.bwie.test.view.SetClicked; import com.bwie.test.zidingyi.MyCreateLayout; import com.google.gson.Gson; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import butterknife.BindView; import butterknife.ButterKnife; import okhttp3.ResponseBody; public class MainActivity extends AppCompatActivity implements MyView{ @BindView(R.id.myCreate_layout) MyCreateLayout myCreateLayout; @BindView(R.id.recycler_miaosha) RecyclerView recyclerMiaosha; @BindView(R.id.recycler_tuijian) RecyclerView recyclerTuijian; private Presenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //查找控件 ButterKnife.bind(this); presenter = new Presenter(this); presenter.attachView(this); Map<String, String> map = new HashMap<>(); presenter.getUrl(RetrofitUtil.getSerVice().doGet(ApiUrl.shouye, map)); } @Override public void getResponseBody(ResponseBody responseBody) { try { ShouYeBean shouYeBean = new Gson().fromJson(responseBody.string(), ShouYeBean.class); final List<ShouYeBean.DataBean> data = shouYeBean.getData(); myCreateLayout.setImage(data); //设置点击事件 myCreateLayout.setClickListner(new MyCreateLayout.OnClickLisner() { @Override public void onItemClick(int position) { if (data.get(position).getType() == 0) { Intent intent = new Intent(MainActivity.this, Main2Activity.class); intent.putExtra("url", data.get(position).getUrl()); startActivity(intent); } else { Toast.makeText(MainActivity.this, "我要跳转到商品详情页", Toast.LENGTH_SHORT).show(); } } }); recyclerMiaosha.setLayoutManager(new LinearLayoutManager(this ,LinearLayoutManager.HORIZONTAL,false)); MyMiaoShaAdapter adapter = new MyMiaoShaAdapter(this, shouYeBean); recyclerMiaosha.setAdapter(adapter); adapter.setonclick(new SetClicked() { @Override public void onClicked(int position) { Intent intent = new Intent(MainActivity .this, GouWuCheActivity.class); startActivity(intent); } }); recyclerTuijian.setLayoutManager(new GridLayoutManager(this ,2,GridLayoutManager.VERTICAL,false)); MyTuiJianAdapter adapter1 = new MyTuiJianAdapter(this, shouYeBean); recyclerTuijian.setAdapter(adapter1); adapter1.setonclick(new SetClicked() { @Override public void onClicked(int position) { Intent intent = new Intent(MainActivity .this, GouWuCheActivity.class); startActivity(intent); } }); } catch (IOException e) { e.printStackTrace(); } } @Override public void getError(Throwable throwable) { } @Override protected void onDestroy() { super.onDestroy(); presenter.getJieYue(); if (presenter != null) { presenter.detachView(); } } }
Main2Activity
import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import com.bwie.test.myyuekaoshi.R; import com.bwie.test.zidingyi.GramophoneView; public class Main2Activity extends AppCompatActivity { private GramophoneView gramophoneView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); gramophoneView = (GramophoneView) findViewById(R.id.gramophone); } public void pauseOrstart(View view) { gramophoneView.pauseOrstart(); } }
GouWuCheActivity
import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.CheckBox; import android.widget.TextView; import com.bwie.test.adapter.MyChaXunAdapter; import com.bwie.test.bean.MyBean; import com.bwie.test.bean.MyHeJiBean; import com.bwie.test.presenter.Presenter; import com.bwie.test.util.ApiUrl; import com.bwie.test.util.RetrofitUtil; import com.bwie.test.view.MyView; import com.google.gson.Gson; import java.io.IOException; import java.util.HashMap; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import okhttp3.ResponseBody; public class GouWuCheActivity extends AppCompatActivity implements MyView{ @BindView(R.id.recycler_view) RecyclerView recyclerView; @BindView(R.id.che_all) CheckBox cheAll; @BindView(R.id.num_all) TextView numAll; @BindView(R.id.sum_all) TextView sumAll; private Presenter presenter; private int num; private String price; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==0){ MyHeJiBean myHeJiBean= (MyHeJiBean) msg.obj; sumAll.setText("合计:¥"+myHeJiBean.getPrice()); numAll.setText("去结算("+myHeJiBean.getNum()+")"); num = myHeJiBean.getNum(); price = myHeJiBean.getPrice(); } } }; private MyChaXunAdapter adapter; private HashMap<String, String> map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gou_wu_che); ButterKnife.bind(this); presenter = new Presenter(this); map = new HashMap<>(); map.put("uid","4427"); } @Override protected void onResume() { super.onResume(); presenter.getUrl(RetrofitUtil.getSerVice().doGet(ApiUrl.select,map)); presenter.attachView(this); } @Override public void getResponseBody(ResponseBody responseBody) { try { MyBean myBean = new Gson().fromJson(responseBody.string(), MyBean.class); List<MyBean.DataBean> list = myBean.getData(); //二级列表选中一级列表选中 for (int i = 0; i<list.size(); i++){ if(ischecked(i,list)){ list.get(i).setCheckbox(true); } } cheAll.setChecked(isChild(list)); recyclerView.setLayoutManager(new LinearLayoutManager(GouWuCheActivity .this,LinearLayoutManager.VERTICAL,false)); adapter = new MyChaXunAdapter(this, myBean, presenter, handler,map); recyclerView.setAdapter(adapter); adapter.setNumSumAll(); } catch (IOException e) { e.printStackTrace(); } } @Override public void getError(Throwable throwable) { } private boolean isChild(List<MyBean.DataBean> list){ for(int i=0; i<list.size(); i++){ if (!list.get(i).isCheckbox()){ return false; } } return true; } private boolean ischecked(int i, List<MyBean.DataBean> list){ for (int j=0; j<list.get(i).getList().size(); j++){ if (list.get(i).getList().get(j).getSelected()==0){ return false; } } return true; } @OnClick({R.id.che_all, R.id.num_all}) public void onClick(View view){ switch (view.getId()){ case R.id.che_all: if (adapter!=null){ adapter.allChed(cheAll.isChecked()); } break; case R.id.num_all: break; } } @Override protected void onDestroy() { super.onDestroy(); } }
adapter文件夹下MyChaXunAdapter
import android.content.Context; import android.os.Handler; import android.os.Message; 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 com.bwie.test.myyuekaoshi.R; import com.bwie.test.adapter.MyChildAdapter; import com.bwie.test.adapter.holder.MyChaXunHolder; import com.bwie.test.bean.MyBean; import com.bwie.test.bean.MyHeJiBean; import com.bwie.test.presenter.Presenter; import com.bwie.test.util.ApiUrl; import com.bwie.test.util.RetrofitUtil; import java.text.DecimalFormat; import java.util.HashMap; import java.util.List; import java.util.Map; import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; import okhttp3.ResponseBody; public class MyChaXunAdapter extends RecyclerView.Adapter<MyChaXunHolder> { private final Context context; private final MyBean myBean; private final Presenter presenter; private final Handler handler; private final HashMap<String, String> map; private int conindex; private MyChildAdapter adapter; public MyChaXunAdapter(Context context, MyBean myBean, Presenter presenter , Handler handler, HashMap<String, String> map) { this.context = context; this.myBean = myBean; this.presenter = presenter; this.handler = handler; this.map = map; } @Override public MyChaXunHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R .layout.item_group, parent, false); MyChaXunHolder holder = new MyChaXunHolder(view); return holder; } @Override public void onBindViewHolder(final MyChaXunHolder holder, final int position) { final List<MyBean.DataBean> data = myBean.getData(); holder.che_group.setChecked(data.get(position).isCheckbox()); holder.text_group.setText(data.get(position).getSellerName()); holder.che_group.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { conindex = 0; allChildChecked(holder.che_group.isChecked(),data.get(position)); } }); holder.recycler_child.setLayoutManager(new LinearLayoutManager(context ,LinearLayoutManager.VERTICAL,false)); adapter = new MyChildAdapter(context, myBean, handler, presenter,position,map); holder.recycler_child.setAdapter(adapter); } private void allChildChecked(final boolean checked, final MyBean.DataBean dataBean) { final MyBean.DataBean.ListBean bean = dataBean.getList().get(conindex); Map<String, String> params = new HashMap<>(); params.put("uid", "4427"); params.put("sellerid", String.valueOf(bean.getSellerid())); params.put("pid", String.valueOf(bean.getPid())); params.put("num", String.valueOf(bean.getNum())); params.put("selected", String.valueOf(checked ? 1 : 0)); RetrofitUtil.getSerVice().doGet(ApiUrl.gengxin,params) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<ResponseBody>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(ResponseBody value) { conindex++; if (conindex<dataBean.getList().size()){ allChildChecked(checked,dataBean); }else { presenter.getUrl(RetrofitUtil.getSerVice() .doGet(ApiUrl.select,map)); } } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } @Override public int getItemCount() { return myBean.getData().size(); } public void setNumSumAll() { double price=0; int num=0; List<MyBean.DataBean> data = myBean.getData(); for (int i=0;i<data.size();i++){ List<MyBean.DataBean.ListBean> beans = data.get(i).getList(); for (int j=0;j<beans.size();j++){ if (beans.get(j).getSelected()==1){ price+=beans.get(j).getBargainPrice()*beans.get(j).getNum(); num+=beans.get(j).getNum(); } } } DecimalFormat decimalFormat = new DecimalFormat("0.00"); String format = decimalFormat.format(price); MyHeJiBean myHeJiBean = new MyHeJiBean(format, num); Message msg = Message.obtain(); msg.what=0; msg.obj=myHeJiBean; handler.sendMessage(msg); } public void allChed(boolean checked) { adapter.allChed(checked); } }
MyChildAdapter
import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Paint; import android.os.Handler; 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.EditText; import android.widget.TextView; import com.bumptech.glide.Glide; import com.bwie.test.myyuekaoshi.R; import com.bwie.test.adapter.holder.MyChildHolder; import com.bwie.test.bean.MyBean; import com.bwie.test.presenter.Presenter; import com.bwie.test.util.ApiUrl; import com.bwie.test.util.RetrofitUtil; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; import okhttp3.ResponseBody; public class MyChildAdapter extends RecyclerView.Adapter<MyChildHolder> { private final Context context; private final MyBean myBean; private final Handler handler; private final Presenter presenter; private final int groupposition; private HashMap<String, String> map; private int conAllCheck; private TextView textjian; private EditText edit_childsum; private TextView textjia; private int num; public MyChildAdapter(Context context, MyBean myBean, Handler handler , Presenter presenter, int groupposition, HashMap<String, String> map) { this.context = context; this.myBean = myBean; this.handler = handler; this.presenter = presenter; this.groupposition = groupposition; this.map = map; } @Override public MyChildHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_child, parent , false); MyChildHolder holder = new MyChildHolder(view); return holder; } @Override public void onBindViewHolder(final MyChildHolder holder, int position) { List<MyBean.DataBean.ListBean> list = myBean.getData().get(groupposition) .getList(); final MyBean.DataBean.ListBean listBean = myBean.getData() .get(groupposition).getList().get(position); String[] split = listBean.getImages().split("\\|"); Glide.with(context).load(split[0]).into(holder.image_child); holder.text_childtitle.setText(listBean.getTitle()); holder.text_chidprice.setText("¥"+listBean.getBargainPrice()); holder.text_childsum.setText(listBean.getNum()+""); //holder.text_chidprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线 holder.text_chidprice.getPaint().setFlags(Paint .STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰 holder.che_child.setChecked(Boolean.parseBoolean(String.valueOf(listBean .getSelected()==0?false:true))); holder.text_childsum.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(context); View view = View.inflate(context, R.layout.item_view, null); textjian = view.findViewById(R.id.textjian); edit_childsum = view.findViewById(R.id.edit_childsum); textjia = view.findViewById(R.id.textjia); edit_childsum.setText(listBean.getNum()+""); num = listBean.getNum(); textjian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (num ==1){ return; }else { num--; } //num--; edit_childsum.setText(num +""); } }); textjia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (num ==200){ return; }else { num++; } edit_childsum.setText(num+""); } }); final String s = edit_childsum.getText().toString(); builder.setTitle("修改购买数量") .setIcon(android.R.drawable.ic_dialog_info) .setView(view) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.d("-------000",s); Map<String, String> params=new HashMap<>(); params.put("uid","4427"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("num", String.valueOf(edit_childsum.getText().toString())); params.put("selected", String.valueOf(listBean.getSelected())); RetrofitUtil.getSerVice().doGet(ApiUrl.gengxin,params) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<ResponseBody>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(ResponseBody value) { presenter.getUrl(RetrofitUtil.getSerVice() .doGet(ApiUrl.select,map)); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); holder.text_childsum.setText(edit_childsum.getText().toString()+""); } }) .setNegativeButton("取消", null) .show(); } }); holder.che_child.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Map<String, String> params=new HashMap<>(); params.put("uid","4427"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("num", String.valueOf(listBean.getNum())); params.put("selected", String.valueOf(listBean.getSelected()==0?1:0)); RetrofitUtil.getSerVice().doGet(ApiUrl.gengxin,params) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<ResponseBody>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(ResponseBody value) { presenter.getUrl(RetrofitUtil.getSerVice() .doGet(ApiUrl.select,map)); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } }); holder.text_jia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Map<String, String> params=new HashMap<>(); params.put("uid","4427"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("num", String.valueOf(listBean.getNum()+1)); params.put("selected", String.valueOf(listBean.getSelected())); RetrofitUtil.getSerVice().doGet(ApiUrl.gengxin,params) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<ResponseBody>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(ResponseBody value) { presenter.getUrl(RetrofitUtil.getSerVice() .doGet(ApiUrl.select,map)); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } }); holder.text_jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num = listBean.getNum(); if (num==1){ return; } Map<String, String> params=new HashMap<>(); params.put("uid","4427"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("num", String.valueOf(listBean.getNum()-1)); params.put("selected", String.valueOf(listBean.getSelected())); RetrofitUtil.getSerVice().doGet(ApiUrl.gengxin,params) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<ResponseBody>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(ResponseBody value) { presenter.getUrl(RetrofitUtil.getSerVice() .doGet(ApiUrl.select,map)); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } }); } @Override public int getItemCount() { return myBean.getData().get(groupposition).getList().size(); } public void allChed(boolean checked) { ArrayList<MyBean.DataBean.ListBean> listBeans = new ArrayList<>(); List<MyBean.DataBean> data = myBean.getData(); for (int i=0;i<data.size();i++){ List<MyBean.DataBean.ListBean> list = data.get(i).getList(); for (int j=0;j<list.size();j++){ listBeans.add(list.get(j)); } } conAllCheck = 0; conAllChecked(listBeans,checked); } private void conAllChecked(final ArrayList<MyBean.DataBean.ListBean> listBeans, final boolean checked) { final MyBean.DataBean.ListBean listBean = listBeans.get(conAllCheck); Map<String, String> params = new HashMap<>(); params.put("uid", "4427"); params.put("sellerid", String.valueOf(listBean.getSellerid())); params.put("pid", String.valueOf(listBean.getPid())); params.put("num", String.valueOf(listBean.getNum())); params.put("selected", String.valueOf(checked ? 1 : 0)); RetrofitUtil.getSerVice().doGet(ApiUrl.gengxin,params) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<ResponseBody>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(ResponseBody value) { conAllCheck++; if (conAllCheck<listBeans.size()){ conAllChecked(listBeans,checked); }else { presenter.getUrl(RetrofitUtil.getSerVice() .doGet(ApiUrl.select,map)); } } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } }
MyMiaoShaAdapter
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.bwie.test.myyuekaoshi.MainActivity; import com.bwie.test.myyuekaoshi.R; import com.bwie.test.adapter.holder.MyMiaoShaHolder; import com.bwie.test.bean.ShouYeBean; import com.bwie.test.view.SetClicked; public class MyMiaoShaAdapter extends RecyclerView.Adapter<MyMiaoShaHolder> { private final Context context; private final ShouYeBean shouYeBean; private SetClicked setClicked; public MyMiaoShaAdapter(Context context, ShouYeBean shouYeBean) { this.context = context; this.shouYeBean = shouYeBean; } @Override public MyMiaoShaHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_miaosha, parent, false); MyMiaoShaHolder holder = new MyMiaoShaHolder(view); return holder; } @Override public void onBindViewHolder(MyMiaoShaHolder holder, final int position) { String[] split = shouYeBean.getMiaosha().getList().get(position).getImages().split("\\|"); holder.sdv_miao.setImageURI(split[0]); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setClicked.onClicked(position); } }); } @Override public int getItemCount() { return shouYeBean.getMiaosha().getList().size(); } public void setonclick(SetClicked setClicked) { this.setClicked = setClicked; } }
MyTuiJianAdapter
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.bwie.test.myyuekaoshi.R; import com.bwie.test.adapter.holder.MyTuiJianHolder; import com.bwie.test.bean.ShouYeBean; import com.bwie.test.view.SetClicked; public class MyTuiJianAdapter extends RecyclerView.Adapter<MyTuiJianHolder> { private final Context context; private final ShouYeBean shouYeBean; private SetClicked onclick; public MyTuiJianAdapter(Context context, ShouYeBean shouYeBean) { this.context = context; this.shouYeBean = shouYeBean; } @Override public MyTuiJianHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_tuijian, parent, false); MyTuiJianHolder holder = new MyTuiJianHolder(view); return holder; } @Override public void onBindViewHolder(MyTuiJianHolder holder, final int position) { ShouYeBean.TuijianBean.ListBean listBean = shouYeBean.getTuijian().getList().get(position); String[] split = listBean.getImages().split("\\|"); holder.sdv_tuijian.setImageURI(split[0]); holder.text_tite.setText(listBean.getTitle()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onclick.onClicked(position); } }); } @Override public int getItemCount() { return shouYeBean.getTuijian().getList().size(); } public void setonclick(SetClicked onclick) { this.onclick = onclick; } }
holder文件夹下MyChaXunHolder
import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.CheckBox; import android.widget.TextView; import com.bwie.test.myyuekaoshi.R; public class MyChaXunHolder extends RecyclerView.ViewHolder { public CheckBox che_group; public TextView text_group; public RecyclerView recycler_child; public MyChaXunHolder(View itemView) { super(itemView); che_group = itemView.findViewById(R.id.che_group); text_group = itemView.findViewById(R.id.text_group); recycler_child = itemView.findViewById(R.id.recycler_child); } }
MyChildHolder
import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; import com.bwie.test.myyuekaoshi.R; public class MyChildHolder extends RecyclerView.ViewHolder { public CheckBox che_child; public ImageView image_child; public TextView text_childtitle; public TextView text_chidprice; public TextView text_jian; public TextView text_childsum; public TextView text_jia; public MyChildHolder(View itemView) { super(itemView); che_child = itemView.findViewById(R.id.che_child); image_child = itemView.findViewById(R.id.image_child); text_childtitle = itemView.findViewById(R.id.text_childtitle); text_chidprice = itemView.findViewById(R.id.text_chidprice); text_jian = itemView.findViewById(R.id.text_jian); text_childsum = itemView.findViewById(R.id.text_childsum); text_jia = itemView.findViewById(R.id.text_jia); } }
MyMiaoShaHolder
import android.support.v7.widget.RecyclerView; import android.view.View; import com.bwie.test.myyuekaoshi.R; import com.facebook.drawee.view.SimpleDraweeView; public class MyMiaoShaHolder extends RecyclerView.ViewHolder { public SimpleDraweeView sdv_miao; public MyMiaoShaHolder(View itemView) { super(itemView); sdv_miao = itemView.findViewById(R.id.sdv_miao); } }
MyTuiJianHolder
import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; import com.bwie.test.myyuekaoshi.R; import com.facebook.drawee.view.SimpleDraweeView; public class MyTuiJianHolder extends RecyclerView.ViewHolder { public SimpleDraweeView sdv_tuijian; public TextView text_tite; public MyTuiJianHolder(View itemView) { super(itemView); sdv_tuijian = itemView.findViewById(R.id.sdv_tuijian); text_tite = itemView.findViewById(R.id.text_tite); } }
zidingyi文件夹下GramophoneView
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import com.bwie.test.myyuekaoshi.R; public class GramophoneView extends View{ private final float DEFUALT_DISK_ROTATE_SPEED = 1f; //磁盘旋转的速度 private final float DEFUALT_PICTURE_RAUID = 200; //中间图片默认半径 private final float DEFUALT_PAUSE_NEEDLE_DEGREE = -45; //暂停状态时唱针的旋转角度 private final float DEFUALT_PLAYING_NEEDLE_DEGREE = -15; //播放状态时唱针的旋转角度 private int pictrueRadio; //中间图片的半径 //指针 private int smallCircleRadiu = 10; //唱针顶部小圆半径,减小了一半 private int bigCircleRadiu = 15; //唱针顶部大圆半径,减小了一半 private int shortArmLength; private int longArmleLength; // 唱针手臂,较长那段的长度 private int shortHeadLength; // 唱针的头,较短那段的长度 private int longHeadLength; private Paint needlePaint; //唱片 private float halfMeasureWidth; private int diskRingWidth; // 黑色圆环宽度 private float diskRotateSpeed; // 唱片旋转速度 private Bitmap pictureBitmap; private Paint diskPaint; //状态控制 private boolean isPlaying; private float currentDiskDegree; // 唱片旋转角度 private float currentNeddleDegree = DEFUALT_PLAYING_NEEDLE_DEGREE; // 唱针旋转角度 public GramophoneView(Context context) { this(context,null); } public GramophoneView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); needlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿 diskPaint = new Paint(Paint.ANTI_ALIAS_FLAG); TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.GramophoneView); // 拿到xml中的图片和图片半径和,旋转的度数 pictrueRadio = (int) typedArray.getDimension(R.styleable.GramophoneView_picture_radiu, DEFUALT_PICTURE_RAUID); diskRotateSpeed = typedArray.getFloat(R.styleable.GramophoneView_disk_rotate_speed, DEFUALT_DISK_ROTATE_SPEED); Drawable drawable = typedArray.getDrawable(R.styleable.GramophoneView_src); if (drawable == null) { pictureBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); } else { pictureBitmap = ((BitmapDrawable)drawable).getBitmap(); } //初始化唱片的变量 diskRingWidth = pictrueRadio >> 1; shortHeadLength = (pictrueRadio + diskRingWidth) / 15; //图片半径和黑色圆环的和 等于 指针的总长度 longHeadLength = shortHeadLength << 1; //左移相当于乘以2 shortArmLength = longHeadLength << 1; longArmleLength = shortArmLength << 1; } /** * 理想的宽高是,取决于picture的 半径的 * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //我们想要的理想宽高 int width = (pictrueRadio+diskRingWidth)*2; int hight = (pictrueRadio+diskRingWidth)*2+longArmleLength; //根据我们理想的宽和高 和xml中设置的宽高,按resolveSize规则做最后的取舍 //resolveSize规则 1、精确模式,按 int measurewidth = resolveSize(width,widthMeasureSpec); int measurehight = resolveSize(hight,heightMeasureSpec); setMeasuredDimension(measurewidth,measurehight);//设置测量的长度 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); halfMeasureWidth = getMeasuredWidth() >> 1; drawDisk(canvas); //画唱片 drawNeedle(canvas);//画指针 if (currentNeddleDegree > DEFUALT_PAUSE_NEEDLE_DEGREE) { invalidate(); } } private void drawNeedle(Canvas canvas) {//指针 canvas.save();//保存 //移动坐标原点,画指针第一段 canvas.translate(halfMeasureWidth, 0); canvas.rotate(currentNeddleDegree); needlePaint.setColor(Color.parseColor("#C0C0C0")); needlePaint.setStrokeWidth(10); canvas.drawLine(0, 0, 0, longArmleLength, needlePaint); //画指针第二段 canvas.translate(0, longArmleLength); canvas.rotate(-30);// needlePaint.setStrokeWidth(10); canvas.drawLine(0, 0, 0, shortArmLength, needlePaint); //画指针第三段 canvas.translate(0, shortArmLength); needlePaint.setStrokeWidth(15); canvas.drawLine(0, 0, 0, longHeadLength, needlePaint); //画指针的第四段 canvas.translate(0, longHeadLength); needlePaint.setStrokeWidth(25); canvas.drawLine(0, 0, 0, shortHeadLength, needlePaint); canvas.restore(); //画指针的支点 canvas.save(); canvas.translate(halfMeasureWidth, 0); needlePaint.setColor(Color.parseColor("#8A8A8A")); needlePaint.setStyle(Paint.Style.FILL); canvas.drawCircle(0, 0, bigCircleRadiu, needlePaint); needlePaint.setColor(Color.parseColor("#C0C0C0")); canvas.drawCircle(0, 0, smallCircleRadiu, needlePaint); canvas.restore(); //当前如果是播放的话,就移动到播放的位置 ,因为逆时针旋转度数是负的所以,- + 需要注意 if (isPlaying) { if (currentNeddleDegree < DEFUALT_PLAYING_NEEDLE_DEGREE) { //不是暂停状态,就是播放状态,或者是切换中状态 currentNeddleDegree += 3; //切换中状态指针是要有动画效果的,所有要改变指针的度数 } } else { if (currentNeddleDegree > DEFUALT_PAUSE_NEEDLE_DEGREE) { currentNeddleDegree -= 3; } } } private void drawDisk(Canvas canvas) { currentDiskDegree = currentDiskDegree % 360 + diskRotateSpeed; canvas.save(); canvas.translate(halfMeasureWidth, longArmleLength + diskRingWidth + pictrueRadio); canvas.rotate(currentDiskDegree); diskPaint.setColor(Color.BLACK); diskPaint.setStyle(Paint.Style.STROKE); diskPaint.setStrokeWidth(pictrueRadio / 2); // diskPaint.setStrokeWidth(20); canvas.drawCircle(0, 0, pictrueRadio + diskRingWidth / 2, diskPaint); Path path = new Path(); // 裁剪的path路径 (为了裁剪成圆形图片,其实是将画布剪裁成了圆形) path.addCircle(0, 0, pictrueRadio, Path.Direction.CW); canvas.clipPath(path); Rect src = new Rect(); //将要画bitmap的那个范围 src.set(0, 0, pictureBitmap.getWidth(), pictureBitmap.getHeight()); Rect dst = new Rect(); dst.set(-pictrueRadio, -pictrueRadio, pictrueRadio, pictrueRadio); //将要将bitmap画要坐标系的那个位置 canvas.drawBitmap(pictureBitmap, src, dst, null); canvas.restore(); } public void pauseOrstart(){ isPlaying =!isPlaying; invalidate(); } /** * 设置图片半径 * @param pictureRadius 图片半径 */ public void setPictureRadius(int pictureRadius) { this.pictrueRadio = pictureRadius; } /** * 设置唱片旋转速度 * @param diskRotateSpeed 旋转速度 */ public void setDiskRotateSpeed(float diskRotateSpeed) { this.diskRotateSpeed = diskRotateSpeed; } /** * 设置图片资源id * @param resId 图片资源id */ public void setPictureRes(int resId) { pictureBitmap = BitmapFactory.decodeResource(getContext().getResources(), resId); invalidate(); } }
MyApp
import android.app.Application; import com.facebook.drawee.backends.pipeline.Fresco; public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
MyCreateLayout
import android.app.Fragment; import android.content.Context; import android.os.Handler; import android.os.Message; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import com.bumptech.glide.Glide; import com.bwie.test.myyuekaoshi.R; import com.bwie.test.bean.ShouYeBean; import java.util.ArrayList; import java.util.List; public class MyCreateLayout extends FrameLayout { private ViewPager vp; private LinearLayout linear_layout; private ArrayList<ImageView> listc; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==0){ int vpCurrentItem = vp.getCurrentItem(); vp.setCurrentItem(vpCurrentItem+1); handler.sendEmptyMessageDelayed(0,2000); } } }; private OnClickLisner clickListner; public MyCreateLayout(@NonNull Context context) { super(context); init(); } public MyCreateLayout(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public MyCreateLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { View view = View.inflate(getContext(), R.layout.item_banner, this); vp = view.findViewById(R.id.vp); linear_layout = view.findViewById(R.id.linear_layout); } public void setImage(List<ShouYeBean.DataBean> list){ MyPagerAdapter adapter = new MyPagerAdapter(getContext(), list); vp.setAdapter(adapter); vp.setCurrentItem(list.size()*10000); handler.sendEmptyMessageDelayed(0,2000); listc = new ArrayList<>(); linear_layout.removeAllViews(); for (int i=0;i<list.size();i++){ ImageView docImage = new ImageView(getContext()); if (i==0){ docImage.setImageResource(R.drawable.shape_01); }else { docImage.setImageResource(R.drawable.shape_02); } listc.add(docImage); LinearLayout.LayoutParams params = new LinearLayout .LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.setMargins(5,0,5,0); linear_layout.addView(docImage,params); } vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for (int i=0;i<listc.size();i++){ if (position%listc.size()==i){ listc.get(i).setImageResource(R.drawable.shape_01); }else { listc.get(i).setImageResource(R.drawable.shape_02); } } } @Override public void onPageScrollStateChanged(int state) { } }); } public void setClickListner(OnClickLisner clickListner) { this.clickListner = clickListner; } private class MyPagerAdapter extends PagerAdapter { private final Context context; private final List<ShouYeBean.DataBean> list; public MyPagerAdapter(Context context, List<ShouYeBean.DataBean> list) { this.context = context; this.list = list; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, final int position) { ImageView imageView = new ImageView(context); imageView.setScaleType(ImageView.ScaleType.FIT_XY); Glide.with(context).load(list.get(position%list.size()).getIcon()).into(imageView); //点击事件 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { //触发 clickListner.onItemClick(position%list.size()); } }); imageView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_MOVE: handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_CANCEL: handler.sendEmptyMessageDelayed(0,2000); break; case MotionEvent.ACTION_UP: handler.sendEmptyMessageDelayed(0,2000); break; } return false; } }); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } public interface OnClickLisner{ void onItemClick(int position); } }
model文件夹下Model
import com.bwie.test.presenter.Presenter; import com.bwie.test.presenter.PresenterPort; import java.io.IOException; import io.reactivex.Observable; import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; import okhttp3.ResponseBody; public class Model { private PresenterPort presenterPort; private Disposable d; public Model(PresenterPort presenterPort) { this.presenterPort = presenterPort; } public void getUrl(Observable<ResponseBody> responseBodyObservable) { responseBodyObservable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<ResponseBody>() { @Override public void onSubscribe(Disposable d) { Model.this.d = d; } @Override public void onNext(ResponseBody value) { presenterPort.getResponseBody(value); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } public void getJieYue() { d.dispose(); } }
presenter文件夹下BasePresenter
import java.lang.ref.Reference; import java.lang.ref.WeakReference; public abstract class BasePresenter<T> { protected Reference<T> mViewRef;//View接口类型弱引用 public void attachView(T view) { mViewRef = new WeakReference<T>(view); //建立关联 } protected T getView() { return mViewRef.get();//获取View } public boolean isViewAttached() {//判断是否与View建立了关联 return mViewRef != null && mViewRef.get() != null; } public void detachView() {//解除关联 if (mViewRef != null) { mViewRef.clear(); mViewRef = null; } } }
Presenter
import com.bwie.test.myyuekaoshi.MainActivity; import com.bwie.test.model.Model; import com.bwie.test.view.MyView; import io.reactivex.Observable; import okhttp3.ResponseBody; public class Presenter extends BasePresenter implements PresenterPort{ private MyView myView; private final Model model; public Presenter(MyView myView) { model = new Model(this); this.myView = myView; } public void getUrl(Observable<ResponseBody> responseBodyObservable) { model.getUrl(responseBodyObservable); } @Override public void getResponseBody(ResponseBody responseBody) { myView.getResponseBody(responseBody); } @Override public void getError(Throwable throwable) { myView.getError(throwable); } public void getJieYue() { model.getJieYue(); } }
PresenterPort
import okhttp3.ResponseBody; public interface PresenterPort { void getResponseBody(ResponseBody responseBody); void getError(Throwable throwable); }
view文件夹下MyView
public interface MyView { void getResponseBody(ResponseBody responseBody); void getError(Throwable throwable); }
SetClicked
public interface SetClicked { void onClicked(int position); }
util文件夹下ApiService
public interface ApiService { @GET("{url}") Observable<ResponseBody> doGet(@Path(value = "url" , encoded = true) String url, @QueryMap Map<String, String> map); @FormUrlEncoded @POST("{url}") Observable<ResponseBody> doPost(@Path(value = "url" , encoded = true) String url, @FieldMap Map<String, String> map); }
ApiUrl
public class ApiUrl { //https://www.zhaoapi.cn/product/getCarts?uid=4427 //http://120.27.23.105/getCarts?uid=4427 public static String gongtong="https://www.zhaoapi.cn/"; //1. 首页接口 public static String shouye="ad/getAd"; //2. 查询购物车 public static String select="product/getCarts"; //3. 更新购物车?uid=71&sellerid=1&pid=1&selected=0&num=10 public static String gengxin="product/updateCarts"; //4. 删除购物车uid=72&pid=1 public static String delete="product/deleteCart"; }
RetrofitUtil
import android.util.Log; import java.io.IOException; import java.util.HashMap; import java.util.Map; import okhttp3.FormBody; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; public class RetrofitUtil { private static RetrofitUtil retrofitUtil=null; private Retrofit retrofit; private RetrofitUtil(){ } private RetrofitUtil(String baseurl){ OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(new CommonParamsInterceptor())//拦截器 .build(); retrofit = new Retrofit.Builder() .baseUrl(baseurl) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } public static RetrofitUtil getficeUrl(String baseurl){ if (retrofitUtil==null){ synchronized (RetrofitUtil.class){ if (retrofitUtil==null){ retrofitUtil=new RetrofitUtil(baseurl); } } } return retrofitUtil; } public <T> T createService(Class<T> service){ return retrofit.create(service); } public static ApiService getSerVice(){ return RetrofitUtil.getficeUrl(ApiUrl.gongtong).createService(ApiService.class); } private static class CommonParamsInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { //调用request()方法得到拦截的请求 Request request = chain.request(); //获取请求的方式 String method = request.method();//GET POST //拦截的请求的路径 String oldUrl = request.url().toString(); Log.e("----oldUrl",oldUrl); //要添加的公共参数...map Map<String,String> map = new HashMap<>(); map.put("source","android"); if ("GET".equals(method)){ StringBuilder stringBuilder = new StringBuilder();//创建一个stringBuilder...字符串缓冲区 stringBuilder.append(oldUrl); if (oldUrl.contains("?")){ //?在最后面....2类型 if (oldUrl.indexOf("?") == oldUrl.length()-1){ }else { //3类型...拼接上& stringBuilder.append("&"); } }else { //不包含? 属于1类型,,,先拼接上?号 stringBuilder.append("?"); } //添加公共参数....source=android&appVersion=100& for (Map.Entry<String,String> entry: map.entrySet()) { //拼接 stringBuilder.append(entry.getKey()) .append("=") .append(entry.getValue()) .append("&"); } //删掉最后一个&符号 if (stringBuilder.indexOf("&") != -1){ stringBuilder.deleteCharAt(stringBuilder.lastIndexOf("&")); } //得到含有公共参数的新路径.....使用新路径去请求 String newUrl = stringBuilder.toString(); Log.e("----newUrl",newUrl); //新的路径构建一个新的请求 request = request.newBuilder().url(newUrl).build(); }else if ("POST".equals(method)){ //参数在请求的实体内容上....拿到以前的参数,把以前的参数和公共参数全都添加到请求实体上 RequestBody requestBody = request.body(); if (requestBody instanceof FormBody){//前边是不是后边的子类/实例对象 FormBody oldBody = (FormBody) requestBody;//keywords...page FormBody.Builder builder = new FormBody.Builder(); //先添加之前的参数 for (int i = 0;i<oldBody.size();i++){ //键值对的形式添加 builder.add(oldBody.name(i),oldBody.value(i)); } //添加公共参数 for (Map.Entry<String,String> entry: map.entrySet()) { builder.add(entry.getKey(),entry.getValue()); } //构建一个新的请求 request = request.newBuilder().url(oldUrl).post(builder.build()).build(); } } //执行请求 获取到响应 Response response = chain.proceed(request); return response; } } }
需要使用
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:name="com.bwie.test.zidingyi.MyApp"> </application>
需要导入的依赖
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' //retrofit2 compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1' //rxjava2 compile 'io.reactivex.rxjava2:rxjava:2.0.1' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' implementation 'com.android.support:recyclerview-v7:26.1.0' //tupian compile 'com.facebook.fresco:fresco:0.14.1' compile 'com.facebook.fresco:animated-base-support:0.14.1' compile 'com.facebook.fresco:animated-gif:0.14.1' compile 'com.facebook.fresco:webpsupport:0.14.1' compile 'com.facebook.fresco:animated-webp:0.14.1' compile 'com.facebook.fresco:imagepipeline-okhttp3:0.14.1' //id implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.github.bumptech.glide:glide:3.7.0'
bean文件夹下MyBean
public class MyBean { private String msg; private String code; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { private String sellerName; private String sellerid; private List<ListBean> list; private boolean checkbox; public boolean isCheckbox() { return checkbox; } public void setCheckbox(boolean checkbox) { this.checkbox = checkbox; } public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } public String getSellerid() { return sellerid; } public void setSellerid(String sellerid) { this.sellerid = sellerid; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } public static class ListBean { private double bargainPrice; private String createtime; private String detailUrl; private String images; private int num; private int pid; private double price; private int pscid; private int selected; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSelected() { return selected; } public void setSelected(int selected) { this.selected = selected; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } } }
MyHeJiBean
public class MyHeJiBean { private String price; private int num; public MyHeJiBean(String price, int num) { this.price = price; this.num = num; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } }
ShouYeBean
public class ShouYeBean { private String msg; private String code; private TuijianBean tuijian; private MiaoshaBean miaosha; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public TuijianBean getTuijian() { return tuijian; } public void setTuijian(TuijianBean tuijian) { this.tuijian = tuijian; } public MiaoshaBean getMiaosha() { return miaosha; } public void setMiaosha(MiaoshaBean miaosha) { this.miaosha = miaosha; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class TuijianBean { private String name; private List<ListBean> list; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } public static class ListBean { private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } } public static class MiaoshaBean { private String name; private int time; private List<ListBeanX> list; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } public List<ListBeanX> getList() { return list; } public void setList(List<ListBeanX> list) { this.list = list; } public static class ListBeanX { private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } } public static class DataBean { private int aid; private String createtime; private String icon; private Object productId; private String title; private int type; private String url; public int getAid() { return aid; } public void setAid(int aid) { this.aid = aid; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public Object getProductId() { return productId; } public void setProductId(Object productId) { this.productId = productId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } } }