allPRojects { repositories { ... maven { url "https://jitpack.io" } }}dependencies { compile 'com.github.dongjunkun:DropDownMenu:1.0.3'}使用
添加DropDownMenu 到你的布局文件,如下
<com.yyydjk.library.DropDownMenu android:id="@+id/dropDownMenu" android:layout_width="match_parent" android:layout_height="match_parent" app:ddmenuTextSize="13sp" //tab字體大小 app:ddtextUnselectedColor="@color/drop_down_unselected" //tab未選中顏色 app:ddtextSelectedColor="@color/drop_down_selected" //tab選中顏色 app:dddividerColor="@color/gray" //分割線顏色 app:ddunderlineColor="@color/gray" //下劃線顏色 app:ddmenuSelectedIcon="@mipmap/drop_down_selected_icon" //tab選中狀態圖標 app:ddmenuUnselectedIcon="@mipmap/drop_down_unselected_icon"//tab未選中狀態圖標 app:ddmaskColor="@color/mask_color" //遮罩顏色,一般是半透明 app:ddmenuBackgroundColor="@color/white" //tab 背景顏色 ... />我們只需要在java代碼中調用下面的代碼
//tabs 所有標題,popupListViews 所有菜單,contentView 內容mDropDownMenu.setDropDownMenu(tabs, popupListViews, contentView);mDropDownMenu.setTabText(position == 0 ? headers[2] : sexs[position]); //item被選擇時調用mDropDownMenu.closeMenu(); //默認自帶動畫效果貼出源碼,方便更好的理解參數:setDropDownMenu(List<String> tabTexts,List<View> popupViews,View contentView)ScreenShot

效果不錯吧,嘿嘿,這是github上的效果,喔并沒有做改動/(^o^)/~那么我們需要做什么呢?其實就是圍繞mDropDownMenu.setDropDownMenu(tabs, popupListViews, contentView);參數展開的: 0.準備最上邊tabs數據; 1.準備PopupView集合,及對應的adapter,包括填充條目item布局樣式; 2.contentView的布局及數據變化加載需要自己設置; 3.可通過listView的setOnItemClickListener獲得需要自己獲取所選的條目position;adapter這里就不絮叨了,我們直接看Activity中代碼:public class DropDownMenuActivity extends AppCompatActivity { @InjectView(R.id.dropDownMenu) DropDownMenu mDropDownMenu; private String headers[] = {"城市", "年齡", "性別", "星座"}; private List<View> popupViews = new ArrayList<>(); private GirdDropDownAdapter cityAdapter; private ListDropDownAdapter ageAdapter; private ListDropDownAdapter sexAdapter; private ConstellationAdapter constellationAdapter; private String citys[] = {"不限", "武漢", "北京", "上海", "成都", "廣州", "深圳", "重慶", "天津", "西安", "南京", "杭州"}; private String ages[] = {"不限", "18歲以下", "18-22歲", "23-26歲", "27-35歲", "35歲以上"}; private String sexs[] = {"不限", "男", "女"}; private String constellations[] = {"不限", "白羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "雙魚座"}; private int constellationPosition = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dropdowmenu); ButterKnife.inject(this); initView(); } private void initView() { //init city menu final ListView cityView = new ListView(this); //gird:準備 cityAdapter = new GirdDropDownAdapter(this, Arrays.asList(citys)); cityView.setDividerHeight(0); cityView.setAdapter(cityAdapter); //init age menu final ListView ageView = new ListView(this); ageView.setDividerHeight(0); ageAdapter = new ListDropDownAdapter(this, Arrays.asList(ages)); ageView.setAdapter(ageAdapter); //init sex menu final ListView sexView = new ListView(this); sexView.setDividerHeight(0); sexAdapter = new ListDropDownAdapter(this, Arrays.asList(sexs)); sexView.setAdapter(sexAdapter); //init constellation final View constellationView = getLayoutInflater().inflate(R.layout.custom_layout, null); GridView constellation = ButterKnife.findById(constellationView, R.id.constellation); constellationAdapter = new ConstellationAdapter(this, Arrays.asList(constellations)); constellation.setAdapter(constellationAdapter); TextView ok = ButterKnife.findById(constellationView, R.id.ok); ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //其內部已經設置了記錄當前tab位置的參數,該參數會隨tab被點擊而改變,所以這里直接設置tab值即可 //此處若想獲得constellations第一個值“不限”,可修改constellationPosition初始值為-1,且這里代碼改為constellationPosition == -1) mDropDownMenu.setTabText((constellationPosition == 0) ? headers[3] : constellations[constellationPosition]); mDropDownMenu.closeMenu();// changeContentView(); //在這里可以請求獲得經篩選后要顯示的內容 } }); //add item click event cityView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { cityAdapter.setCheckItem(position); mDropDownMenu.setTabText(position == 0 ? headers[0] : citys[position]); mDropDownMenu.closeMenu(); } }); ageView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ageAdapter.setCheckItem(position); mDropDownMenu.setTabText(position == 0 ? headers[1] : ages[position]); mDropDownMenu.closeMenu(); } }); sexView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { sexAdapter.setCheckItem(position); mDropDownMenu.setTabText(position == 0 ? headers[2] : sexs[position]); mDropDownMenu.closeMenu(); } }); constellation.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { constellationAdapter.setCheckItem(position); constellationPosition = position; } }); //init popupViews popupViews.add(cityView); popupViews.add(ageView); popupViews.add(sexView); popupViews.add(constellationView); //init context view TextView contentView = new TextView(this); contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); contentView.setText("內容顯示區域"); contentView.setGravity(Gravity.CENTER); contentView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); //init dropdownview mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView); } @Override public void onBackPressed() { //退出activity前關閉菜單 if (mDropDownMenu.isShowing()) { mDropDownMenu.closeMenu(); } else { super.onBackPressed(); } }}主要地方已經做了注釋,希望能幫到大家吧!下邊是我自己畫的DropDownMenu層級圖,可以幫助理解布局:
雖然有點丑,下次改進吧,嘿嘿。。。簡書 3Q竹林
新聞熱點
疑難解答