国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

下拉列表—DropDownMenu的使用解析

2019-11-09 18:14:54
字體:
來源:轉載
供稿:網友

DropDownMenu使用解析

下拉篩選列表網上有很多,但是想到自己動手豐衣足食,就自己試試啦!在布局時因為考慮到流暢問題,所以沒用PopupWindow,而是采用基本布局方式完成的。經過篩選我用的是這個 https://github.com/dongjunkun/DropDownMenu.git,想要的可以自己下demo哈!只要核心代碼如下:

Gradle Dependency

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層級圖,可以幫助理解布局:a雖然有點丑,下次改進吧,嘿嘿。。。簡書 3Q竹林
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米林县| 花垣县| 衡阳市| 荥阳市| 龙岩市| 隆林| 晴隆县| 当阳市| 白银市| 嘉善县| 阜康市| 玉树县| 忻州市| 绵竹市| 瑞丽市| 尼玛县| 晋宁县| 浠水县| 灵寿县| 雅安市| 冷水江市| 苗栗县| 聊城市| 阳泉市| 富源县| 乌恰县| 右玉县| 旌德县| 青川县| 上思县| 车致| 新晃| 靖西县| 漯河市| 蕉岭县| 余江县| 中西区| 宜宾市| 扶余县| 剑川县| 抚宁县|