這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Android中如何使用CustomFiltControl彈窗控件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)公司專注于桑日企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,購物商城網(wǎng)站建設(shè)。桑日網(wǎng)站建設(shè)公司,為桑日等地區(qū)提供建站服務(wù)。全流程定制制作,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
package com.zzq.mack.customfiltcontrol; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.GridLayout; import android.text.TextUtils; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; import com.zzq.mack.customfiltcontrol.model.FiltModel; import java.util.List; /** * Author:xqt * Email:zzq1573@gmail.com * Date:2018/3/31 0031 11:24 * Description:篩選彈框 版權(quán)所有轉(zhuǎn)載請注明出處 */ public class FiltPopuWindow extends PopupWindow{ public FiltPopuWindow(Context context,View view){ //這里可以修改popupwindow的寬高 super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); setContentView(view); initViews(); } private void initViews() { setAnimationStyle(R.style.popwin_anim_style); //setBackgroundDrawable(new ColorDrawable(0x00000000)); setFocusable(true); setOutsideTouchable(true); } public static class Builder { private Context context; private List<FiltModel> listData; private int columnCount; private GridLayout rootGridLayout; private LinearLayout contextll; //背景顏色 private int colorBg = Color.parseColor("#F8F8F8"); private int titleTextSize = 14;//SP private int tabTextSize = 14;//SP private int titleTextColor = Color.parseColor("#333333");//標(biāo)題字體顏色 private int tabTextColor = R.color.fit_item_textcolor;//選項字體顏色 private int tabBgDrawable = R.drawable.item_lable_bg_shape;//選項背景顏色 //當(dāng)前加載的行數(shù) private int row = -1; private FiltPopuWindow mFiltPopuWindow; public Builder(Context context) { this.context = context; } /** * 設(shè)置數(shù)據(jù)源 * @return */ public Builder setDataSource(List<FiltModel> listData) { this.listData = listData; return this; } public Builder setColumnCount(int columnCount){ this.columnCount = columnCount; return this; } public Builder setColorBg(int color){ colorBg = context.getResources().getColor(color); return this; } public Builder setTitleTextSize(int titleTextSize) { this.titleTextSize = titleTextSize; return this; } public Builder setTabTextSize(int tabTextSize) { this.tabTextSize = tabTextSize; return this; } public Builder setTitleTextColor(int titleTextColor) { this.titleTextColor = titleTextColor; return this; } public Builder setTabTextColor(int tabTextColor) { this.tabTextColor = tabTextColor; return this; } public Builder setTabBgDrawable(int tabBgDrawable) { this.tabBgDrawable = tabBgDrawable; return this; } public Builder build(){ newItemLayout(getRowCount(),columnCount); for (int i = 0; i < listData.size(); i++){ ++row; TextView view = new TextView(context); view.setText(listData.get(i).getTypeName()); view.setTextColor(titleTextColor); view.setTextSize(titleTextSize); //配置列 第一個參數(shù)是起始列標(biāo) 第二個參數(shù)是占幾列 title(篩選類型)應(yīng)該占滿整行,so -> 總列數(shù) GridLayout.Spec columnSpec = GridLayout.spec(0,columnCount); //配置行 第一個參數(shù)是起始行標(biāo) 起始行+起始列就是一個確定的位置 GridLayout.Spec rowSpec = GridLayout.spec(row); //將Spec傳入GridLayout.LayoutParams并設(shè)置寬高為0或者WRAP_CONTENT,必須設(shè)置寬高,否則視圖異常 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(rowSpec, columnSpec); lp.width = GridLayout.LayoutParams.WRAP_CONTENT; lp.height = GridLayout.LayoutParams.WRAP_CONTENT; lp.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); lp.bottomMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_8); rootGridLayout.addView(view,lp); //添加選項 addTabs(listData.get(i),i); } return this; } private void newItemLayout(int rowCount,int columnCount){ contextll = new LinearLayout(context); contextll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); contextll.setBackgroundColor(context.getResources().getColor(R.color.color_33000000)); contextll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mFiltPopuWindow != null){ mFiltPopuWindow.dismiss(); //點擊外部消失 } } }); rootGridLayout = new GridLayout(context); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); rootGridLayout.setOrientation(GridLayout.HORIZONTAL); rootGridLayout.setRowCount(rowCount); rootGridLayout.setColumnCount(columnCount); rootGridLayout.setBackgroundColor(colorBg); rootGridLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); int pandd = context.getResources().getDimensionPixelSize(R.dimen.dp_10); lp.weight = 1; rootGridLayout.setPadding(pandd,pandd,pandd,pandd); contextll.addView(rootGridLayout,lp); } /** * 添加選項 * @param model */ private void addTabs(final FiltModel model, final int titleIndex){ List<FiltModel.TableMode> tabs = model.getTabs(); for (int i = 0; i < tabs.size(); i++){ if (i % columnCount == 0){ row ++; } final FiltModel.TableMode tab = tabs.get(i); final TextView lable = new TextView(context); lable.setTextColor(context.getResources().getColorStateList(tabTextColor)); lable.setBackgroundDrawable(context.getResources().getDrawable(tabBgDrawable)); lable.setSingleLine(true); lable.setGravity(Gravity.CENTER); lable.setEllipsize(TextUtils.TruncateAt.MIDDLE); //這里可以自行修改tab框框的大小 int panddT = context.getResources().getDimensionPixelSize(R.dimen.dp_2); int panddL = context.getResources().getDimensionPixelSize(R.dimen.dp_8); lable.setPadding(panddL,panddT,panddL,panddT); lable.setTextSize(tabTextSize); rootGridLayout.addView(lable,getItemLayoutParams(i,row)); lable.setText(tab.name); if (tabs.get(i) == model.getTab()){ lable.setSelected(true); } lable.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //lable.setSelected(true); if (tab != model.getTab()){ //清空上次選中 rootGridLayout.getChildAt(getIndex(model,titleIndex)).setSelected(false); //設(shè)置當(dāng)前選中 model.setTab(tab); lable.setSelected(true); } } }); } } private GridLayout.LayoutParams getItemLayoutParams(int i,int row){ //使用Spec定義子控件的位置和比重 GridLayout.Spec rowSpec = GridLayout.spec(row,1f); GridLayout.Spec columnSpec = GridLayout.spec(i%columnCount,1f); //將Spec傳入GridLayout.LayoutParams并設(shè)置寬高為0,必須設(shè)置寬高,否則視圖異常 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(rowSpec, columnSpec); lp.width = 0; lp.height = GridLayout.LayoutParams.WRAP_CONTENT; lp.bottomMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_8); if(i % columnCount == 0) {//最左邊 lp.leftMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_10); lp.rightMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_20); }else if((i + 1) % columnCount == 0){//最右邊 lp.rightMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_10); }else {//中間 lp.rightMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_20); } return lp; } /** * 獲取當(dāng)前選中tab的 在整個GridLayout的索引 * @return */ private int getIndex(FiltModel model,int titleIndex){ int index = 0; for (int i = 0; i < titleIndex; i++){ //計算當(dāng)前類型之前的元素所占的個數(shù) title算一個 index += listData.get(i).getTabs().size() + 1; } //加上當(dāng)前 title下的索引 FiltModel.TableMode tableModel = model.getTab(); index += model.getTabs().indexOf(tableModel) + 1; return index; } /** * 計算行數(shù) * @return */ private int getRowCount(){ int row = 0; for (FiltModel model : listData){ //計算當(dāng)前類型之前的元素所占的個數(shù) title算一個 row ++; int size = model.getTabs().size(); row += (size / columnCount) + (size % columnCount > 0 ? 1 : 0) ; } return row; } public FiltPopuWindow createPop(){ if (listData == null || listData.size() == 0){ try { throw new Exception("沒有篩選條件"); } catch (Exception e) { Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show(); e.printStackTrace(); } return null; } mFiltPopuWindow = new FiltPopuWindow(context,contextll); return mFiltPopuWindow; } } }
上述就是小編為大家分享的Android中如何使用CustomFiltControl彈窗控件了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前題目:Android中如何使用CustomFiltControl彈窗控件
網(wǎng)頁地址:http://www.rwnh.cn/article36/gpospg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、定制網(wǎng)站、品牌網(wǎng)站制作、云服務(wù)器、微信公眾號、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)