上一篇转载了大神写的关于DropDownMenu的博客,其实在要做下拉筛选的时候,带我的师傅就给我推荐过DropDownMenu这个控件,也同时告诉我,学徒阶段最好自己写,虽然肯定写得不好,自己实现过总比一直抄别人的好。所以就自己写了一个下拉筛选,但是由于当时自己还是个学徒,接触Android时间比较短,代码显得很乱,而且很繁琐。当时的基本思路就是设置对应tabbutton数量的boolean值去标识对应tabbutton的check状态……剩下的就不说了,说到这里就能感觉到其繁琐程度……后来又重新想过改进,但由于公司时间比较紧,就一直没做,想改进那会的思路基本和大神的思路差不多,当然还是没有大神的全面,也没想过做成Demo之类的,只是单纯地想要去优化一下,今天专门腾出了一点时间,然后借鉴大神的思路,自己重新写了一个简单的下拉筛选,不足之处,希望各位大神指正。多余的不说,直接上代码:
private LinearLayout mTabButton; private String mTextArray[] = {"性别", "年龄", "身高"}; private RecyclerView mRcv; private DemoAdapter mAdapter; private int current_position = -1; private LinearLayout mLlList; private ArrayList<String>mDatas = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { mTabButton = (LinearLayout)findViewById(R.id.ll_screen); mLlList = (LinearLayout)findViewById(R.id.ll_list); mLlList.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mLlList.setVisibility(View.GONE); closeButton(current_position,false,""); } }); mAdapter = new DemoAdapter(this); mAdapter.setmClick(this); mRcv = (RecyclerView)findViewById(R.id.rv_list); mRcv.setAdapter(mAdapter); mRcv.setLayoutManager(new LinearLayoutManager(this)); for (int i=0;i<mTextArray.length;i++){ addButton(mTextArray[i],i); } } private void addButton(String title, final int i) { final CheckBox tab = new CheckBox(this ); tab.setSingleLine(); tab.setEllipsize(TextUtils.TruncateAt.END); tab.setGravity(Gravity.CENTER); tab.setButtonDrawable(null); tab.setTextSize(TypedValue.COMPLEX_UNIT_SP,15); tab.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f)); ColorStateList color = getResources().getColorStateList(R.color.selector_screen_tabbutton); tab.setTextColor(color); tab.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.selector_pull), null); tab.setText(title); tab.setPadding(10,30,10,30); //添加点击事件 tab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { switchButton(tab,i); current_position = i; } }); mTabButton.addView(tab); } private void closeButton(int position,boolean set_text,String text){ CheckBox box = (CheckBox) mTabButton.getChildAt(position); if (box.isChecked()){ box.setChecked(false); } if (set_text){ box.setText(text); } } private void switchButton(CheckBox button,int position) { for (int i=0;i<mTabButton.getChildCount();i++){ if (position!=i) { closeButton(i,false,""); } } if (button.isChecked()){ showList(position); }else { mLlList.setVisibility(View.GONE); } } private void showList(int position) { CheckBox box = (CheckBox) mTabButton.getChildAt(position); if (current_position!=position) { mDatas.clear(); switch (position){ case 0: { //性别 mDatas.add("不限"); mDatas.add("男"); mDatas.add("女"); }break; case 1: { //年龄 for (int i=18;i<=25;i++){ mDatas.add(i+""); } }break; case 2: { //身高 for (int i=155;i<=190;i++){ mDatas.add(i+""); } }break; } } mAdapter.setmDatas(mDatas,box.getText().toString()); mLlList.setVisibility(View.VISIBLE); }
activity_main的布局文件:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:paddingBottom="50dp" android:textSize="30sp" android:gravity="bottom|center_horizontal" android:text="这个是个content,根据具体情况换成筛选出来的listview" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:background="#FFFFFF" android:id="@+id/ll_screen" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> </LinearLayout> <View android:background="#DBD9D9" android:layout_width="match_parent" android:layout_height="1dp"/> <LinearLayout android:id="@+id/ll_list" android:visibility="gone" android:background="#4A4A4A4A" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_list" android:clickable="true" android:background="#FFFFFF" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout> </LinearLayout> </FrameLayout>其中唯一的一个TextView需要换成筛选结束,容纳筛选结果的contentView,一般是一个RecyclerView。这里为了测试用。
这里只是简单的实现了一下,思路和DropDownMenu的思路基本相同,只是把TabButon换成CheckBox,省去设置TextView字体颜色、drawableRight的代码。(其实就是自己比较懒)