底部的fragment:可以用dialogfragment从底部弹出,我这里选的第三方
compile 'me.shaohui:bottomdialog:1.1.9'//底部弹窗
1.自定义bottomdialog继承bottomdialog:
public class MyBottomShit extends BaseBottomDialog {
private ArrayList mTimeList;
private RecyclerView mRightRecycleview;
private Context mContext;
private List mHangKongData;
private RecyclerView mRightHangKongRecycleview;
private TextView mTvTime;
private TextView mTvHangKongCompany;
private RecyclerView mRvYouSeleted;
private AdapterBottomTime adapterBottomTime;
private AdapterCompany adapterBottomCompany;
private TextView mTvClearAll;
private TextView mTvBottomSure;
private TextView mTvBottomcalcel;
private List mCheckTimeList = new ArrayList<>();//时间数据
private List mCheckCompanyList = new ArrayList<>();//公司数据
private RxBaseActivity mActivity;
public List mSelectedInnertimeList = new ArrayList<>();
public List mSelectedInnerCompanyList = new ArrayList<>();
private boolean isAddFirstCOmpanyData = false;
private LinearLayoutManager mCompanyManagers;
private HashMap mIntegerBooleanHashMap;//公司的勾选控制
private List mCompanyKeys = new ArrayList<>();//存储的公司勾选key
private HashMap mCompanyHashMap = new HashMap<>();//选择单位公司
private LinearLayoutManager mTimeLinearLayoutManager;
private AdapterBottomCompany mAdapterBottomCompany;
private ImageView mIvClear;
private Boolean isAssociate = false;//是否联动
@Override
public int getLayoutRes() {
return R.layout.dialog_layout;
}
@Override
public int getHeight() {
return SystemUtil.dp2px(260);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext =context;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = (RxBaseActivity) activity;
}
@Override
public void bindView(View v) {
mRightRecycleview = v.findViewById(R.id.rv_right_details);
mRightHangKongRecycleview = v.findViewById(R.id.rv_right_hangkong);
mTvTime = v.findViewById(R.id.tv_qifei_time);
mTvHangKongCompany = v.findViewById(R.id.tv_hangkong_company);
mRvYouSeleted = v.findViewById(R.id.rv_you_selected);
mTvClearAll = v.findViewById(R.id.tv_clear_all);
mTvBottomSure = v.findViewById(R.id.tv_bottom_sure);
mTvBottomcalcel = v.findViewById(R.id.tv_bottom_cancel);
mIvClear = v.findViewById(R.id.iv_clear);
getTheDate();
initRecycleview();
setData();
mTvBottomcalcel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
mTvBottomSure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getTargetFragment() != null) {
Intent resultIntent = new Intent();
// resultIntent.putCharSequenceArrayListExtra("timemap",mSelectedInnertimeList);
resultIntent.putExtra("timemap",(Serializable) mSelectedInnertimeList);
resultIntent.putExtra("companylist",(Serializable)mSelectedInnerCompanyList);
getTargetFragment().onActivityResult(300,
Activity.RESULT_OK,
resultIntent);
dismiss();
}
// LocationUtil.getInstance().getLatelyLocationInfo(mActivity)
// .flatMap(info -> HttpManage.getAviation().getAirInfo(
// info.getLongitude(), info.getLatitude(),
// 1, 999, 1, "","D","",""))
// .compose(mActivity.bindToLifecycle())
// .compose(new RequestTransformer<>())
// .subscribe(new ProgressSubscriber<>(mContext, new OnResultCallBack() {
// @Override
// public void onSuccess(ChaXunHangBanBean result) {
// if (result.getData().size() >0 || null != result.getData()) {
// List data = result.getData();
// if (data.size() > 0) {
mdata.clear();
mdata.addAll(data);
adapterItemDIGang = new AdapterItemDIGang(R.layout.item_digang_adapter, mdata);
rvGuonei.setLayoutManager(new LinearLayoutManager(mContext));
rvGuonei.setAdapter(adapterItemDIGang);
adapterItemDIGang.notifyDataSetChanged();
// } else {
// // mMultipleStatusView.showEmpty();
// }
// }
// }
//
// @Override
// public void onError(int code, String errorMsg, Throwable e) {
// Log.e("TAG", "errorMsg=" + errorMsg);
// mMultipleStatusView.showError();
// }
// }));
}
});
mTvTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ToastUtils.show("点击了起飞时间");
mRightRecycleview.setVisibility(View.VISIBLE);
mRightHangKongRecycleview.setVisibility(View.INVISIBLE);
mTvClearAll.setVisibility(View.VISIBLE);
mIvClear.setVisibility(View.VISIBLE);
}
});
mTvHangKongCompany.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ToastUtils.show("点击了航空公司");
mRightHangKongRecycleview.setVisibility(View.VISIBLE);
mRightRecycleview.setVisibility(View.INVISIBLE);
mTvClearAll.setVisibility(View.INVISIBLE);
mIvClear.setVisibility(View.INVISIBLE);
}
});
mTvClearAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSelectedInnerCompanyList.clear();
mSelectedInnertimeList.clear();
for (int i=0;i0){//
// Set integers = mCompanyHashMap.keySet();
// Iterator mKeyIn = integers.iterator();
// while (mKeyIn.hasNext()){
// Integer next = mKeyIn.next();
// mCompanyHashMap.put(next,false);
// }
//
// adapterBottomCompany.setMyCHeckBoxState(mCompanyHashMap);
// adapterBottomCompany.notifyDataSetChanged();
//
// }else {
// ToastUtils.show("数据已清空");
// }
//
//
// // adapterBottomCompany.resetCheckBoxState(mIntegerBooleanHashMap);
//
// mSelectedInnerCompanyList.clear();
// mSelectedInnerCompanyList.add("");
//
// Log.d("lwp","keys:"+mCompanyKeys.size());
}
});
// you can do bind view operation
Log.d("lwp","MyBottomShit start");
}
@SuppressLint("UseSparseArrays")
private void initRecycleview() {
int size = mTimeList.size();
mCheckTimeList.clear();
for (int i= 0;i0 ){
mSelectedInnertimeList.remove(mSelectedInnertimeList.indexOf(i));
}
}
}else {
if (mSelectedInnertimeList.size() >0) {
mSelectedInnertimeList.remove(mSelectedInnertimeList.indexOf(pos));
// for (int i=0;i0 ){
// mSelectedInnerCompanyList.remove(i);
// }
//
// }
// }else {
if (mSelectedInnerCompanyList.size() >0) {
mSelectedInnerCompanyList.remove(pos+"");
}
mCheckCompanyList.get(pos).setIscheck(ischeck);
}
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
mAdapterBottomCompany.notifyDataSetChanged();
}
};
handler.post(r);
// adapterBottomTime.notifyDataSetChanged();
// adapterBottomTime.setNewData(mCheckTimeList);
}
// }
});
//ignore
// this.adapterBottomCompany.setOnMyItemClickListener(new AdapterCompany.OnMyItemClickListener() {
// @Override
// public void onCHeckBoxClick(int pos,boolean ischeck) {
// if (pos == 0){
// Iterator i = mIntegerBooleanHashMap.entrySet().iterator();
// while (i.hasNext()) {
// Integer obj = (Integer) i.next();
// Integer key = obj;
// mCompanyKeys.add(key);
// }
// if (mCompanyHashMap.size() >0){//
// Set integers = mCompanyHashMap.keySet();
// Iterator mKeyIn = integers.iterator();
// while (mKeyIn.hasNext()){
// Integer next = mKeyIn.next();
// mCompanyHashMap.put(next,true);
// }
//
// adapterBottomCompany.setMyCHeckBoxState(mCompanyHashMap);
//
// adapterBottomCompany.notifyDataSetChanged();
//
// }
// mSelectedInnerCompanyList.clear();
// mSelectedInnerCompanyList.add("");
// }
// mCompanyHashMap.put(pos,ischeck);
// if (ischeck){
// mSelectedInnerCompanyList.add(mHangKongData.get(pos).getCode());
// }else {
// mSelectedInnerCompanyList.remove(mHangKongData.get(pos).getCode());
// }
// }
// });
//控制勾选
mIntegerBooleanHashMap = new HashMap<>();
if (false){
// adapterBottomCompany.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
// @Override
// public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
// if (view.getId() == R.id.btn_selected_company){
// String code = mHangKongData.get(position).getCode();
// mSelectedInnerCompanyList.add(code);
// mIntegerBooleanHashMap.put(position,true);
// // adapterBottomCompany.resetCheckBoxState(mIntegerBooleanHashMap);
// if (position == 0){
// // CheckBox mComPanyCheckBox = mCompanyManagers.getChildAt(8).findViewById(R.id.btn_selected_company);
// // CheckBox mComPanyCheckBox = mCompanyManagers.findViewByPosition(8).findViewById(R.id.btn_selected_company);
// // View viewByPosition = mCompanyManagers.findViewByPosition(8);
int firstVisibleItemPosition = ((LinearLayoutManager) mCompanyManagers).findFirstVisibleItemPosition();
CheckBox mComPanyCheckBox = mCompanyManagers.getChildAt(8 - firstVisibleItemPosition).findViewById(R.id.btn_selected_company);
mComPanyCheckBox.setChecked(true);
// }
// }
// }
// });
}
// adapterBottomCompany.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
// @Override
// public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
// // BaseApplication.selectedInnerCompanyList.add(mHangKongData.get(position).getCode());
if (null == BaseApplication.selectedInnerCompanyList.get(position)){
BaseApplication.selectedInnerCompanyList.put(position,position);
}else {
ToastUtils.show("您已经添加过了公司");
}
// }
// });
}
private void setData() {
}
private void getTheDate() {
Bundle mBundle = getArguments();
mTimeList = mBundle.getStringArrayList("bottomarray");
mHangKongData = (List) mBundle.getSerializable("bottomcompany");
Log.d("lwp","timesize:"+mTimeList.size());
}
@Override
public void onDestroyView() {
if (null != mSelectedInnertimeList || mSelectedInnertimeList.size()>0){
mSelectedInnertimeList.clear();
}
if (null != mSelectedInnerCompanyList || mSelectedInnerCompanyList.size()>0){
mSelectedInnerCompanyList.clear();
}
super.onDestroyView();
}
}
2.点击外部的筛选
mBtnShaiXuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putStringArrayList("bottomarray", (ArrayList) mBottomTime);
bundle.putSerializable("bottomcompany", (Serializable) mHangKongData);
mBottomShit.setArguments(bundle);
mBottomShit.setTargetFragment(DiGangFragment.this,300);
mBottomShit.show(getFragmentManager());
}
});
注意的是 这个启动BottomShit的fragment中需要加上setTargetFragment,不然在dialogfragment返回到当前fragment的时候没办法接收到数据
3.设置当前fragment的OnActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK ){
if (requestCode == 300){
List oldselectedInnertimeList = (List) data.getSerializableExtra("timemap");
List selectedInnertimeList = removeDuplicate(oldselectedInnertimeList);
List oldselectedInnerCompanyList = (List) data.getSerializableExtra("companylist");
List selectedInnerCompanyList = removeStrDuplicate(oldselectedInnerCompanyList);
//companylist
//处理时间
mTime = new StringBuffer();
Log.d("lwp","mcompany.seze:"+selectedInnerCompanyList.size());
mStringCompany = new StringBuffer();
int sb_length = mTime.length();// 取得字符串的长度
mTime.delete(0,sb_length);
int msbv = mStringCompany.length();// 取得字符串的长度
mStringCompany.delete(0,msbv);
if ( selectedInnertimeList.size() ==0){
// ToastUtils.show("暂无时间数据");
mTime.append("");
}else {
int size = selectedInnertimeList.size();
for (int i=0;i HttpManage.getAviation().getAirInfo(
info.getLongitude(), info.getLatitude(),
1, 999, 1, "","D",mTime.toString(),mStringCompany.toString()))
.compose(bindToLifecycle())
.compose(new RequestTransformer<>())
.subscribe(new ProgressSubscriber<>(mContext, new OnResultCallBack() {
@Override
public void onSuccess(ChaXunHangBanBean result) {
if (result.getData().size() >0 || null != result.getData()) {
List data = result.getData();
if (data.size() > 0) {
mdata.clear();
mdata.addAll(data);
adapterItemDIGang = new AdapterItemDIGang(R.layout.item_digang_adapter, mdata);
rvGuonei.setLayoutManager(new LinearLayoutManager(mContext));
rvGuonei.setAdapter(adapterItemDIGang);
adapterItemDIGang.notifyDataSetChanged();
} else {
mMultipleStatusView.showEmpty();
}
}
}
@Override
public void onError(int code, String errorMsg, Throwable e) {
Log.e("TAG", "errorMsg=" + errorMsg);
mMultipleStatusView.showError();
}
}));
}
}
}
4.说一下recycleview中checkbox的点击处理,首先是
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyTimeHolder){
((MyTimeHolder) holder).mTvTime.setText(mCHeckList.get(position).getTime());
((MyTimeHolder) holder).mCheckBox.setChecked(mCHeckList.get(position).isCHeck());
((MyTimeHolder) holder).mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onItemChildClickListener.OnItemClick(position,isChecked);
}
});
}
}
注意是设置setOnCheckedChangeListener不是setOnClickListenber,然后设置一个javabean对象存入列表相应的位置以及是否选中的状态,点击完之后通知adapter刷新数据,如我定义的javabean对象:
public class CheckTimeBean {
int pos;
String time;
boolean isCHeck;
public int getPos() {
return pos;
}
public void setPos(int pos) {
this.pos = pos;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public boolean isCHeck() {
return isCHeck;
}
public void setCHeck(boolean CHeck) {
isCHeck = CHeck;
}
}
刚开始默认是false转台需要自己初始化设置:
int size = mTimeList.size();
mCheckTimeList.clear();
for (int i= 0;i
参考:https://blog.csdn.net/cxc19890214/article/details/73484728