Android實(shí)現(xiàn)簡(jiǎn)單下拉篩選框
最近接到一個(gè)新的項(xiàng)目,項(xiàng)目時(shí)間比較緊張,有一個(gè)功能類(lèi)似于58同城,京東的一個(gè)下拉篩選框,為了節(jié)省時(shí)間,從網(wǎng)上面拷貝了一份封裝好的代碼,進(jìn)行的自己的一些修改,感覺(jué)靈活性還挺高的,分享出來(lái)給大家看一看
大致效果如下,可以自己加入自己的布局

先看一下這個(gè)ExpandTabView這個(gè)類(lèi) 代碼比較簡(jiǎn)單 我就不做具體介紹了 有不懂的可以私信我
public class ExpandTabView extends LinearLayout implements OnDismissListener {
private ToggleButton selectedButton;
private ArrayList<String> mTextArray = new ArrayList<String>();
private ArrayList<RelativeLayout> mViewArray = new ArrayList<RelativeLayout>();
private ArrayList<ToggleButton> mToggleButton = new ArrayList<ToggleButton>();
private Context mContext;
private final int SMALL = 0;
private int displayWidth;
private int displayHeight;
private PopupWindow popupWindow;
private int selectPosition;
public ExpandTabView(Context context) {
super(context);
init(context);
}
public ExpandTabView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
/**
* 根據(jù)選擇的位置設(shè)置tabitem顯示的值
*/
public void setTitle(String valueText, int position) {
if (position < mToggleButton.size()) {
mToggleButton.get(position).setText(valueText);
}
}
public void setTitle(String title){
}
/**
* 根據(jù)選擇的位置獲取tabitem顯示的值
*/
public String getTitle(int position) {
if (position < mToggleButton.size() && mToggleButton.get(position).getText() != null) {
return mToggleButton.get(position).getText().toString();
}
return "";
}
/**
* 設(shè)置tabitem的個(gè)數(shù)和初始值
*/
public void setValue(ArrayList<String> textArray, ArrayList<View> viewArray) {
if (mContext == null) {
return;
}
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mTextArray = textArray;
for (int i = 0; i < viewArray.size(); i++) {
final RelativeLayout r = new RelativeLayout(mContext);
int maxHeight = (int) (displayHeight * 0.7);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, maxHeight);
rl.leftMargin = 10;
rl.rightMargin = 10;
r.addView(viewArray.get(i), rl);
mViewArray.add(r);
r.setTag(SMALL);
ToggleButton tButton = (ToggleButton) inflater.inflate(R.layout.toggle_button, this, false);
addView(tButton);
View line = new TextView(mContext);
line.setBackgroundResource(R.drawable.choosebar_line);
if (i < viewArray.size() - 1) {
LayoutParams lp = new LayoutParams(2, LayoutParams.FILL_PARENT);
addView(line, lp);
}
mToggleButton.add(tButton);
tButton.setTag(i);
tButton.setText(mTextArray.get(i));
r.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onPressBack();
}
});
r.setBackgroundColor(mContext.getResources().getColor(R.color.popup_main_background));
tButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// initPopupWindow();
ToggleButton tButton = (ToggleButton) view;
if (selectedButton != null && selectedButton != tButton) {
selectedButton.setChecked(false);
}
selectedButton = tButton;
selectPosition = (Integer) selectedButton.getTag();
startAnimation();
if (mOnButtonClickListener != null && tButton.isChecked()) {
mOnButtonClickListener.onClick(selectPosition);
}
}
});
}
}
private void startAnimation() {
if (popupWindow == null) {
popupWindow = new PopupWindow(mViewArray.get(selectPosition), displayWidth, displayHeight);
popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
popupWindow.setFocusable(false);
popupWindow.setOutsideTouchable(true);
}
if (selectedButton.isChecked()) {
if (!popupWindow.isShowing()) {
showPopup(selectPosition);
} else {
popupWindow.setOnDismissListener(this);
popupWindow.dismiss();
hideView();
}
} else {
if (popupWindow.isShowing()) {
popupWindow.dismiss();
hideView();
}
}
}
private void showPopup(int position) {
View tView = mViewArray.get(selectPosition).getChildAt(0);
if (tView instanceof ViewBaseAction) {
ViewBaseAction f = (ViewBaseAction) tView;
f.show();
}
if (popupWindow.getContentView() != mViewArray.get(position)) {
popupWindow.setContentView(mViewArray.get(position));
}
popupWindow.showAsDropDown(this, 0, 0);
}
/**
* 如果菜單成展開(kāi)狀態(tài),則讓菜單收回去
*/
public boolean onPressBack() {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
hideView();
if (selectedButton != null) {
selectedButton.setChecked(false);
}
return true;
} else {
return false;
}
}
private void hideView() {
View tView = mViewArray.get(selectPosition).getChildAt(0);
if (tView instanceof ViewBaseAction) {
ViewBaseAction f = (ViewBaseAction) tView;
f.hide();
}
}
private void init(Context context) {
mContext = context;
displayWidth = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth();
displayHeight = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight();
setOrientation(LinearLayout.HORIZONTAL);
}
@Override
public void onDismiss() {
showPopup(selectPosition);
popupWindow.setOnDismissListener(null);
}
private OnButtonClickListener mOnButtonClickListener;
/**
* 設(shè)置tabitem的點(diǎn)擊監(jiān)聽(tīng)事件
*/
public void setOnButtonClickListener(OnButtonClickListener l) {
mOnButtonClickListener = l;
}
/**
* 自定義tabitem點(diǎn)擊回調(diào)接口
*/
public interface OnButtonClickListener {
public void onClick(int selectPosition);
}
}
這個(gè)代碼基本就是對(duì)popupwindow進(jìn)行了封裝,通過(guò)對(duì)ToggleButton按鈕的監(jiān)聽(tīng)來(lái)實(shí)現(xiàn)popupwindow的彈出和收回。
外部設(shè)置的話,也特別簡(jiǎn)單,只需要將自己定義好的布局傳入到list集合中就可以。
下面是MainActivity中的代碼
public class MainActivity extends AppCompatActivity {
private ExpandTabView expandTabView;
private ArrayList<View> mViewArray = new ArrayList<View>();
private ViewLeft viewLeft;
private ViewMiddle viewMiddle;
private ViewRight viewRight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initVaule();
initListener();
}
private void initView() {
expandTabView = (ExpandTabView) findViewById(R.id.expandtab_view);
viewLeft = new ViewLeft(this);
viewMiddle = new ViewMiddle(this);
viewRight = new ViewRight(this);
}
private void initVaule() {
mViewArray.add(viewMiddle);
mViewArray.add(viewLeft);
mViewArray.add(viewRight);
ArrayList<String> mTextArray = new ArrayList<String>();
mTextArray.add("區(qū)域");
mTextArray.add("距離");
mTextArray.add("距離");
expandTabView.setValue(mTextArray, mViewArray);
// expandTabView.setTitle(viewLeft.getShowText(), 0);
// expandTabView.setTitle(viewMiddle.getShowText(), 1);
// expandTabView.setTitle(viewRight.getShowText(), 2);
}
private void initListener() {
viewLeft.setOnSelectListener(new ViewLeft.OnSelectListener() {
@Override
public void getValue(String distance, String showText) {
onRefresh(viewLeft, showText);
}
});
viewMiddle.setOnSelectListener(new ViewMiddle.OnSelectListener() {
@Override
public void getValue(String showText) {
onRefresh(viewMiddle,showText);
}
});
viewRight.setOnSelectListener(new ViewRight.OnSelectListener() {
@Override
public void getValue(String distance, String showText) {
onRefresh(viewRight, showText);
}
});
}
private void onRefresh(View view, String showText) {
expandTabView.onPressBack();
int position = getPositon(view);
if (position >= 0 && !expandTabView.getTitle(position).equals(showText)) {
expandTabView.setTitle(showText, position);
}
// Toast.makeText(MainActivity.this, showText, Toast.LENGTH_SHORT).show();
}
private int getPositon(View tView) {
for (int i = 0; i < mViewArray.size(); i++) {
if (mViewArray.get(i) == tView) {
return i;
}
}
return -1;
}
@Override
public void onBackPressed() {
if (!expandTabView.onPressBack()) {
finish();
}
}
}
以上就是這個(gè)篩選菜單欄的大致用法,個(gè)人感覺(jué)還是比較簡(jiǎn)單的,也比較靈活,修改起來(lái)也比較方便。
但是在項(xiàng)目中使用的時(shí)候碰到了一個(gè)問(wèn)題,就是popupwindow在7.0的手機(jī)上彈出位置異常的問(wèn)題,,查了一下,是因?yàn)槭謾C(jī)狀態(tài)欄高度的問(wèn)題
于是重寫(xiě)了一下popupwindow的showAsDropDown方法就解決了,下面是具體代碼
@Override
public void showAsDropDown(View anchor, int xoff, int yoff) {
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, xoff, yoff);
}
通過(guò)對(duì)SDK版本來(lái)進(jìn)行判斷,大于24的話就執(zhí)行這個(gè)方法,解決了popupwindow在7.0手機(jī)上異常彈出的問(wèn)題。
最后附上Demo地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Android RecyclerView UI的滾動(dòng)控件示例
本篇文章主要介紹了淺談Android RecyclerView UI的滾動(dòng)控件示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Android 中動(dòng)態(tài)加載.jar的實(shí)現(xiàn)步驟
本文介紹動(dòng)態(tài)加載 .jar的實(shí)現(xiàn)步驟,這將對(duì)你的android開(kāi)發(fā)很有幫助,剛興趣的朋友可以了解下哦2013-01-01
Android入門(mén)之使用SharedPreference存取信息詳解
這篇文章主要為大家詳細(xì)介紹了Android如何使用SharedPreference實(shí)現(xiàn)存取信息,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-12-12
Android學(xué)習(xí)小結(jié)之獲取被啟動(dòng)的Activity傳回的數(shù)據(jù)
這篇文章主要介紹了獲取被啟動(dòng)的Activity傳回的數(shù)據(jù),非常不錯(cuò),介紹的非常詳細(xì),需要的朋友可以參考下2016-08-08
如何在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動(dòng)效果
本篇文章是對(duì)在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動(dòng)效果進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android輸入法與表情面板切換時(shí)的界面抖動(dòng)問(wèn)題解決方法
這篇文章主要介紹了Android輸入法與表情面板切換時(shí)的界面抖動(dòng)問(wèn)題解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
AndroidHttpClient詳解及調(diào)用示例
本文給大家介紹AndroidHttpClient結(jié)構(gòu)、使用方式及調(diào)用示例詳解,需要的朋友可以參考下2015-10-10

