1.Api
public interface Api { @GET("video/findVideos?") Observable<VideoEntity> getVideoData(@Query("currentPage")int currentPage ,@Query("pageSize")int pageSize); @POST("/user/loginjson") Observable<UserEntity> getLoginData(@Body RequestBody body); }
2.RetrofitManager
public class RetrofitManager { public static RetrofitManager retrofitManager=null; public RetrofitManager() { } public static RetrofitManager getInstance() { if(retrofitManager==null){ retrofitManager=new RetrofitManager(); } return retrofitManager; } public Retrofit retrofit; public Retrofit getRetrofit() { if(retrofit==null){ retrofit=createRetrofit(); } return retrofit; } private Retrofit createRetrofit() { OkHttpClient client=new OkHttpClient.Builder() .writeTimeout(5, TimeUnit.MINUTES) .readTimeout(5, TimeUnit.MINUTES) .callTimeout(5, TimeUnit.MINUTES) .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .build(); retrofit=new Retrofit.Builder() .client(client) .baseUrl("http://10.161.9.80:7012") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit; } }
3.BaseModel
public class BaseModel { protected Api api; public BaseModel() { api= RetrofitManager.getInstance().getRetrofit().create(Api.class); } }
4.BaseViewModel
public abstract class BaseViewModel<BaMo extends BaseModel> extends ViewModel { protected BaMo mModel; public BaseViewModel() { mModel=createModel(); } protected abstract BaMo createModel(); }
5.BaseActivity
public abstract class BaseActivity<VDB extends ViewDataBinding,VM extends BaseViewModel> extends AppCompatActivity { protected VDB viewDataBind; protected VM viewModel; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); viewDataBind= DataBindingUtil.setContentView(this,getLayoutId()); viewModel= new ViewModelProvider(this).get(getViewModel()); initView(); initData(); handObserver(); } protected void handObserver() { } protected abstract void initView(); protected abstract void initData(); protected abstract Class<VM> getViewModel(); protected abstract int getLayoutId(); }
BaseFragment
public abstract class BaseFragment<VDB extends ViewDataBinding,VM extends BaseViewModel> extends Fragment {
protected VDB viewDataBind;
protected VM viewModel;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
viewDataBind= DataBindingUtil.inflate(inflater,getLyoutId(),container,false);
viewModel=new ViewModelProvider(this).get(getViewModel());
initView();
initData();
handObserver();
return viewDataBind.getRoot();
}
protected void handObserver() {
}
protected abstract void initData();
protected abstract void initView() ;
protected abstract Class<VM> getViewModel();
protected abstract int getLyoutId();
}
上拉加载刷新
\
轮播图
Activity
public class MainActivity extends AppCompatActivity { ActivityMainBinding activityMainBinding ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); activityMainBinding= DataBindingUtil.setContentView(this,R.layout.activity_main); FragmentManager supportFragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fl, HomeFragment.getInstance()); fragmentTransaction.commit(); activityMainBinding.bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()){ case R.id.home: Fragment(HomeFragment.getInstance()); break; case R.id.friends: Fragment(FriendsFragment.getInstance()); break; case R.id.kiabo: Fragment(KaiboFragment.getInstance()); break; case R.id.shop: Fragment(ShopFragment.getInstance()); break; case R.id.mine: Fragment(MineFragment.getInstance()); break; } return true; } }); } public void Fragment(Fragment fragment){ FragmentManager supportFragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fl,fragment); fragmentTransaction.commit(); } }
activity布局
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="8" android:id="@+id/fl"></FrameLayout> <com.google.android.material.bottomnavigation.BottomNavigationView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" app:menu="@menu/menu" android:id="@+id/bottom"></com.google.android.material.bottomnavigation.BottomNavigationView> </LinearLayout>
CommonTabLayout