您的位置:首页 > 其它

网上预约挂号之左右列表

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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bmob