您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Android中如何使用CustomFiltControl彈窗控件,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
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:篩選彈框 版權所有轉載請注明出處 */ 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");//標題字體顏色 private int tabTextColor = R.color.fit_item_textcolor;//選項字體顏色 private int tabBgDrawable = R.drawable.item_lable_bg_shape;//選項背景顏色 //當前加載的行數 private int row = -1; private FiltPopuWindow mFiltPopuWindow; public Builder(Context context) { this.context = context; } /** * 設置數據源 * @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); //配置列 第一個參數是起始列標 第二個參數是占幾列 title(篩選類型)應該占滿整行,so -> 總列數 GridLayout.Spec columnSpec = GridLayout.spec(0,columnCount); //配置行 第一個參數是起始行標 起始行+起始列就是一個確定的位置 GridLayout.Spec rowSpec = GridLayout.spec(row); //將Spec傳入GridLayout.LayoutParams并設置寬高為0或者WRAP_CONTENT,必須設置寬高,否則視圖異常 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); //設置當前選中 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并設置寬高為0,必須設置寬高,否則視圖異常 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; } /** * 獲取當前選中tab的 在整個GridLayout的索引 * @return */ private int getIndex(FiltModel model,int titleIndex){ int index = 0; for (int i = 0; i < titleIndex; i++){ //計算當前類型之前的元素所占的個數 title算一個 index += listData.get(i).getTabs().size() + 1; } //加上當前 title下的索引 FiltModel.TableMode tableModel = model.getTab(); index += model.getTabs().indexOf(tableModel) + 1; return index; } /** * 計算行數 * @return */ private int getRowCount(){ int row = 0; for (FiltModel model : listData){ //計算當前類型之前的元素所占的個數 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彈窗控件了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。