記錄時(shí)間:2017-12-11 15:48
話不多說(shuō),先上圖缔莲,有需要的伙伴相詳看:
基本樣式是這樣的(GIF制作略顯麻煩哥纫,求原諒)。
說(shuō)一下需求痴奏,點(diǎn)擊篩選按鈕彈出此頁(yè)面蛀骇。進(jìn)行多項(xiàng)條件篩選,篩選完成點(diǎn)擊確定加載數(shù)據(jù)读拆。
彈出頁(yè)面我用了PopupWindow喜歡用dialog實(shí)現(xiàn)的請(qǐng)自動(dòng)忽略擅憔。。檐晕。
為了實(shí)現(xiàn)這種稍顯復(fù)雜的頁(yè)面暑诸,自定義了PopupWindow
public class FiltratePopup extends PopupWindow {
private View view;
private RecyclerAdapter mAdapter;
FiltratePopup(final Context mContext, List<RecyclerModel> listData , View.OnClickListener listener) {
this.view = LayoutInflater.from(mContext).inflate(R.layout.popup_filtrate_view, null);
TextView mBtnSure = view.findViewById(R.id.btn_sure);
mBtnSure.setOnClickListener(listener);
RecyclerView mRecyclerView = view.findViewById(R.id.recyclerView);
//設(shè)置布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
mRecyclerView.setLayoutManager(linearLayoutManager);
//設(shè)置recycleView 滑動(dòng)的方向
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mAdapter = new RecyclerAdapter(mContext,listData,R.layout.item_recy1);
mRecyclerView.setAdapter(mAdapter);
// 設(shè)置外部可點(diǎn)擊
this.setOutsideTouchable(true);
// mMenuView添加OnTouchListener監(jiān)聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
this.view.setOnTouchListener((v, event) -> {
int height = view.findViewById(R.id.pop_layout).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
});
/* 設(shè)置彈出窗口特征 */
// 設(shè)置視圖
this.setContentView(this.view);
// 設(shè)置彈出窗體的寬和高
this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
// 設(shè)置彈出窗體可點(diǎn)擊
this.setFocusable(true);
// 實(shí)例化一個(gè)ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
// 設(shè)置彈出窗體的背景
this.setBackgroundDrawable(dw);
}
//設(shè)置公共方法 為了獲取選擇地值
Map<String, String> getCheckData() {
return mAdapter.getCheckData();
}
@Override
public void showAsDropDown(View anchor) {
if (Build.VERSION.SDK_INT >= 24) {
Rect rect = new Rect();
anchor.getGlobalVisibleRect(rect);
int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
setHeight(h);
}
super.showAsDropDown(anchor);
}
}
樣式和布局操作都在里面進(jìn)行設(shè)置,其中RecyclerView的Adapter 參考了紅橙Darren的RecyclerView更全解析之 - 打造通用的萬(wàn)能Adapter 非常感謝 紅橙Darren大神的無(wú)私奉獻(xiàn)辟灰。
展示通用的Adapter使用
public class RecyclerAdapter extends CommonRecyclerAdapter<RecyclerModel> {
private Map<String, String> mapCheckData = new HashMap<>(); //存放點(diǎn)擊后的選擇項(xiàng)
private Context mContext;
public RecyclerAdapter(Context mContext, List<RecyclerModel> mData, int mLayoutId) {
super(mContext, mData, mLayoutId);
this.mContext = mContext;
}
@Override
public void convert(ViewHolder holder, RecyclerModel item, int position) {
holder.setText(R.id.tx_title, item.getTitle());
List<String> listData = item.getContentList();
FiltrateAdapter mAdapter = new FiltrateAdapter(mContext, listData, 1);
holder.setGridViewAdapter(R.id.gv_filtrate, mAdapter,
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mAdapter.refresh(i, listData.size()); //點(diǎn)擊后刷新頁(yè)面顯示效果
//判斷當(dāng)前選項(xiàng)是否被選中
if (mapCheckData.get(item.getTitle()) != null &&
mapCheckData.get(item.getTitle()).equals(listData.get(i))) {
mapCheckData.remove(item.getTitle());
} else {
mapCheckData.put(item.getTitle(), listData.get(i));
}
}
});
}
Map<String, String> getCheckData() {
return mapCheckData;
}
}
在MainActivity中引用自定義的PopupWindow
//設(shè)置popupView
private void setPopupView() {
mPopup = new FiltratePopup(getBaseContext(), mListData, view -> {
mapCheckData = mPopup.getCheckData();
if (mapCheckData != null && mapCheckData.size() > 0) {
Toast.makeText(getBaseContext(), mapCheckData.toString(), Toast.LENGTH_SHORT).show();
mTextView.setText(mapCheckData.toString());
}
mPopup.dismiss();
});
}
private void initView() {
mFiltrateLay = (LinearLayout) findViewById(R.id.lay_filtrate);
mFiltrateLay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mPopup.showAsDropDown(mFiltrateLay);
}
});
mTextView = (TextView) findViewById(R.id.textView3);
}
}
如果想加酷炫的動(dòng)畫效果个榕,請(qǐng)自行添加。芥喇。笛洛。