base包
BaseContract
public interface BaseContract { interface BasePresenter<T extends BaseView>{ void attchView(T view); //绑定 void detachView();//解绑 } interface BaseView{ void showLoading(); void dismissLoading(); } }
BasePresenter
public class BasePresenter<T extends BaseContract.BaseView> implements BaseContract.BasePresenter<T>{ protected T mView; @Override public void attchView(T view) { this.mView=view; } @Override public void detachView() { if (mView!=mView){ mView=null; } } } BaseActivity
public abstract class BaseActivity<T extends BaseContract.BasePresenter> extends AppCompatActivity implements IBase,BaseContract.BaseView{ @Inject protected T mPresenter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(getContentLayout()); inject(); //绑定 mPresenter.attchView(this); } @Override protected void onDestroy() { super.onDestroy(); //解绑 mPresenter.detachView(); } @Override public void showLoading() { } @Override public void dismissLoading() { } @Override public void initView(View view) { } }BaseFragment
public abstract class BaseFragment<T extends BaseContract.BasePresenter> extends Fragment implements IBase, BaseContract.BaseView { @Inject protected T mPresenter; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); inject(); mPresenter.attchView(this); } @Override public void onDestroy() { super.onDestroy(); mPresenter.detachView(); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(getContentLayout(), null); initView(view); return view; } @Override public void showLoading() { } @Override public void dismissLoading() { } }component包
HttpComponent 桥梁
@Component(modules = HttpModule.class) public interface HttpComponent { //登陆 密码 void inject(LoginActivity loginActivity); //注册 void inject(RegisterActivity registerActivity); //首页 void inject(Homepage homepage); //分类 void inject(Fragment_02 fragment_02); //详情列表 void inject(ListActivity listActivity); //轮播图 void inject(ListDetailsActivity listDetailsActivity); //购物车 void inject(ShopCartActivity shopCartActivity); //void inject(MakeSureOrderActivity makeSureOrderActivity); } module包 HttpModule 容器
@Module public class HttpModule { @Provides //不需要拦截器 LoginApi provideLoginApi(){ OkHttpClient okHttpClient = new OkHttpClient.Builder() .writeTimeout(20, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) .connectTimeout(10, TimeUnit.SECONDS) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Api.LOGIN_URL) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .client(okHttpClient)//用自己的设置读取和链接超时时间 .build(); LoginApiService loginApiService = retrofit.create(LoginApiService.class); return LoginApi.getLoginApi(loginApiService); } @Provides //需要拦截器 AddInterceptor_ addInterceptor_(){ OkHttpClient okHttpClient = new OkHttpClient.Builder() .writeTimeout(20, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) .connectTimeout(10, TimeUnit.SECONDS) .addInterceptor(new MyInterceptor())//拦截器 .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Api.LOGIN_URL) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .client(okHttpClient)//用自己的设置读取和链接超时时间 .build(); AddInterceptor_Service addInterceptor_service = retrofit.create(AddInterceptor_Service.class); return AddInterceptor_.getAddInterceptor_(addInterceptor_service); } }net包
拦截器
//工具类拦截器 public class MyInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { //创建一个FormBody.Builder对象,用于添加公共参数 FormBody.Builder builder = new FormBody.Builder(); //先获取原始的请求 Request originalRequest = chain.request(); //获取原始请求里的请求体数据 FormBody formBody = (FormBody) originalRequest.body(); for (int i = 0; i < formBody.size(); i++) { //先把原始的请求体的参数添加到builder里 builder.add(formBody.name(i),formBody.value(i)); } //添加公共参数 builder.add("source","android"); FormBody body = builder.build(); //创建一个新的Request Request request = new Request.Builder() .url(originalRequest.url()) .post(body) .build(); Response response = chain.proceed(request); return response; } } net包 LoginApi
public class LoginApi { private static LoginApi loginApi; private LoginApiService loginApiService; private LoginApi(LoginApiService loginApiService){ this.loginApiService=loginApiService; } public static LoginApi getLoginApi(LoginApiService loginApiService){ if (loginApi==null){ loginApi=new LoginApi(loginApiService); } return loginApi; } //登陆 public Observable<UserBean> login(String mobile, String passwod){ return loginApiService.login(mobile,passwod); } //注册 public Observable<UserRegisterBean> loginRegister(String mobile, String passwod){ return loginApiService.loginRegister(mobile,passwod); } //轮播图 public Observable<AdBean>getAd(){ return loginApiService.getAd(); } //分类 public Observable<CatagoryBean> getCatagory() { return loginApiService.getCatagory(); } //获取子分类 public Observable<ProductCatagoryBean> getProductCatagory(String cid) { return loginApiService.getProductCatagory(cid); } //购物详情列表 public Observable<ProductsBean> getProduct(String pscid) { return loginApiService.getProduct(pscid); } //轮播图页面 加入购物车 public Observable<BaseBean> getCatagory(String uid, String pid, String token) { return loginApiService.addCart(uid, pid, token); } //购物车查询 public Observable<GetCartsBean> getCatagory(String uid, String token) { return loginApiService.getCart(uid, token); } //购物车更新 public Observable<BaseBean> updateCarts(String uid, String sellerid, String pid, String num, String selected, String token) { return loginApiService.updateCarts(uid, sellerid, pid, num, selected, token); } //删除购物车 public Observable<BaseBean> deleteCart(String uid, String pid, String token) { return loginApiService.deleteCart(uid, pid, token); } // public Observable<AddrsBean> getAddrs(String uid, String token) { return loginApiService.getAddrs(uid, token); } // public Observable<BaseBean> getCatagory1(String uid, String price, String token) { return loginApiService.createOrder(uid, price, token); } }
net包
LoginApiService
public interface LoginApiService {
//登陆 @FormUrlEncoded @POST("user/login")//post请求 Observable<UserBean> login(@Field("mobile") String mobile, @Field("password") String password ); //注册 @FormUrlEncoded //有参数用 @POST("user/reg") //post请求 Observable<UserRegisterBean> loginRegister(@Field("mobile") String mobile, @Field("password") String password ); //轮播图 @GET("ad/getAd") Observable<AdBean> getAd(); //分类 左边 @GET("product/getCatagory") Observable<CatagoryBean> getCatagory(); //右边 @FormUrlEncoded @POST("product/getProductCatagory") Observable<ProductCatagoryBean> getProductCatagory(@Field("cid") String cid); //列表 @FormUrlEncoded @POST("product/getProducts") Observable<ProductsBean> getProduct(@Field("pscid") String pscid); //轮播图 @FormUrlEncoded @POST("product/addCart") Observable<BaseBean> addCart(@Field("Uid") String uid, @Field("Pid") String pid, @Field("Token") String token); //购物车1 @FormUrlEncoded @POST("product/getCarts") Observable<GetCartsBean> getCart(@Field("Uid") String uid, @Field("Token") String token); //购物车更新 @FormUrlEncoded @POST("product/updateCarts") Observable<BaseBean> updateCarts(@Field("uid") String uid, @Field("sellerid") String sellerid, @Field("pid") String pid, @Field("num") String num, @Field("selected") String selected, @Field("token") String token); //删除购物车 @FormUrlEncoded @POST("product/deleteCart") Observable<BaseBean> deleteCart(@Field("uid") String uid, @Field("pid") String pid, @Field("token") String token); // @FormUrlEncoded @POST("user/getAddrs") Observable<AddrsBean> getAddrs(@Field("uid") String uid, @Field("token") String token); // @FormUrlEncoded @POST("product/createOrder") Observable<BaseBean> createOrder(@Field("Uid") String uid, @Field("Price") String price, @Field("Token") String token); }ui包
登陆:
login包
contract包
LoginContract
public interface LoginContract { //泛型 实现 P层和 View层关联 interface Presenter extends BaseContract.BasePresenter<View>{ //登陆方法 需要两个参数 void login(String mobile,String password); } interface View extends BaseContract.BaseView{ void loginSuccess(UserBean userBean); //登陆bean包 } } ui包 登陆: login包 presenter包 LoginPresenter
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter{ private LoginApi loginApi; @Inject public LoginPresenter(LoginApi loginApi){ this.loginApi=loginApi; } @Override public void login(String mobile, String password) { loginApi.login(mobile,password) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Observer<UserBean>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(UserBean userBean) { mView.loginSuccess(userBean); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } } ui包 登陆:v层 login包 LoginActivity
public class LoginActivity extends BaseActivity<LoginPresenter> implements View.OnClickListener, LoginContract.View { private EditText mName; private EditText mPass; private Button mBtn; /** * 快速注册 */ private TextView mText1; /** * 忘记密码 */ private TextView mText2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); } @Override//实现接口布局 public int getContentLayout() { return R.layout.activity_login; } @Override //注入 public void inject() { DaggerHttpComponent.builder() .httpModule(new HttpModule()) .build() .inject(this); } //显示页面 private void initView() { mName = (EditText) findViewById(R.id.name); mPass = (EditText) findViewById(R.id.pass); mBtn = (Button) findViewById(R.id.btn); mBtn.setOnClickListener(this); mText1 = (TextView) findViewById(R.id.text1); mText1.setOnClickListener(this); mText2 = (TextView) findViewById(R.id.text2); } @Override//点击事件 public void onClick(View v) { switch (v.getId()) { default: break; case R.id.btn: String mobile = mName.getText().toString(); String password = mPass.getText().toString(); mPresenter.login(mobile, password); break; case R.id.text1: Intent intent=new Intent(LoginActivity.this, RegisterActivity.class); startActivityForResult(intent,1); break; } } @Override//成功的回调方法 public void loginSuccess(UserBean userBean) { Toast.makeText(LoginActivity.this, userBean.getMsg(), Toast.LENGTH_SHORT).show(); //存值 到 ShopCartActivity 获取 SharedPreferencesUtils.setParam(LoginActivity.this,"uid",userBean.getData().getUid() + ""); SharedPreferencesUtils.setParam(LoginActivity.this,"name",userBean.getData().getUsername() + ""); SharedPreferencesUtils.setParam(LoginActivity.this,"iconUrl",userBean.getData().getIcon() + ""); SharedPreferencesUtils.setParam(LoginActivity.this,"token",userBean.getData().getToken() + ""); LoginActivity.this.finish(); } @Override//销毁 protected void onDestroy () { super.onDestroy(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode==1&&resultCode==1){ String mobule1 = data.getStringExtra("mobule"); String password1 = data.getStringExtra("password"); mName.setText(mobule1); mPass.setText(password1); } } } ui包 登陆:v层 LoginActivity布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="40dp" tools:context="com.example.com.a1512mvp_.ui.login.LoginActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号" android:layout_marginTop="30dp" android:id="@+id/name"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:layout_marginTop="30dp" android:id="@+id/pass"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="登陆" android:id="@+id/btn"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="快速注册" android:id="@+id/text1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="忘记密码" android:id="@+id/text2"/> </RelativeLayout> </LinearLayout> </LinearLayout>
ui包
注册:
login_register包
contract包
LoginRegister
public interface LoginRegister { interface View extends BaseContract.BaseView{ void loginSuccess(UserRegisterBean UserRegisterBean); //登陆bean包 } //泛型 实现 P层和 View层关联 interface Presenter extends BaseContract.BasePresenter<View>{ //登陆方法 需要两个参数 void loginRegister(String mobile, String password); } }ui包
注册:
login_register包
Presenter包
public class LoginRegisterPresenter extends BasePresenter<LoginRegister.View> implements LoginRegister.Presenter { private LoginApi loginApi; @Inject public LoginRegisterPresenter(LoginApi loginApi) { this.loginApi = loginApi; } @Override public void loginRegister(String mobile, String password) { loginApi.loginRegister(mobile,password) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Observer<UserRegisterBean>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(UserRegisterBean userRegisterBean) { mView.loginSuccess(userRegisterBean); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } }ui包
注册:v层
login_register包
public class RegisterActivity extends BaseActivity<LoginRegisterPresenter> implements LoginRegister.View, View.OnClickListener { /** * 请输入账号 */ private EditText mName; /** * 请输入密码 */ private EditText mPass; /** * 注册 */ private Button mBtn; private String mobile; private String password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); } @Override public int getContentLayout() { return R.layout.activity_register; } @Override public void inject() { DaggerHttpComponent.builder() .httpModule(new HttpModule()) .build() .inject(this); } private void initView() { mName = (EditText) findViewById(R.id.name); mPass = (EditText) findViewById(R.id.pass); mBtn = (Button) findViewById(R.id.btn); mBtn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.btn: mobile = mName.getText().toString(); password = mPass.getText().toString(); mPresenter.loginRegister(mobile, password); break; } } //成功回调的方法 public void loginSuccess(UserRegisterBean userRegisterBean) { Toast.makeText(RegisterActivity.this, userRegisterBean.getMsg(), Toast.LENGTH_SHORT).show(); if(userRegisterBean.getMsg().equals("注册成功")){ Intent intent = getIntent(); intent.putExtra("mobule", mobile); intent.putExtra("password",password); setResult(1, intent); //关闭页面 finish(); /* //发送消息 可以传递任何类型 EventBus.getDefault().postSticky("mobile"); EventBus.getDefault().postSticky("password"); //跳转 startActivity(new Intent(RegisterActivity.this,LoginActivity.class));*/ } } } ui包 login_register包 注册:v层 RegisterActivity布局
<?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" android:padding="40dp" tools:context="com.example.com.a1512mvp_.ui.login_register.RegisterActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号" android:layout_marginTop="30dp" android:id="@+id/name"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:layout_marginTop="30dp" android:id="@+id/pass"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="注册" android:id="@+id/btn"/> </LinearLayout>
首页欢迎界面
列表展示:
homepage包
contract包
HompageContract
public interface HomepageContract { interface View extends BaseContract.BaseView{ void getAdSuccess(AdBean adBean);//轮播图bean包 void getCatagorySuccess(CatagoryBean catagoryBean);//分类包 } //泛型 实现 P层和 View层关联 interface Presenter extends BaseContract.BasePresenter<View>{ //轮播图方法 void getAd(); //分类方法 void getCatagory(); } } 首页欢迎界面: 列表展示: Hompage包 Presenter包 HompagePresenter
public class HomepagePresenter extends BasePresenter<HomepageContract.View> implements HomepageContract.Presenter { private LoginApi loginApi; @Inject public HomepagePresenter(LoginApi loginApi) { this.loginApi = loginApi; } //轮播图方法 实现接口方法 @Override public void getAd() { loginApi.getAd() .observeOn(AndroidSchedulers.mainThread())//接收时的线程 .subscribeOn(Schedulers.io())//发送时的线程 .subscribe(new Observer<AdBean>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(AdBean adBean) { mView.getAdSuccess(adBean); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } //分类方法 实现接口方法 @Override public void getCatagory() { loginApi.getCatagory() .observeOn(AndroidSchedulers.mainThread())// 接收时的线程 .subscribeOn(Schedulers.io()) // 发送时的线程 .subscribe(new Observer<CatagoryBean>() { // 观察者 @Override public void onSubscribe(Disposable d) { } @Override public void onNext(CatagoryBean catagoryBean) { mView.getCatagorySuccess(catagoryBean); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } }首页欢迎界面
Hompage包
列表展示 V 层
public class Homepage extends BaseFragment<HomepagePresenter> implements HomepageContract.View { private View view; private ImageView mIvZxing; private Banner mBanner; private RecyclerView mRvClass; /** * 京东快报 */ private TextView mTvJD; private LinearLayout mLlMore; private MarqueeView mMarqueeView; private RecyclerView mRvSecKill; private RecyclerView mRvRecommend; private LinearLayout mLine1; //获取布局 @Override public int getContentLayout() { return R.layout.fragment_01; } //注入重要 报空指针P @Override public void inject() { DaggerHttpComponent.builder() .httpModule(new HttpModule()) .build() .inject(this); } @Override public void initView(View view) { mIvZxing = (ImageView) view.findViewById(R.id.ivZxing); mBanner = (Banner) view.findViewById(R.id.banner); //轮播图 mBanner.setImageLoader(new GlideImageLoader()); //分类的 //设置布局管理器 mRvClass = (RecyclerView) view.findViewById(R.id.rvClass); GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2, RecyclerView.HORIZONTAL, false); mRvClass.setLayoutManager(gridLayoutManager); //秒杀 //设置布局管理器 mRvSecKill = (RecyclerView) view.findViewById(R.id.rvSecKill); GridLayoutManager gridLayoutManager1 = new GridLayoutManager(getContext(), 1, RecyclerView.HORIZONTAL, false); mRvSecKill.setLayoutManager(gridLayoutManager1); //推荐 //设置布局管理器 mRvRecommend = (RecyclerView) view.findViewById(R.id.rvRecommend); GridLayoutManager gridLayoutManager2 = new GridLayoutManager(getContext(), 2, RecyclerView.VERTICAL, false); mRvRecommend.setLayoutManager(gridLayoutManager2); mTvJD = (TextView) view.findViewById(R.id.tvJD); mLlMore = (LinearLayout) view.findViewById(R.id.llMore); mMarqueeView = (MarqueeView) view.findViewById(R.id.marqueeView); List<String> info = new ArrayList<>(); info.add("1. 天行健,君子以自强不息"); info.add("2. 地势坤,君子以厚德载物"); info.add("3. 为天地立心"); info.add("4. 为生民立命"); info.add("5. 为往圣继绝学"); info.add("6. 为万世开太平"); mMarqueeView.startWithList(info); //输入框跳转页面 mLine1=view.findViewById(R.id.line110); mLine1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getContext(), SouSuoActivity.class); startActivity(intent); } }); //请求数据 initData(); } //请求数据的方法 private void initData() { //图片+推荐+秒杀 mPresenter.getAd(); //分类 mPresenter.getCatagory(); } //成功的方法《图片+推荐+秒杀》 @Override public void getAdSuccess(final AdBean adBean) { //轮播图// List<AdBean.DataBean> data = adBean.getData(); List<String> images = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { images.add(data.get(i).getIcon()); } //设置图片集合 mBanner.setImages(images); mBanner.start(); //banner设置方法全部调用完毕时最后调用 // 《 秒杀解析 》 /// RvSecKillAdapter rvSecKillAdapter = new RvSecKillAdapter(getActivity(), adBean.getMiaosha().getList()); mRvSecKill.setAdapter(rvSecKillAdapter); rvSecKillAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(int position) { //跳转显示详情 //集合调取数据 获取详细地址 进行跳转 String detailUrl = adBean.getMiaosha().getList().get(position).getDetailUrl(); //进行跳转展示数据——页面 Intent intent = new Intent(getContext(), WebViewMiaoShaActivity.class); intent.putExtra("detailUrl", detailUrl); startActivity(intent); } @Override public void onLongItemClick(int position) { } }); // 《 推荐解析 》 /// RvRecommendAdapter rvRecommendAdapter = new RvRecommendAdapter(getActivity(), adBean.getTuijian().getList()); mRvRecommend.setAdapter(rvRecommendAdapter); } //成功的方法《分类》 @Override public void getCatagorySuccess(CatagoryBean catagoryBean) { List<CatagoryBean.DataBean> data = catagoryBean.getData(); RvClassAdapter adapter = new RvClassAdapter(getActivity(), data); mRvClass.setAdapter(adapter); } @Override//图片销毁 public void onDestroy() { super.onDestroy(); mBanner.stopAutoPlay();//结束轮播 } } 首页欢迎界面 列表展示 V 层 Hompage布局:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:background="#77f"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#ff3660" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/ivZxing" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="15dp" android:background="@drawable/a_s"/> <LinearLayout android:layout_width="match_parent" android:layout_height="30dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:orientation="horizontal" android:id="@+id/line110" android:layout_weight="1" android:background="@drawable/shape_search"></LinearLayout> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:background="@drawable/my_msg_bai"/> </LinearLayout> <com.youth.banner.Banner android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="200dp"/> <android.support.v7.widget.RecyclerView android:id="@+id/rvClass" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> <RelativeLayout android:layout_width="match_parent" android:layout_height="30dp" android:layout_margin="15dp" android:background="@drawable/shape_search" > <TextView android:id="@+id/tvJD" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:text="京东快报"/> <LinearLayout android:id="@+id/llMore" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:orientation="horizontal"> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#000000"></View> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="更多"/> </LinearLayout> <com.sunfusheng.marqueeview.MarqueeView android:id="@+id/marqueeView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:layout_toLeftOf="@id/llMore" android:layout_toRightOf="@id/tvJD" app:mvAnimDuration="1000" app:mvDirection="bottom_to_top" app:mvInterval="3000" app:mvSingleLine="true" app:mvTextColor="#000000" app:mvTextSize="14sp"/> </RelativeLayout> <android.support.v7.widget.RecyclerView android:id="@+id/rvSecKill" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#33000000" android:gravity="center" android:text="为您推荐"/> <android.support.v7.widget.RecyclerView android:id="@+id/rvRecommend" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout> </ScrollView> Hompage包点击秒杀跳转WebView
public class WebViewMiaoShaActivity extends AppCompatActivity { //点击 秒杀 展示页面 private WebView mWv; private String detailUrl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view_miao_sha); //接收地址 拿到参数 Intent intent = getIntent(); detailUrl = intent.getStringExtra("detailUrl"); initView(); mWv.loadUrl(detailUrl); initView(); } private void initView() { mWv = (WebView) findViewById(R.id.wv); WebSettings settings = mWv.getSettings(); //支持js settings.setJavaScriptEnabled(true); } }
Hompage包
adapter包
分类适配器:
RvClassAdapter
public class RvClassAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<CatagoryBean.DataBean> list; private LayoutInflater inflater; private OnItemClickListener onItemClickListener; public RvClassAdapter(Context context, List<CatagoryBean.DataBean> list) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.rvclass_item, parent, false); ClassViewHoler classViewHoler = new ClassViewHoler(view); return classViewHoler; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) { //显示数据 ClassViewHoler classViewHoler = (ClassViewHoler) holder; CatagoryBean.DataBean dataBean = list.get(position); classViewHoler.iv.setImageURI(dataBean.getIcon()); classViewHoler.tv.setText(dataBean.getName()); //给条目设置点击事件 classViewHoler.ll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(position); } } }); } @Override public int getItemCount() { return list.size(); } class ClassViewHoler extends RecyclerView.ViewHolder { private final SimpleDraweeView iv; private final TextView tv; private final LinearLayout ll; public ClassViewHoler(View itemView) { super(itemView); iv = itemView.findViewById(R.id.iv); tv = itemView.findViewById(R.id.tv); ll = itemView.findViewById(R.id.ll); } } }分类适配器 布局
RvClassAdapter
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:gravity="center" android:orientation="vertical"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/iv" android:layout_width="50dp" android:layout_height="50dp"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Hompage包
adapter包
推荐适配器:
RvRecommendAdapter
pub ic classRvRecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
//推荐适配器 private Context context; private List<AdBean.TuijianBean.ListBean> list; private LayoutInflater inflater; private OnItemClickListener onItemClickListener; public RvRecommendAdapter(Context context, List<AdBean.TuijianBean.ListBean> list) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.rvrecommend_item, parent, false); RecommendViewHolder recommendViewHolder = new RecommendViewHolder(view); return recommendViewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { RecommendViewHolder recommendViewHolder = (RecommendViewHolder) holder; AdBean.TuijianBean.ListBean listBean = list.get(position); String images = listBean.getImages(); String[] split = images.split("\\|"); String url = split.length == 0 ? images : split[0]; recommendViewHolder.iv.setImageURI(url); recommendViewHolder.tv.setText(listBean.getTitle()); } @Override public int getItemCount() { return list.size(); } class RecommendViewHolder extends RecyclerView.ViewHolder { private final SimpleDraweeView iv; private final TextView tv; public RecommendViewHolder(View itemView) { super(itemView); iv = itemView.findViewById(R.id.iv); tv = itemView.findViewById(R.id.tv); } } }推荐适配器布局:
RvRecommendAdapter
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:padding="10dp"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/iv" android:layout_width="200dp" android:layout_height="200dp"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Hompage包
adapter包
秒杀适配器:
RvSeckiIIAdapter
public class RvSecKillAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<AdBean.MiaoshaBean.ListBeanX> list; private LayoutInflater inflater; private OnItemClickListener onItemClickListener; public RvSecKillAdapter(Context context, List<AdBean.MiaoshaBean.ListBeanX> list) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); } public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener = onItemClickListener; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.rvseckill_item, parent, false); SecKillViewHolder secKillViewHolder = new SecKillViewHolder(view); return secKillViewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) { SecKillViewHolder secKillViewHolder = (SecKillViewHolder) holder; AdBean.MiaoshaBean.ListBeanX listBeanX = list.get(position); String url = listBeanX.getImages().split("\\|")[0]; secKillViewHolder.iv.setImageURI(url); secKillViewHolder.iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener!=null){ onItemClickListener.onItemClick(position); } } }); } @Override public int getItemCount() { return list.size(); } class SecKillViewHolder extends RecyclerView.ViewHolder{ private final SimpleDraweeView iv; public SecKillViewHolder(View itemView) { super(itemView); iv = itemView.findViewById(R.id.iv); } } } 秒杀适配器布局: RvSeckiIIAdapter
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:padding="10dp" android:layout_height="wrap_content"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/iv" android:layout_width="90dp" android:layout_height="90dp"/> </LinearLayout>
第二个页面:
fragment02
分类
左右v层
public class Fragment_02 extends BaseFragment<ClassifyPresenter> implements ClassifyContract.View { private View view; private ImageView mIvZxing; private RecyclerView mRvLeft; private ImageView mIv; private ExpandableListView mElv; @Override public int getContentLayout() { return R.layout.fragment_02; } @Override public void inject() { DaggerHttpComponent.builder() .httpModule(new HttpModule()) .build() .inject(this); } @Override public 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); //关连 mPresenter.getCatagory(); //设置布局管理器 mRvLeft.setLayoutManager(new LinearLayoutManager(getContext())); mRvLeft.addItemDecoration(new DividerItemDecoration(getContext(), RecyclerView.VERTICAL)); } @Override//左边//左边//左边 public void getCatagorySuccess(final CatagoryBean catagoryBean) { List<CatagoryBean.DataBean> data = catagoryBean.getData(); //创建适配器 final RvLeftAdapter adapter = new RvLeftAdapter(getContext(), data); //显示左侧列表数据 mRvLeft.setAdapter(adapter); int cid = data.get(0).getCid(); mPresenter.getProductCatagory(cid + ""); //设置第一个为默认选中 adapter.changeCheck(0, true); //设置点击事件 adapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(int position) { //改变DataBean里面check的属性值 adapter.changeCheck(position, true); //请求右侧对应的数据 mPresenter.getProductCatagory(catagoryBean.getData().get(position).getCid() + ""); } @Override public void onLongItemClick(int position) { } }); } @Override//右边//右边//右边 public void getProductCatagorySuccess(ProductCatagoryBean productCatagoryBean) { //定义一个集合用于存放title List<String> groupList = new ArrayList<>(); //定义一个集合用于存放title对应的内容 List<List<ProductCatagoryBean.DataBean.ListBean>> childList = new ArrayList<>(); List<ProductCatagoryBean.DataBean> data = productCatagoryBean.getData(); for (int i = 0; i < data.size(); i++) { groupList.add(data.get(i).getName()); List<ProductCatagoryBean.DataBean.ListBean> list = data.get(i).getList(); childList.add(list); } //使用ExpandableListView展示数据 ElvAdapter elvAdapter = new ElvAdapter(getContext(), groupList, childList); mElv.setAdapter(elvAdapter); //默认展开列表 for (int i = 0; i < groupList.size(); i++) { mElv.expandGroup(i); } productCatagoryBean.getMsg(); } }
fragment02 布局
v层
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#7ef"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#ff3660" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/ivZxing" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="15dp" android:background="@drawable/a_s"/> <LinearLayout android:layout_width="match_parent" android:layout_height="30dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_weight="1" android:background="@drawable/shape_search"></LinearLayout> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:background="@drawable/my_msg_bai"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <android.support.v7.widget.RecyclerView android:id="@+id/rvLeft" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3"></android.support.v7.widget.RecyclerView> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="7" android:orientation="vertical"> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="150dp"/> <ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="match_parent" ></ExpandableListView> </LinearLayout> </LinearLayout> </LinearLayout>
fragment02
分类左边列表适配器
父列表
public class RvLeftAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<CatagoryBean.DataBean> list; private LayoutInflater inflater; private OnItemClickListener onItemClickListener; public RvLeftAdapter(Context context, List<CatagoryBean.DataBean> list) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.rvleft_item, parent, false); LeftViewHolder leftViewHolder = new LeftViewHolder(view); return leftViewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) { CatagoryBean.DataBean dataBean = list.get(position); LeftViewHolder leftViewHolder = (LeftViewHolder) holder; leftViewHolder.tv.setText(dataBean.getName()); //设置字体颜色 if (dataBean.isChecked()) { leftViewHolder.tv.setTextColor(Color.RED); leftViewHolder.tv.setBackgroundColor(Color.GRAY); } else { leftViewHolder.tv.setTextColor(Color.BLACK); leftViewHolder.tv.setBackgroundColor(Color.WHITE); } leftViewHolder.tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(position); } } }); } @Override public int getItemCount() { return list.size(); } class LeftViewHolder extends RecyclerView.ViewHolder { private final TextView tv; public LeftViewHolder(View itemView) { super(itemView); tv = itemView.findViewById(R.id.tv); } } /** * 选中后,改变字体颜色和背景色 * * @param position * @param bool */ public void changeCheck(int position, boolean bool) { //先还原一下 for (int i = 0;i<list.size();i++){ list.get(i).setChecked(false); } CatagoryBean.DataBean dataBean = list.get(position); dataBean.setChecked(bool); //刷新界面 notifyDataSetChanged(); } }
分类左边列表适配器
父列表布局
图片下面的
二级列表
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center"/> </LinearLayout> fragment02 分类右边 分类右边列表适配器 父列表布局
public class ElvAdapter extends BaseExpandableListAdapter { private Context context; private List<String> groupList; private List<List<ProductCatagoryBean.DataBean.ListBean>> childList; private LayoutInflater inflater; public ElvAdapter(Context context, List<String> groupList, List<List<ProductCatagoryBean.DataBean.ListBean>> childList) { this.context = context; this.groupList = groupList; this.childList = childList; inflater = LayoutInflater.from(context); } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return 1;//因为二级列表是一个RecylerView,所以返回1即可 } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder groupViewHolder; if (convertView == null) { groupViewHolder = new GroupViewHolder(); convertView = inflater.inflate(R.layout.rvleft_item, null); groupViewHolder.tv = convertView.findViewById(R.id.tv); convertView.setTag(groupViewHolder); } else { groupViewHolder = (GroupViewHolder) convertView.getTag(); } //显示数据 groupViewHolder.tv.setText(groupList.get(groupPosition)); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder childViewHolder; if (convertView == null) { childViewHolder = new ChildViewHolder(); convertView = inflater.inflate(R.layout.elv_rv, null); childViewHolder.rv = convertView.findViewById(R.id.rv); convertView.setTag(childViewHolder); } else { childViewHolder = (ChildViewHolder) convertView.getTag(); } //显示数据 final List<ProductCatagoryBean.DataBean.ListBean> listBeans = childList.get(groupPosition); //设置布局管理器 childViewHolder.rv.setLayoutManager(new GridLayoutManager(context, 3)); //设置适配器 ElvRvAdapter elvRvAdapter = new ElvRvAdapter(context, listBeans); childViewHolder.rv.setAdapter(elvRvAdapter); elvRvAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(int position) { //点击跳转到列表页面 //点击跳转到列表页面 //点击跳转到列表页面 Intent intent = new Intent(context, ListActivity.class); int pscid = listBeans.get(position).getPscid(); intent.putExtra("pscid", pscid); context.startActivity(intent); } @Override public void onLongItemClick(int position) { } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } class GroupViewHolder { TextView tv; } class ChildViewHolder { RecyclerView rv; } }列表
右边适配器
getGroupView布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center"/> </LinearLayout>列表
右边适配器
getChildView布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </LinearLayout>右边子类适配器
getChildView下面的适配器
public class ElvRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<ProductCatagoryBean.DataBean.ListBean> listBeans; private LayoutInflater inflater; private OnItemClickListener onItemClickListener; public ElvRvAdapter(Context context, List<ProductCatagoryBean.DataBean.ListBean> listBeans) { this.context = context; this.listBeans = listBeans; inflater = LayoutInflater.from(context); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.rvclass_item, parent, false); ElvRvAdapterViewHolder elvRvAdapterViewHolder = new ElvRvAdapterViewHolder(view); return elvRvAdapterViewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) { ElvRvAdapterViewHolder elvRvAdapterViewHolder = (ElvRvAdapterViewHolder) holder; ProductCatagoryBean.DataBean.ListBean listBean = listBeans.get(position); elvRvAdapterViewHolder.iv.setImageURI(listBean.getIcon()); elvRvAdapterViewHolder.tv.setText(listBean.getName()); //给条目设置点击事件 elvRvAdapterViewHolder.ll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(position); } } }); } @Override public int getItemCount() { return listBeans.size(); } class ElvRvAdapterViewHolder extends RecyclerView.ViewHolder { private final SimpleDraweeView iv; private final TextView tv; private final LinearLayout ll; public ElvRvAdapterViewHolder(View itemView) { super(itemView); ll = itemView.findViewById(R.id.ll); iv = itemView.findViewById(R.id.iv); tv = itemView.findViewById(R.id.tv); } } }右边子的布局
getChildView适配器
下面的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:gravity="center" android:orientation="vertical"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/iv" android:layout_width="50dp" android:layout_height="50dp"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
二级列表子点击跳转
ListActivity页面
列表展示
public class ListActivity extends BaseActivity<ListPresenter> implements ListContract.View { private ImageView mIvZxing; private XRecyclerView mXrv; private int pscid; public static final int LISTACTIVITY = 1; private boolean isRefresh = true; private XrvListAdapter adapter; @Override//接收传值 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); pscid = intent.getIntExtra("pscid", 0); //预显页面 initView(); //调用p层方法 mPresenter.getProducts(pscid + ""); } @Override//初始化页面 public int getContentLayout() { return R.layout.activity_list; } //设置加载更多和布局 private void initView() { mIvZxing = (ImageView) findViewById(R.id.ivZxing); //设置布局管理器 mXrv = (XRecyclerView) findViewById(R.id.xrv); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); mXrv.setLayoutManager(linearLayoutManager); //设置刷新和加载更多监听 mXrv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { //刷新 isRefresh = true; mPresenter.getProducts(pscid + ""); } @Override public void onLoadMore() { //加载更多 isRefresh = false; mPresenter.getProducts(pscid + ""); } }); } //注入 @Override public void inject() { DaggerHttpComponent.builder() .httpModule(new HttpModule()) .build() .inject(this); } //回调的成功方法 @Override public void onSuccess(List<ProductsBean.DataBean> list) { final List<ProductsBean.DataBean> tempList = new ArrayList<>(); tempList.addAll(list); //创建适配器 if (isRefresh) { adapter = new XrvListAdapter(this, list); mXrv.setAdapter(adapter); adapter.refresh(tempList); mXrv.refreshComplete();//设置刷新完成 } else { if (adapter != null) { //判断适配器是否创建过 adapter.loadMore(tempList); mXrv.loadMoreComplete();//设置加载更多完成 } } if (adapter == null) { return; } //点击传值 adapter.setOnListItemClickListener(new XrvListAdapter.OnListItemClickListener() { @Override public void OnItemClick(ProductsBean.DataBean dataBean) { //这是列表单商品 轮播图 //这是列表单商品 轮播图 //这是列表单商品 轮播图 Intent intent = new Intent(ListActivity.this, ListDetailsActivity.class); intent.putExtra("bean", dataBean); intent.putExtra("flag", LISTACTIVITY); startActivity(intent); } }); } }ListActivity
列表布局
<?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.example.com.a1512mvp_.ui.classify.ListActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#ff3660" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/ivZxing" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="15dp" android:background="@drawable/a_s"/> <LinearLayout android:layout_width="match_parent" android:layout_height="30dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_weight="1" android:background="@drawable/shape_search"></LinearLayout> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:background="@drawable/my_msg_bai"/> </LinearLayout> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xrv" android:layout_width="match_parent" android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView> </LinearLayout> classify包 presenter包
public class ListPresenter extends BasePresenter<ListContract.View> implements ListContract.Presenter { private LoginApi loginApi; @Inject public ListPresenter(LoginApi loginApi) { this.loginApi = loginApi; } @Override public void getProducts(String pscid) { loginApi.getProduct(pscid) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .map(new Function<ProductsBean, List<ProductsBean.DataBean>>() { @Override public List<ProductsBean.DataBean> apply(ProductsBean productsBean) throws Exception { return productsBean.getData(); } }).subscribe(new Consumer<List<ProductsBean.DataBean>>() { @Override public void accept(List<ProductsBean.DataBean> dataBeans) throws Exception { if (mView != null) { mView.onSuccess(dataBeans); } } }); } }
classify包
contrac包
public interface ListContract { interface View extends BaseContract.BaseView { void onSuccess(List<ProductsBean.DataBean> list); } interface Presenter extends BaseContract.BasePresenter<View> { void getProducts(String pscid); } }
classify包
Presenter包
ClassifyPresenter 层
public class ClassifyPresenter extends BasePresenter<ClassifyContract.View> implements ClassifyContract.Presenter { private LoginApi loginApi; @Inject public ClassifyPresenter(LoginApi loginApi) { this.loginApi = loginApi; } @Override//左边//左边 public void getCatagory() { loginApi.getCatagory() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Observer<CatagoryBean>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(CatagoryBean catagoryBean) { if (mView!=null){ mView.getCatagorySuccess(catagoryBean); } } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } @Override //右边 //右边 public void getProductCatagory(String cid) { loginApi.getProductCatagory(cid) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Observer<ProductCatagoryBean>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(ProductCatagoryBean productCatagoryBean) { if (mView!=null){ mView.getProductCatagorySuccess(productCatagoryBean); } } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } }classify包
contract包
ClassifyContract
public interface ClassifyContract { //主意主意 P层必须加泛型 和 View 关联 interface View extends BaseContract.BaseView{ //左边 void getCatagorySuccess(CatagoryBean catagoryBean); //右边 void getProductCatagorySuccess(ProductCatagoryBean productCatagoryBean); } interface Presenter extends BaseContract.BasePresenter<View>{ //左边 void getCatagory(); //右边 void getProductCatagory(String cid); } }
点击跳转
轮播图页面
ListDetailsActivity
public class ListDetailsActivity extends BaseActivity<AddCartPresenter> implements View.OnClickListener,AddCartContract.View { private ImageView mIvShare; private Banner mBanner; private TextView mTvTitle; private TextView mTvPrice; private TextView mTvVipPrice; private AdBean.TuijianBean.ListBean listBean; private ProductsBean.DataBean bean; private int flag; private String images; /** * 购物车 */ private TextView mTvShopCard; /** * 加入购物车 */ private TextView mTvAddCard; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //获取JavaBean Intent intent = getIntent(); flag = intent.getIntExtra("flag", -1); if (flag == -1) { return; } if (flag == ListActivity.LISTACTIVITY) { bean = (ProductsBean.DataBean) intent.getSerializableExtra("bean"); images = bean.getImages(); } else { listBean = (AdBean.TuijianBean.ListBean) intent.getSerializableExtra("bean"); images = listBean.getImages(); } //预显页面 initView(); //设置值 setData(); } //预显页面 private void initView() { mIvShare = (ImageView) findViewById(R.id.ivShare); mBanner = (Banner) findViewById(R.id.banner); mTvTitle = (TextView) findViewById(R.id.tvTitle); mTvPrice = (TextView) findViewById(R.id.tvPrice); mTvVipPrice = (TextView) findViewById(R.id.tvVipPrice); mTvShopCard = (TextView) findViewById(R.id.tvShopCard); mTvShopCard.setOnClickListener(this); mTvAddCard = (TextView) findViewById(R.id.tvAddCard); mTvAddCard.setOnClickListener(this); } //点击事件 @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.tvShopCard: //跳转到购物车 Intent intent = new Intent(ListDetailsActivity.this, ShopCartActivity.class); startActivity(intent); break; case R.id.tvAddCard: //先判断是否登录 String token = (String) SharedPreferencesUtils.getParam(ListDetailsActivity.this, "token", ""); if (TextUtils.isEmpty(token)) { //还未登录 //跳转到登录页面 Intent intent1 = new Intent(ListDetailsActivity.this, LoginActivity.class); startActivity(intent1); } else { //登录过了 String uid = (String) SharedPreferencesUtils.getParam(ListDetailsActivity.this, "uid", ""); int pid = 0; if (flag == ListActivity.LISTACTIVITY) { pid = bean.getPid(); } else { pid = listBean.getPid(); } Log.d("哈哈", "onClick: "+uid+" "+token); mPresenter.addCart(uid, pid + "", token); } break; } } //设置值 private void setData() { int money = 0; if (flag == ListActivity.LISTACTIVITY) { money = bean.getSalenum(); } else { money = listBean.getSalenum(); } //给原价加横线 SpannableString spannableString = new SpannableString("原价:" + money); StrikethroughSpan strikethroughSpan = new StrikethroughSpan(); spannableString.setSpan(strikethroughSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); mTvPrice.setText(spannableString); mBanner.setOnBannerListener(new OnBannerListener() { @Override public void OnBannerClick(int position) { Intent intent = new Intent(ListDetailsActivity.this, BannerDetailsActivity.class); intent.putExtra("imgs", images); intent.putExtra("position", position); startActivity(intent); } }); //设置图片加载器 mBanner.setImageLoader(new GlideImageLoader()); String[] imgs = null; if (flag == ListActivity.LISTACTIVITY) { imgs = bean.getImages().split("\\|"); mTvTitle.setText(bean.getTitle()); mTvVipPrice.setText("现价:" + bean.getPrice()); } else { imgs = listBean.getImages().split("\\|"); mTvTitle.setText(listBean.getTitle()); mTvVipPrice.setText("现价:" + listBean.getPrice()); } //设置图片集合 mBanner.setImages(Arrays.asList(imgs)); //banner设置方法全部调用完毕时最后调用 mBanner.start(); } @Override//获取布局方法 public int getContentLayout() { return R.layout.activity_list_details; } @Override//注入 public void inject() { DaggerHttpComponent.builder() .build() .inject(this); } @Override//成功回调的方法 public void onSuccess(String str) { Toast.makeText(ListDetailsActivity.this, str, Toast.LENGTH_LONG).show(); } }轮播图页面
ListDetailsActivity
布局
<?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.example.com.a1512mvp_.ui.classify.ListDetailsActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#33000000"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:background="@drawable/leftjiantou"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="商品详情"/> <ImageView android:id="@+id/ivShare" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="@drawable/share"/> </RelativeLayout> <com.youth.banner.Banner android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="200dp"></com.youth.banner.Banner> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/tvPrice" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/tvVipPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000"/> <View android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"></View> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/tvShopCard" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="购物车"/> <TextView android:id="@+id/tvAddCard" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#ff0000" android:gravity="center" android:text="加入购物车"/> </LinearLayout> </LinearLayout> classify包 presenter包 AddCartPresenter
购物车轮播图 public class AddCartPresenter extends BasePresenter<AddCartContract.View> implements AddCartContract.Presenter { private AddInterceptor_ addInterceptor_; @Inject public AddCartPresenter(AddInterceptor_ addInterceptor_) { this.addInterceptor_ = addInterceptor_; } @Override public void addCart(String uid, String pid, String token) { addInterceptor_.getCatagory(uid, pid, token) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .map(new Function<BaseBean, String>() { @Override public String apply(BaseBean baseBean) throws Exception { return baseBean.getMsg(); } }).subscribe(new Consumer<String>() { @Override public void accept(String s) throws Exception { if (mView != null) { mView.onSuccess(s); } } }); } } classify包 contract包 AddCartContract
public interface AddCartContract { interface View extends BaseContract.BaseView { void onSuccess(String str); } interface Presenter extends BaseContract.BasePresenter<View> { void addCart(String uid, String pid, String token); } }
点击图片跳转页面
点击图片放大
BannerDetailsActivity
public class BannerDetailsActivity extends AppCompatActivity { private HackyViewPager mHvp; private TextView mTv; private ProductsBean.DataBean bean; private List<String> list; private int position; private int count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_banner_details); //获取JavaBean Intent intent = getIntent(); position = intent.getIntExtra("position", -1); String images = intent.getStringExtra("imgs"); String[] split = images.split("\\|"); list = Arrays.asList(split); count = list.size(); //预显页面 initView(); } //预显页面 private void initView() { mHvp = (HackyViewPager) findViewById(R.id.hvp); mTv = (TextView) findViewById(R.id.tv); //添加适配器 MyAdapter myAdapter = new MyAdapter(this, list); mHvp.setAdapter(myAdapter); mHvp.setCurrentItem(position);//显示那一张图片 mTv.setText((position + 1) + "/" + count);//对应上面显示第几张 //当手机滑动 走这个方法 mHvp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override //当手机滑动 显示第几张 public void onPageSelected(int p) { BannerDetailsActivity.this.position = p; mTv.setText((position + 1) + "/" + count); } @Override public void onPageScrollStateChanged(int state) { } }); } }点击图片
跳转页面布局
点击图片放大
BannerDetailsActivity布局
<?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.example.com.a1512mvp_.ui.classify.BannerDetailsActivity"> <com.example.com.a1512mvp_.widjet.HackyViewPager android:id="@+id/hvp" android:layout_width="match_parent" android:layout_height="match_parent"> </com.example.com.a1512mvp_.widjet.HackyViewPager> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="50dp"/> </RelativeLayout> ListDetailsActivity 点击购物车按钮 跳转ShopCartActivity 这个很关键了
public class ShopCartActivity extends BaseActivity<ShopcartPresenter>implements ShopcartContract.View { private ExpandableListView mElv; private ProgressDialog progressDialog; //全选 private CheckBox mCbAll; //合计: private TextView mTvMoney; //去结算: private TextView mTvTotal; private ElvShopcartAdapter elvShopcartAdapter; //适配器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //预显页面 initView(); //初始化dialog progressDialog = DialogUtil.getProgressDialog(this); String token = (String) SharedPreferencesUtils.getParam(ShopCartActivity.this, "token", ""); String uid = (String) SharedPreferencesUtils.getParam(ShopCartActivity.this, "uid", ""); mPresenter.getCarts(uid,token); } //显示页面 private void initView() { mElv = (ExpandableListView) findViewById(R.id.elv); mCbAll = (CheckBox) findViewById(R.id.cbAll); mTvMoney = (TextView) findViewById(R.id.tvMoney); mTvTotal = (TextView) findViewById(R.id.tvTotal); //点击全选 mCbAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (elvShopcartAdapter != null) { progressDialog.show(); elvShopcartAdapter.changeAllState(mCbAll.isChecked()); } } }); //点击结算 mTvTotal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ShopCartActivity.this, MakeSureOrderActivity.class); startActivity(intent); //把用户选中的商品传过去 List<SellerBean> gList = elvShopcartAdapter.getGroupList(); List<List<GetCartsBean.DataBean.ListBean>> cList = elvShopcartAdapter.getchildList(); //存值 传值 EventBus MessageEvent messageEvent = new MessageEvent(); messageEvent.setcList(cList); messageEvent.setgList(gList); EventBus.getDefault().postSticky(messageEvent); } }); } @Override//获取布局 public int getContentLayout() { return R.layout.activity_shop_cart; } @Override//注入 public void inject() { DaggerHttpComponent.builder() .build() .inject(this); } @Override//查询购物车的方法 public void showCartList(List<SellerBean> groupList, List<List<GetCartsBean.DataBean.ListBean>> childList) { Log.d("zzzzzzzzzzz",groupList.size()+" "+childList.size()); elvShopcartAdapter = new ElvShopcartAdapter(this, groupList, childList, mPresenter, progressDialog); //判断商家是否全部选中 mCbAll.setChecked(isSellerAddSelected(groupList)); //添加适配器 mElv.setAdapter(elvShopcartAdapter); //获取数量和总价 适配器里调用的方法 String[] strings = elvShopcartAdapter.computeMoneyAndNum(); mTvMoney.setText("总计:" + strings[0] + "元"); mTvTotal.setText("去结算("+strings[1]+"个)"); //默认展开列表 for (int i = 0; i < groupList.size(); i++) { mElv.expandGroup(i); } //关闭进度条 progressDialog.dismiss(); } @Override//更新购物车的方法 public void updateCartsSuccess(String msg) { if (elvShopcartAdapter!=null){ elvShopcartAdapter.updataSuccess(); } } @Override//删除购物车的方法 public void deleteCartSuccess(String msg) { //调用适配器里的delSuccess()方法 if (elvShopcartAdapter!=null){ elvShopcartAdapter.delSuccess(); } } //判断所有商家是否全部选中 private boolean isSellerAddSelected(List<SellerBean> groupList) { for (int i = 0; i < groupList.size(); i++) { SellerBean sellerBean = groupList.get(i); if (!sellerBean.isSelected()) { return false; } } return true; } }
跳转 购物车布局 ShopCartActivity布局 这个很关键
<?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.example.com.a1512mvp_.ui.shopcart.ShopCartActivity"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:text="购物车"/> <ExpandableListView android:id="@+id/elv" android:groupIndicator="@null" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"></ExpandableListView> <RelativeLayout android:layout_width="match_parent" android:layout_height="40dp"> <CheckBox android:id="@+id/cbAll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全选"/> <TextView android:id="@+id/tvMoney" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/cbAll" android:text="合计:"/> <TextView android:id="@+id/tvTotal" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:textColor="#ffffff" android:background="#ff0000" android:gravity="center" android:layout_marginLeft="10dp" android:text="去结算:"/> </RelativeLayout> </LinearLayout> 购物车工具类 存值用 ShopCartActivity 里面有个跳转
public class MessageEvent { private List<SellerBean> gList; private List<List<GetCartsBean.DataBean.ListBean>> cList; public List<SellerBean> getgList() { return gList; } public void setgList(List<SellerBean> gList) { this.gList = gList; } public List<List<GetCartsBean.DataBean.ListBean>> getcList() { return cList; } public void setcList(List<List<GetCartsBean.DataBean.ListBean>> cList) { this.cList = cList; } } 购物车适配器 ShopCartActivity适配器 非常重要
public class ElvShopcartAdapter extends BaseExpandableListAdapter { private Context context; private List<SellerBean> groupList; private List<List<GetCartsBean.DataBean.ListBean>> childList; private ProgressDialog progressDialog; private LayoutInflater inflater; private ShopcartPresenter shopcartPresenter; private final String uid; private final String token; private int productIndex; private int groupPosition; private boolean checked; private static final int GETCARTS = 0;//查询购物车 private static final int UPDATE_PRODUCT = 1; //更新商品 private static final int UPDATE_SELLER = 2; //更新卖家 private static int state = GETCARTS; private boolean allSelected; public ElvShopcartAdapter(Context context, List<SellerBean> groupList, List<List<GetCartsBean.DataBean.ListBean>> childList, ShopcartPresenter shopcartPresenter, ProgressDialog progressDialog) { this.context=context; //上下文 this.childList=childList;//二级集合 this.groupList=groupList;//一级集合 this.progressDialog=progressDialog;初始化进度对话框 this.shopcartPresenter=shopcartPresenter;//P层 inflater = LayoutInflater.from(context); //布局 //获取存储的 uid token 值 uid = (String) SharedPreferencesUtils.getParam(context, "uid", ""); token = (String) SharedPreferencesUtils.getParam(context, "token", ""); } //重写适配器方法10个 @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.get(groupPosition).get(groupPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } //一级商+家列表 @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { final GroupViewHolder groupViewHolder; if (convertView == null) { groupViewHolder = new GroupViewHolder(); //获取一级布局 convertView = inflater.inflate(R.layout.shopcart_seller_item, null); groupViewHolder.cbSeller = convertView.findViewById(R.id.cbSeller); groupViewHolder.tvSeller = convertView.findViewById(R.id.tvSeller); convertView.setTag(groupViewHolder); } else { groupViewHolder = (GroupViewHolder) convertView.getTag(); } //一级布局设置值 SellerBean sellerBean = groupList.get(groupPosition); groupViewHolder.tvSeller.setText(sellerBean.getSellerName()); groupViewHolder.cbSeller.setChecked(sellerBean.isSelected()); //给商家checkbox设置点击事件 groupViewHolder.cbSeller.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //设置当前的更新状态 state = UPDATE_PRODUCT; //显示进度条 progressDialog.show(); //默认从第一个商品开始更新购物车状态 productIndex = 0; //全局记录一下当前更新的商家 ElvShopcartAdapter.this.groupPosition = groupPosition;//自己调用自己 //该商家是否选中 checked = groupViewHolder.cbSeller.isChecked(); //更新商家下的商品状态 updateProductInSeller(); } }); return convertView; } //二级商+品列表 @Override public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { //获取二级布局 final ChildViewHolder childViewHolder; if (convertView == null) { childViewHolder = new ChildViewHolder(); //获取二级布局 convertView = inflater.inflate(R.layout.shopcart_seller_product_item, null); //二级布局组件 childViewHolder.cbProduct = convertView.findViewById(R.id.cbProduct); childViewHolder.iv = convertView.findViewById(R.id.iv); childViewHolder.tvTitle = convertView.findViewById(R.id.tvTitle); childViewHolder.tvPrice = convertView.findViewById(R.id.tvPrice); childViewHolder.tvDel = convertView.findViewById(R.id.tvDel); childViewHolder.addSubView = convertView.findViewById(R.id.addSubCard); convertView.setTag(childViewHolder); } else { childViewHolder = (ChildViewHolder) convertView.getTag(); } //给二级组件赋值 final GetCartsBean.DataBean.ListBean listBean = childList.get(groupPosition).get(childPosition); //根据服务器返回的select值,给checkBox设置是否选中 childViewHolder.cbProduct.setChecked(listBean.getSelected() == 1 ? true : false); //给二级组件赋值 childViewHolder.tvTitle.setText(listBean.getTitle()); childViewHolder.tvPrice.setText(listBean.getPrice() + ""); Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(childViewHolder.iv); childViewHolder.addSubView.setNum(listBean.getNum() + ""); //给二级列表的checkbox设置点击事件 childViewHolder.cbProduct.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { state = GETCARTS; //显示进度条 progressDialog.show(); ElvShopcartAdapter.this.groupPosition = groupPosition; //调用更新购物车接口,改变购物车的状态 //获取卖家id String sellerid = groupList.get(groupPosition).getSellerid(); //获取pid String pid = listBean.getPid() + ""; //是否选中 boolean childChecked = childViewHolder.cbProduct.isChecked(); shopcartPresenter.updateCarts(uid, sellerid, pid, "1", childChecked ? "1" : "0", token); } }); //给加号设置点击事件 //给加号设置点击事件 childViewHolder.addSubView.setAddOnclickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressDialog.show(); state = GETCARTS; //获取sellerId String sellerid = groupList.get(groupPosition).getSellerid(); //获取pid int pid = listBean.getPid(); //获取数量 int num = listBean.getNum(); num += 1; //是否选中 String isChecked = childViewHolder.cbProduct.isChecked() ? "1" : "0"; //调用更新购物车的接口即可 shopcartPresenter.updateCarts(uid, sellerid, pid + "", num + "", isChecked, token); } }); //给减号设置点击事件 //给减号设置点击事件 childViewHolder.addSubView.setSubOnclickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressDialog.show(); state = GETCARTS; //获取当前商品的数量 int num = listBean.getNum(); if (num <= 1) { progressDialog.dismiss(); Toast.makeText(context, "数量不能小于1", Toast.LENGTH_SHORT).show(); return; } num -= 1; //获取sellerId String sellerid = groupList.get(groupPosition).getSellerid(); //获取pid int pid = listBean.getPid(); //是否选中 String isChecked = childViewHolder.cbProduct.isChecked() ? "1" : "0"; //更新购物车 shopcartPresenter.updateCarts(uid, sellerid, pid + "", num + "", isChecked, token); } }); //给删除设置点击事件 //给删除设置点击事件 childViewHolder.tvDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressDialog.show(); state = GETCARTS; //获取pid int pid = listBean.getPid(); //删除购物车里的选项 shopcartPresenter.deleteCart(uid, pid + "", token); } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } //一级ViewHolder class GroupViewHolder{ CheckBox cbSeller; TextView tvSeller; } //二级ViewHolder class ChildViewHolder { CheckBox cbProduct; ImageView iv; TextView tvTitle; TextView tvPrice; TextView tvDel; AddSubView addSubView; } //计算数量和价钱的方法 public String[] computeMoneyAndNum() { double sum = 0; int num = 0; for (int i = 0; i < groupList.size(); i++) { for (int j = 0; j < childList.get(i).size(); j++) { //判断商品是否选中 GetCartsBean.DataBean.ListBean listBean = childList.get(i).get(j); if (listBean.getSelected() == 1) { //该商品为选中状态 sum += listBean.getPrice() * listBean.getNum(); num += listBean.getNum(); } } } return new String[]{sum + "", num + ""}; } //更新商家下的商品状态 private void updateProductInSeller() { //获取SellerId SellerBean sellerBean = groupList.get(groupPosition); String sellerid = sellerBean.getSellerid(); //获取pid GetCartsBean.DataBean.ListBean listBean = childList.get(groupPosition).get(productIndex); int num = listBean.getNum(); int pid = listBean.getPid(); shopcartPresenter.updateCarts(uid, sellerid, pid + "", num + "", checked ? "1" : "0", token); } // private void updateProductInSeller(boolean bool) { //获取SellerId SellerBean sellerBean = groupList.get(groupPosition); String sellerid = sellerBean.getSellerid(); //获取pid GetCartsBean.DataBean.ListBean listBean = childList.get(groupPosition).get(productIndex); int pid = listBean.getPid(); int num = listBean.getNum(); shopcartPresenter.updateCarts(uid, sellerid, pid + "", num + "", bool ? "1" : "0", token); } //更新购物车成功回调的方法 public void updataSuccess() { switch (state) { case GETCARTS: //更新成功以后调用查询购物车接口 productIndex = 0; groupPosition = 0; shopcartPresenter.getCarts(uid, token); break; case UPDATE_PRODUCT: //更新成功一个商品以后,再接着更新该商家下面的其它商品,直到没有商品为止 productIndex++; //下标是否越界 if (productIndex < childList.get(groupPosition).size()) { //可以继续跟新商品 updateProductInSeller(); } else { //商品已经全部更新完成,请查询购物车 state = GETCARTS; updataSuccess(); } break; case UPDATE_SELLER: //遍历所有商家下的商品,并更新状态 productIndex++; //下标是否越界 if (productIndex < childList.get(groupPosition).size()) { //可以继续跟新商品 updateProductInSeller(allSelected); } else { //商品已经全部更新完成,请查询购物车 productIndex = 0; groupPosition++; if (groupPosition < groupList.size()) { //可以继续跟新商品 updateProductInSeller(allSelected); } else { //商品已经全部更新完成,请查询购物车 state = GETCARTS; updataSuccess(); } } break; } } //删除成功回调接口 public void delSuccess() { shopcartPresenter.getCarts(uid, token); } //遍历商家下的商品,修改状态 public void changeAllState(boolean bool) { this.allSelected = bool; state = UPDATE_SELLER; updateProductInSeller(bool); } //创建集合 public List<SellerBean> getGroupList() { //先创建一个集合 List<SellerBean> gList = new ArrayList<>(); //遍历原先的groupList for (int i = 0; i < groupList.size(); i++) { if (groupList.get(i).isSelected()) { gList.add(groupList.get(i)); } } return gList; } //创建集合 public List<List<GetCartsBean.DataBean.ListBean>> getchildList() { List<List<GetCartsBean.DataBean.ListBean>> cList = new ArrayList<>(); for (int i = 0; i < groupList.size(); i++) { List<GetCartsBean.DataBean.ListBean> l = new ArrayList<>(); for (int j = 0; j < childList.get(i).size(); j++) { if (childList.get(i).get(j).getSelected() == 1) { l.add(childList.get(i).get(j)); } } if (l.size()>0){ cList.add(l); } } return cList; } } 购物车适配器布局 ShopCartActivity适配器布局 一级列表 getGroupView布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="30dp" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cbSeller" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp"/> <TextView android:id="@+id/tvSeller" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp"/> </LinearLayout> 购物车适配器布局 ShopCartActivity适配器布局 二级列表 getCheildView布局
<?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"> <CheckBox android:id="@+id/cbProduct" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp"/> <TextView android:id="@+id/tvDel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="#ff0000" android:gravity="center_vertical" android:text="删除" android:textColor="#ffffff"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@id/tvDel" android:layout_toRightOf="@id/cbProduct" android:orientation="horizontal"> <ImageView android:id="@+id/iv" android:layout_width="70dp" android:layout_height="70dp" android:layout_gravity="center_vertical"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:orientation="vertical"> <TextView android:id="@+id/tvTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tvPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="12312" android:textColor="#ff0000"/> <com.example.com.a1512mvp_.widjet.AddSubView android:id="@+id/addSubCard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"/> </RelativeLayout> </LinearLayout> </LinearLayout> </RelativeLayout>二级列表里面的自定义View
自定义 加减号 View
AddSubView
public class AddSubView extends LinearLayout { private TextView sub; private TextView num; private TextView add; public AddSubView(Context context) { this(context, null); } public AddSubView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); View view = LayoutInflater.from(context).inflate(R.layout.add_sub_view, this); sub = findViewById(R.id.child_text_jian); num = findViewById(R.id.child_text_num); add = findViewById(R.id.child_text_add); } //设置数量 public void setNum(String str) { num.setText(str); } //获取数量 public String getNum() { return num.getText().toString(); } //给加号设置点击事件 public void setAddOnclickListener(OnClickListener onclickListener) { add.setOnClickListener(onclickListener); } //给减号设置点击事件 public void setSubOnclickListener(OnClickListener onclickListener) { sub.setOnClickListener(onclickListener); } } 自定义View AddSubView布局 加减号 布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/child_image" android:layout_marginRight="5dp" android:layout_toLeftOf="@+id/child_text_delete" android:orientation="horizontal"> <TextView android:id="@+id/child_text_jian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/cart_biankuang" android:padding="5dp" android:text="-"/> <TextView android:id="@+id/child_text_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/cart_biankuang" android:paddingBottom="5dp" android:paddingLeft="8dp" android:paddingRight="8dp" android:paddingTop="5dp"/> <TextView android:id="@+id/child_text_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/cart_biankuang" android:padding="5dp" android:text="+"/> </LinearLayout>自定义View加减号background
加减号里面的background
在目录 drawable 里面写
减号 加号用一个
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff" /> <stroke android:width="0.1dp" android:color="#000000" /> </shape>点击结算跳转页面
MakeSureOrderActivity
定位
这个页面没有做完 就是定位
public class MakeSureOrderActivity extends BaseActivity<MakeSureOrderPresenter> implements MakeSureOrderContract.View { private ImageView mDetailImageBack; private RelativeLayout mDetaiRelative; /** * 收货人: */ private TextView mTextName; private TextView mTextPhone; private ImageView mDingWeiImage; private TextView mTextAddr; private RelativeLayout mRelativeAddr01; private ExpandableListView elv; /** * 实付款:¥0.00 */ private TextView mTextShiFuKu; /** * 提交订单 */ private TextView mTextSubmitOrder; private LinearLayout mLinearBottom; private ProgressDialog progressDialog; //声明AMapLocationClient类对象 public AMapLocationClient mLocationClient = null; //声明定位回调监听器 /* public AMapLocationListener mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation amapLocation) { amapLocation.getCity();//城市信息 amapLocation.getDistrict();//城区信息 amapLocation.getStreet();//街道信息 amapLocation.getStreetNum();//街道门牌号信息 } }; //声明AMapLocationClientOption对象 public AMapLocationClientOption mLocationOption = null;*/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /*//初始化定位 mLocationClient = new AMapLocationClient(getApplicationContext()); //设置定位回调监听 mLocationClient.setLocationListener(mLocationListener); //初始化AMapLocationClientOption对象 mLocationOption = new AMapLocationClientOption(); *//** * 设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景)*//* mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn); if (null != mLocationClient) { mLocationClient.setLocationOption(mLocationOption); //设置场景模式后最好调用一次stop,再调用start以保证场景模式生效 mLocationClient.stopLocation(); mLocationClient.startLocation(); } //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //获取一次定位结果: //该方法默认为false。 mLocationOption.setOnceLocation(true); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption); //启动定位 mLocationClient.startLocation();*/ EventBus.getDefault().register(this); //初始化dialog progressDialog = DialogUtil.getProgressDialog(this); initView(); //先去获取常用收货地址列表 if (mPresenter != null) { mPresenter.getAddrs(getUid(), getToken()); } } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Override public int getContentLayout() { return R.layout.activity_make_sure_order; } @Override public void inject() { DaggerHttpComponent.builder() .build() .inject(this); } private void initView() { mDetailImageBack = (ImageView) findViewById(R.id.detail_image_back); mDetaiRelative = (RelativeLayout) findViewById(R.id.detai_relative); mTextName = (TextView) findViewById(R.id.text_name); mTextPhone = (TextView) findViewById(R.id.text_phone); mDingWeiImage = (ImageView) findViewById(R.id.ding_wei_image); mTextAddr = (TextView) findViewById(R.id.text_addr); mRelativeAddr01 = (RelativeLayout) findViewById(R.id.relative_addr_01); elv = (ExpandableListView) findViewById(R.id.elv); mTextShiFuKu = (TextView) findViewById(R.id.text_shi_fu_ku); mTextSubmitOrder = (TextView) findViewById(R.id.text_submit_order); mLinearBottom = (LinearLayout) findViewById(R.id.linear_bottom); } @Subscribe(threadMode = ThreadMode.MainThread, sticky = true) public void onMessageEvent(final MessageEvent event) { /* Do something */ new Handler().postDelayed(new Runnable() { @Override public void run() { List<List<GetCartsBean.DataBean.ListBean>> cList = event.getcList(); List<SellerBean> gList = event.getgList(); MakeSureOrderAdapter adapter = new MakeSureOrderAdapter(MakeSureOrderActivity.this, gList, cList, progressDialog, mPresenter); elv.setAdapter(adapter); for (int i = 0; i < gList.size(); i++) { elv.expandGroup(i); } } }, 1000); } @Override public void addrsSuccess(List<AddrsBean.DataBean> list) { if (list != null && list.size() > 0) { //如果有数据,说明之前添加过地址 toast("获取到地址列表"); mTextName.setText(list.get(0).getName()); mTextAddr.setText(list.get(0).getAddr()); } else { //如果没有数据,则没有则弹出一个dialog onGetDefaultAddrEmpty(); } } @Override public void createOrderSuccess(String msg) { } @Override public void updateCartsSuccess(String msg) { //关闭进度条 progressDialog.dismiss(); //把未选中的条目移除掉,去再次获取购物车 } @Override public void deleteCartSuccess(String msg) { } public void onGetDefaultAddrEmpty() { //弹出对话框...取消,,,finish/确定...添加新地址...没添加点击了返回,当前确认订单页面finish,,,如果添加了显示地址 AlertDialog.Builder builder = new AlertDialog.Builder(MakeSureOrderActivity.this); builder.setTitle("提示") .setMessage("您还没有默认的收货地址,请设置收货地址") .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //结束确认订单显示 MakeSureOrderActivity.this.finish(); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //确定...跳转添加新地址...如果没有保存地址,确认订单finish,,, //如果保存了地址...数据传回来进行显示(带有回传值的跳转) Intent intent = new Intent(MakeSureOrderActivity.this, AddNewAddrActivity.class); startActivityForResult(intent, 1001); } }) .show(); } } 定位页面布局 MakeSureOrderActivity 布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!--标题--> <RelativeLayout android:id="@+id/detai_relative" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/detail_image_back" android:layout_width="40dp" android:layout_height="40dp" android:padding="5dp" android:src="@drawable/leftjiantou"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="确认订单"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!--地址--> <RelativeLayout android:id="@+id/relative_addr_01" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <TextView android:id="@+id/text_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/text_addr" android:text="收货人:"/> <TextView android:id="@+id/text_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"/> <ImageView android:id="@+id/ding_wei_image" android:layout_width="20dp" android:layout_height="20dp" android:layout_below="@+id/text_name" android:layout_marginTop="10dp" android:src="@drawable/ding_wei"/> <TextView android:id="@+id/text_addr" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/text_name" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:layout_toRightOf="@+id/ding_wei_image"/> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="10dp" android:background="#f4f4f4"/> </LinearLayout> <!--列表展示商品 可以使用recyclerView 还可以使用自定义的listView--> <ExpandableListView android:id="@+id/elv" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/linear_bottom" android:layout_below="@+id/detai_relative" > </ExpandableListView> <!--底部 提交订单--> <LinearLayout android:id="@+id/linear_bottom" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <TextView android:id="@+id/text_shi_fu_ku" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:gravity="center_vertical|right" android:padding="10dp" android:text="实付款:¥0.00" android:textColor="#ff0000"/> <TextView android:id="@+id/text_submit_order" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#ff0000" android:gravity="center" android:text="提交订单" android:textColor="#ffffff"/> </LinearLayout> </LinearLayout>
Fragment05
我的页面 点击换头像