mvp
api 包下的网络接口
Api
public class Api {
public static final String urlSTRING = "http://172.17.8.100/";
public static final String searchSTRING = "small/commodity/v1/findCommodityByKeyword";
public static final String shopCardSTRING= "ks/product/getCarts?uid=51";
}
ApiService
在到包的时候一定注意
import io.reactivex.Observable; import retrofit2.http.GET; import retrofit2.http.QueryMap;
public interface ApiServices {
@GET(Api.searchSTRING)
Observable<SearchBean> getSearch(@QueryMap HashMap<String,String> params);
@GET(Api.shopCardSTRING)
Observable<ShopCardBean> getShopCard();
}
contracts
IContactClass
public interface IContractClass {
//V
interface MainView{
void Success(Object result);
void Error(Object msg);
}
//m
interface MainModel{
void getModel(HashMap<String,String > params, RetrofitCallBack retrofitCallBack);
}
//p
public abstract class MainPresetner{
public abstract void getPresenter(HashMap<String,String>params);
}
}
ShopCardContractClass
public interface ShopCardContractClass {
//view
interface MainView{
void ShopSuccess(Object result);
void ShopError(Object msg);
}
//m
interface MiainModel{
void getShopModel(RetrofitCallBack retrofitCallBack);
}
//p
public abstract class MainPresetner{
public abstract void getShopPresenter();
}
}
model
RetrofitCallBack
public interface RetrofitCallBack {
void Success(Object result);
void Error(Object msg);
}
SearchModel
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
public class SearchModel implements IContractClass.MainModel {
private ApiServices apiServices;
@Override
public void getModel(HashMap<String, String> params, final RetrofitCallBack retrofitCallBack) {
ApiServices apiServices = RetrofitUtils.getInstance().create(ApiServices.class);
apiServices.getSearch(params)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableObserver<SearchBean>() {
@Override
public void onNext(SearchBean searchBean) {
retrofitCallBack.Success(searchBean);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
ShopCardModel
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
public class ShopCardModel implements ShopCardContractClass.MiainModel {
@Override
public void getShopModel(final RetrofitCallBack retrofitCallBack) {
ApiServices apiServices = RetrofitUtils.getInstance().create(ApiServices.class);
apiServices.getShopCard()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableObserver<ShopCardBean>() {
@Override
public void onNext(ShopCardBean shopCardBean) {
retrofitCallBack.Success(shopCardBean);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
presenter
SearchPresenter
public class SearchPresenter extends IContractClass.MainPresetner {
private IContractClass.MainView mainView;
private SearchModel searchModel;
public SearchPresenter(IContractClass.MainView mainView){
this.mainView=mainView;
searchModel = new SearchModel();
}
@Override
public void getPresenter(HashMap<String, String> params) {
searchModel.getModel(params, new RetrofitCallBack() {
@Override
public void Success(Object result) {
mainView.Success(result);
}
@Override
public void Error(Object msg) {
mainView.Error(msg);
}
});
}
public void onDestroy(){
if (searchModel!=null){
searchModel=null;
}else if (mainView!=null){
mainView=null;
}
}
}
ShopCardPresenter
public class ShopCardPresenter extends ShopCardContractClass.MainPresetner {
private ShopCardContractClass.MainView mainView;
private ShopCardModel shopCardModel;
public ShopCardPresenter(ShopCardContractClass.MainView mainView) {
this.mainView = mainView;
shopCardModel = new ShopCardModel();
}
@Override
public void getShopPresenter() {
shopCardModel.getShopModel(new RetrofitCallBack() {
@Override
public void Success(Object result) {
mainView.ShopSuccess(result);
}
@Override
public void Error(Object msg) {
mainView.ShopError(msg);
}
});
}
public void onDestroy(){
if (shopCardModel!=null){
shopCardModel=null;
}else if (mainView!=null){
mainView=null;
}
}
}
util
RetrofitUtils
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitUtils {
private static RetrofitUtils retrofitUtils;
private final Retrofit retrofit;
public static RetrofitUtils getInstance(){
if (retrofitUtils==null){
synchronized (RetrofitUtils.class){
if (retrofitUtils==null){
retrofitUtils=new RetrofitUtils();
}
}
}
return retrofitUtils;
}
private RetrofitUtils(){
retrofit = new Retrofit.Builder()
.baseUrl(Api.urlSTRING)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
public <T>T create(Class<T> tClass){
return retrofit.create(tClass);
}
}
MainAcitvity
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.day_20.frag.HomeFrag;
import com.example.day_20.frag.MeFrag;
import com.example.day_20.frag.ShopCardFrag;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.viewPager)
ViewPager viewPager;
@BindView(R.id.button1)
RadioButton button1;
@BindView(R.id.button2)
RadioButton button2;
@BindView(R.id.button3)
RadioButton button3;
@BindView(R.id.radioGroup)
RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
final List<Fragment> list = new ArrayList<>();
list.add(new HomeFrag());
list.add(new ShopCardFrag());
list.add(new MeFrag());
radioGroup.check(R.id.button1);
viewPager.setOffscreenPageLimit(2);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
switch (i){
case 0:
radioGroup.check(R.id.button1);
break;
case 1:
radioGroup.check(R.id.button2);
break;
case 2:
radioGroup.check(R.id.button3);
break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.button1:
viewPager.setCurrentItem(0);
break;
case R.id.button2:
viewPager.setCurrentItem(1);
break;
case R.id.button3:
viewPager.setCurrentItem(2);
break;
}
}
});
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
}
}
布局
Main布局
<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="66dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="66dp"
android:button="@null"
android:gravity="center"
android:text="首页"/>
<RadioButton
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="66dp"
android:button="@null"
android:gravity="center"
android:text="购物车"/>
<RadioButton
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="66dp"
android:button="@null"
android:gravity="center"
android:text="我的"/>
</RadioGroup>
</LinearLayout>
fraghome
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.example.day_20.activity.SeahView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.day_20.activity.SeahView>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
fragme 创建一个布局就可以
fragshopcard
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvT"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:paddingTop="20dp"
>
<CheckBox
android:id="@+id/all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
/>
<TextView
android:id="@+id/allPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="总价: "/>
</RelativeLayout>
</LinearLayout>
iteam 搜索展示的布局
<LinearLayout 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"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="150dp"
android:layout_height="150dp"
app:viewAspectRatio="1"
android:id="@+id/img"
android:layout_gravity="center"
app:roundedCornerRadius="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="66dp"
android:text="123"
android:textSize="18sp"
android:id="@+id/names"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="33dp"
android:text="123"
android:textSize="18sp"
android:id="@+id/names1"
android:gravity="center"/>
</LinearLayout>
iteam_shop 购物车商品的条目
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/oneCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"/>
<TextView
android:id="@+id/name_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"
android:text="123"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvT1"
android:layout_width="match_parent"
android:layout_marginLeft="66dp"
android:layout_height="wrap_content"/>
</LinearLayout>
iteam_view
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="60dp"
android:text=""/>
<EditText
android:id="@+id/edit"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="60dp" />
<Button
android:id="@+id/search"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="66dp"
android:text="搜索"/>
</LinearLayout>
iteams
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal"
xmlns:app="http://schemas.android.com/apk/res-auto">
<CheckBox
android:id="@+id/twoCheck"
android:layout_width="wrap_content"
android:layout_height="100dp" />
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="100dp"
android:layout_height="100dp"
app:viewAspectRatio="1"
android:id="@+id/img1"
android:layout_gravity="center"
app:roundedCornerRadius="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123"
android:textSize="18sp"
android:id="@+id/names3"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123"
android:textSize="18sp"
android:id="@+id/names4"
/>
<com.example.day_20.activity.View_add_del
android:id="@+id/views_add"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp">
</com.example.day_20.activity.View_add_del>
</LinearLayout>
</LinearLayout>
view_add_del
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/del"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"/>
<TextView
android:id="@+id/edit_num"
android:layout_width="55dp"
android:text="1"
android:layout_height="wrap_content" />
<Button
android:id="@+id/add"
android:layout_width="33dp"
android:layout_height="wrap_content"
android:text="+"/>
</LinearLayout>