网上预约挂号之左右列表
2017-11-18 19:53
232 查看
左右列表:
RecyclerView+CardView+SwipeRefreshLayout+Bmob内置的Rxjava使用Bmob3.5.1操作数据库,该sdk内置了Rxjava包,所以就来学习一下观察者模式
工具类 GetDepartmentUtil获取大科(左)列表数据
Observable:被观察者,可以理解为数据发射源,通过subscriber.onNext发送出去,返回一个List类型的Observable
public Observable<List<BigDepartment>> getBD(){ return Observable.create(new Observable.OnSubscribe<List<BigDepartment>>() { @Override public void call(final Subscriber<? super List<BigDepartment>> subscriber) { if(!subscriber.isUnsubscribed()){ BmobQuery<BigDepartment> bmobQuery = new BmobQuery<BigDepartment>(); Observable<List<BigDepartment>> observable = bmobQuery.findObjectsObservable(BigDepartment.class); observable.subscribe(new Subscriber<List<BigDepartment>>() { @Override public void onCompleted() { } @Override public void onError(Throwable throwable) { } @Override public void onNext(List<BigDepartment> bigDepartments) { subscriber.onNext(bigDepartments); } }); } } }); }
获取小科(右)列表数据
外键查询需要传入ObjectId参数
public Observable<List<Department>> getD(final String objectId){ return Observable.create(new Observable.OnSubscribe<List<Department>>() { @Override public void call(final Subscriber<? super List<Department>> subscriber) { if(!subscriber.isUnsubscribed()){ BmobQuery<Department> query = new BmobQuery<Department>(); BigDepartment bigDepartment = new BigDepartment(); bigDepartment.setObjectId(objectId); query.addWhereEqualTo("d_bigdepartmentcode",new BmobPointer(bigDepartment)); query.include("Department,BigDepartment,d_bigdepartmentcode"); Observable<List<Department>> observable = query.findObjectsObservable(Department.class); observable.subscribe(new Subscriber<List<Department>>() { @Override public void onCompleted() { } @Override public void onError(Throwable throwable) { } @Override public void onNext(List<Department> departments) { subscriber.onNext(departments); } }); } } }); }
View
<?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/left_list" android:layout_width="100dp" android:layout_height="match_parent" android:background="@color/orange02" android:scrollbars="none" /> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/sfl" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:paddingTop="10dp" android:id="@+id/right_list" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" /> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>
左列表
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="80dp" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@color/orange02" 4000 android:layout_margin="10dp" android:foreground="?attr/selectableItemBackground" app:cardBackgroundColor="@color/white" app:cardCornerRadius="5dp" app:cardElevation="10dp" > <TextView android:id="@+id/view" android:background="@color/orange02" android:layout_width="match_parent" android:layout_height="20dp"/> <TextView android:layout_gravity="center" android:id="@+id/left_b" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:text="辅助部门" /> </android.support.v7.widget.CardView>
右列表
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView 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:background="@color/orange02" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:foreground="?attr/selectableItemBackground" app:cardBackgroundColor="@color/white" app:cardElevation="5dp"> <TextView android:gravity="center_vertical" android:id="@+id/right_d" android:drawableRight="@drawable/right_arrow" android:paddingRight="10dp" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingLeft="15dp" android:text="内科" android:layout_width="match_parent" android:layout_height="60dp" /> </android.support.v7.widget.CardView>
adapter
这里参考了一篇博客写的适配器 RecycleView 实现左右列表并排
就是设置了一个回调接口,设置Item的点击和点击时样式改变
左右列表的adapter都是差不多的,都是List类型
Activity
设置列表的数据,更新UI
也就是观察者去获取被观察者发射的数据,然后订阅确定两者的关系,在回调函数中onNext设置Adapter,传递参数去获取右列表的数据,在点击item之前得将数据源清除
new GetDepartmentUtil().getBD()//获取数据 .subscribeOn(Schedulers.io())//IO .observeOn(AndroidSchedulers.mainThread())//运行在主线程 .subscribe(new Subscriber<List<BigDepartment>>(){回调的函数}
设置刷新,使用谷歌官方的刷新控件SwipeRefreshLayout
protected void initRefreshLayout(final int position) { mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light); //给swipeRefreshLayout绑定刷新监听 mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //设置2秒的时间来执行以下事件 new Handler().postDelayed(new Runnable() { public void run() { //在这里清空之前的数据 clearData(); //清空数据后还得传入该页的position leftAdapter.notifyDataSetChanged(); leftAdapter.setPosition(position); leftAdapter.notifyDataSetChanged(); //此时才刷新右边列表 setRightData(position); rightAdapter.notifyDataSetChanged(); //重置为没有刷新,即为可刷新 mSwipeRefreshLayout.setRefreshing(false); } }, 1000); } }); }
代码:
public class SelectDepartmentActivity extends AppCompatActivity{ @BindView(R.id.left_list) RecyclerView leftrv; @BindView(R.id.right_list) RecyclerView rightrv; @BindView(R.id.sfl) SwipeRefreshLayout mSwipeRefreshLayout; private boolean mIsRefreshing = false; private MostLeftAdapter leftAdapter; private MostRightAdapter rightAdapter; List<String> bdnames = new ArrayList<String>(); List<String> bdids = new ArrayList<String>(); List<String> dnames = new ArrayList<String>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_select_department); ButterKnife.bind(this); setLeftName(); } /** * 设置左边的数据源 */ private void setLeftName() { new GetDepartmentUtil().getBD().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<List<BigDepartment>>() { @Override public void onCompleted() { } @Override public void onError(Throwable throwable) { } @Override public void onNext(List<BigDepartment> bigDepartments) { for(BigDepartment bigDepartment:bigDepartments){ String name = bigDepartment.getBd_name(); String id = bigDepartment.getObjectId(); bdnames.add(name); bdids.add(id); } Log.i("数据",bdnames + ""); Log.i("数据",bdids.get(0) + ""); Log.i("数据",bdids + ""); LinearLayoutManager layoutManager = new LinearLayoutManager(SelectDepartmentActivity.this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); leftrv.setLayoutManager(layoutManager); leftAdapter = new MostLeftAdapter(SelectDepartmentActivity.this,bdnames); leftrv.setAdapter(leftAdapter); mSwipeRefreshLayout.measure(0,0); initRefreshLayout(0); //默认根据left的第一项数据去加载右边得数据 setRightData(0); if(!mIsRefreshing){ initRefreshLayout(0); } leftAdapter.notifyDataSetChanged(); leftAdapter.setOnItemClickListener(new MostLeftAdapter.MyItemClickListener() { @Override public void onItemClick(View view, int position) { clearData(); //设置position,根据position的状态刷新 leftAdapter.setPosition(position); leftAdapter.notifyDataSetChanged(); // //在这里清空之前的数据 // clearData(); //加载当前界面完成 setRightData(position); //如果还想继续刷新该页 if(!mIsRefreshing){ //刷新后重新置为没有刷新 initRefreshLayout(position); } } }); } }); } private void setRightData(int position) { new GetDepartmentUtil().getD(bdids.get(position)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<List<Department>>() { @Override public void onCompleted() { } @Override public void onError(Throwable throwable) { } @Override public void onNext(List<Department> departments) { for(Department department:departments){ String name = department.getD_departmentname(); dnames.add(name); } LinearLayoutManager layoutManager1 = new LinearLayoutManager(SelectDepartmentActivity.this); layoutManager1.setOrientation(LinearLayoutManager.VERTICAL); rightrv.setLayoutManager(layoutManager1); rightAdapter = new MostRightAdapter(SelectDepartmentActivity.this,dnames); // rightrv.removeAllViews(); // rightAdapter.notifyDataSetChanged(); rightrv.setAdapter(rightAdapter); rightAdapter.setOnItemClickListener(new MostRightAdapter.MyItemClickListener() { @Override public void onItemClick(View view, int postion) { } }); } }); } protected void initRefreshLayout(final int position) { mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light); //给swipeRefreshLayout绑定刷新监听 mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.On a9fc RefreshListener() { @Override public void onRefresh() { //设置2秒的时间来执行以下事件 new Handler().postDelayed(new Runnable() { public void run() { //在这里清空之前的数据 clearData(); //清空数据后还得传入该页的position leftAdapter.notifyDataSetChanged(); leftAdapter.setPosition(position); leftAdapter.notifyDataSetChanged(); //此时才刷新右边列表 setRightData(position); rightAdapter.notifyDataSetChanged(); //重置为没有刷新,即为可刷新 mSwipeRefreshLayout.setRefreshing(false); } }, 1000); } }); } /** * 清除数据 */ private void clearData() { dnames.clear(); rightrv.removeAllViews(); } }
相关文章推荐
- jquery控制左右箭头滚动图片列表
- 第三天:浪迹天涯网上商城(1.0版本)--后台管理系统--商品列表的查询
- 标准c++去 string 对象 所有空格%%%%%%%%%%去 左右空格代码,在网上找半天,不如自己写的好
- 学习笔记——下拉列表左右选择
- js案例之全选与下拉列表左右选择
- Jquery实现下拉列表左右选择
- 可拖动拉伸的左右分栏效果,本例子是用Table以及JS来实现的,左右分栏可改变,拖动中间的边框就可实现宽度的调整,点击时可设置为隐藏左侧列表。
- Jq和JS实现下拉列表左右选择(时间)
- dhtmlxgrid实现左右列表数据移动
- Python程序访问北京预约挂号平台
- Android 使用SwipeActionAdapter开源库实现简单列表的左右滑动操作
- 表格中用js左右添加从下拉列表中选择的内容
- java结合jQuery.ajax实现左右菜单联动刷新列表内容
- IOS超级简单上下左右滚动TableView,仿同花顺自选列表
- JavaScript Select和Option列表元素上下左右移动
- Python程序访问北京预约挂号平台
- 下拉列表的左右选择的实现
- 分时分级诊疗微信预约系统解决医院挂号难的问题!
- 微信医疗健康分时分级挂号预约系统——乐享其约
- js-案例:下拉列表左右选择分析