新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Android中如何使用CustomFiltControl彈窗控件
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Android中如何使用CustomFiltControl彈窗控件,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)公司專(zhuān)注于桑日企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,購(gòu)物商城網(wǎng)站建設(shè)。桑日網(wǎng)站建設(shè)公司,為桑日等地區(qū)提供建站服務(wù)。全流程定制制作,專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專(zhuā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)載請(qǐng)注明出處
*/
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 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;//選項(xiàng)字體顏色
private int tabBgDrawable = R.drawable.item_lable_bg_shape;//選項(xiàng)背景顏色
//當(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 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);
//配置列 第一個(gè)參數(shù)是起始列標(biāo) 第二個(gè)參數(shù)是占幾列 title(篩選類(lèi)型)應(yīng)該占滿(mǎn)整行,so -> 總列數(shù)
GridLayout.Spec columnSpec = GridLayout.spec(0,columnCount);
//配置行 第一個(gè)參數(shù)是起始行標(biāo) 起始行+起始列就是一個(gè)確定的位置
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);
//添加選項(xiàng)
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();
//點(diǎn)擊外部消失
}
}
});
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);
}
/**
* 添加選項(xiàng)
* @param model
*/
private void addTabs(final FiltModel model, final int titleIndex){
List 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的 在整個(gè)GridLayout的索引
* @return
*/
private int getIndex(FiltModel model,int titleIndex){
int index = 0;
for (int i = 0; i < titleIndex; i++){
//計(jì)算當(dāng)前類(lèi)型之前的元素所占的個(gè)數(shù) title算一個(gè)
index += listData.get(i).getTabs().size() + 1;
}
//加上當(dāng)前 title下的索引
FiltModel.TableMode tableModel = model.getTab();
index += model.getTabs().indexOf(tableModel) + 1;
return index;
}
/**
* 計(jì)算行數(shù)
* @return
*/
private int getRowCount(){
int row = 0;
for (FiltModel model : listData){
//計(jì)算當(dāng)前類(lèi)型之前的元素所占的個(gè)數(shù) title算一個(gè)
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("沒(méi)有篩選條件");
} 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彈窗控件了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前名稱(chēng):Android中如何使用CustomFiltControl彈窗控件
網(wǎng)頁(yè)地址:http://www.dlmjj.cn/article/gpospg.html


咨詢(xún)
建站咨詢(xún)
