版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中实现一个下拉筛选框
今天就跟大家聊聊有关怎么在Android中实现一个下拉筛选框,可能很多人都不太了解,为了让大家更加了解,在下给大家总结了以下内容,希望大家根据这篇文章可以有所收获。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);
}
/**
*
根据选择的位置设置tabitem显示的值
*/
public
void
setTitle(String
valueText,
int
position)
{
if
(position
<
mToggleButton.size())
{
mToggleButton.get(position).setText(valueText);
}
}
public
void
setTitle(String
title){
}
/**
*
根据选择的位置获取tabitem显示的值
*/
public
String
getTitle(int
position)
{
if
(position
<
mToggleButton.size()
&&
mToggleButton.get(position).getText()
!=
null)
{
return
mToggleButton.get(position).getText().toString();
}
return
"";
}
/**
*
设置tabitem的个数和初始值
*/
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);
}
/**
*
如果菜单成展开状态,则让菜单收回去
*/
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;
/**
*
设置tabitem的点击监听事件
*/
public
void
setOnButtonClickListener(OnButtonClickListener
l)
{
mOnButtonClickListener
=
l;
}
/**
*
自定义tabitem点击回调接口
*/
public
interface
OnButtonClickListener
{
public
void
onClick(int
selectPosition);
}
}这个代码基本就是对popupwindow进行了封装,通过对ToggleButton按钮的监听来实现popupwindow的弹出和收回。外部设置的话,也特别简单,只需要将自己定义好的布局传入到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("区域");
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();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 钢结构工业厂房施工组织设计
- 五星级高层酒店客房装修工程施工组织设计#福建#卫生间防水施工#安装工程
- 《税法》(高职版)课件 第六章 其他税种税法(上)
- 钳工工艺技术实训 课件全套 (童永华)项目1-11 入门知识- 冲压模具折装
- 引导学生创新思维的教师说课课件培训
- 山东省淄博市周村区2024年小升初总复习语文测试卷含答案
- 安徽省黄山市歙县2024届小升初语文高频考点检测卷含答案
- 提高学生艺术创作能力的教学方法
- 云南省昆明市黄冈实验学校七年级(上)期末历史试卷(解析版)
- 2023 年下半年全国教师资格考试(高级中学)密押卷(一)-(三)
- 直播带岗方案
- 2024年实现全面小康目标
- 生物奥赛练习题
- 2024年《初级会计实务》刷题必备题库宝典(500题)
- 金属探测器检测记录
- 城市绿化调研报告
- 水库水位库容曲线复核合同
- 新课程理念下的课堂教学教学课件
- 大足石刻市场调研报告
- 第13课《警惕可怕的狂犬病》 课件
- 汽车销售情景演练
评论
0/150
提交评论