Android implements universal filter bar

Today, I'm going to write the implementation of a general filter bar. It's also because the filter bar was used in many places in the previous project. Therefore, in order to make it easier to use later, I simply encapsulated it. Without more nonsense, let's take a look at the effect picture:

Many apps use this filter column, and I believe you are no stranger

1、 Demand analysis

After seeing the filter bar, first consider the layout. When I first came into contact with Android, I might see the idea of this layout: the filter bar is a horizontal layout of LinearLayout, and then divided into three parts, each accounting for 1. Then put a textview and ImageView in each part, and then listen to the click events of each part, Handle the color, text and direction of textview and ImageView. Indeed, this can achieve the function, but it is very troublesome to write. You need to manually deal with the changes of textview and ImageView. There may be many filter columns, which will be disordered, and it will take a lot of time to find and modify bugs. There may be a small place written backwards, and the effect will be disordered

So think about it. Is there a better layout design for the filter column? Flipped through the available controls and found Check@R_ 391_ 2419 @ actually, it's easy to use here (maybe for Check@R_391_2419 @How to say it's easy to use, because Check@R_ 391_ 2419 @ there are two states (checked and unchecked) that can just meet the requirements. When selected, the font color will be changed to blue, and the icon will change direction and color. When unchecked, it will be reset to the starting style, and the style change can be easily completed by setting the selector. Just manage Check@R_ 391_ 2419 @ is OK

For pop-up boxes, it's easy to customize a popwindow. However, it should be noted that when the pop-up box pops up, the transparency of the background color needs to be changed, and the transparency of the background color needs to be restored after the pop-up box disappears, so some special processing needs to be done. Here we plan to monitor the changes of popwindow to complete the relevant processing

2、 Code implementation and optimization

According to the above analysis, the layout design is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.junweiliu.filterdemo.MainActivity">

 <!--筛选栏-->
 <LinearLayout
  android:id="@+id/ll_stay_visit_selsect"
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:background="@color/white"
  android:gravity="center_vertical"
  android:orientation="horizontal">
 <!--筛选地点-->
 <LinearLayout
  android:id="@+id/ll_place_tab"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:gravity="center">
  <Check@R_391_2419@
   android:id="@+id/cb_place"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@null"
   android:button="@null"
   android:drawablePadding="10dp"
   android:drawableRight="@drawable/seletor_stock_arrow"
   android:gravity="center"
   android:text="地点"
   android:textColor="@drawable/selector_text_stock"
   android:textSize="15sp"/>
 </LinearLayout>
 <!--筛选类型-->
 <LinearLayout
  android:id="@+id/ll_type"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:gravity="center">
  <Check@R_391_2419@
   android:id="@+id/cb_type"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@null"
   android:button="@null"
   android:drawablePadding="10dp"
   android:drawableRight="@drawable/seletor_stock_arrow"
   android:gravity="center"
   android:text="条件"
   android:textColor="@drawable/selector_text_stock"
   android:textSize="15sp"/>
 </LinearLayout>
 <!--筛选时间-->
 <LinearLayout
  android:id="@+id/ll_time"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:gravity="center">
  <Check@R_391_2419@
   android:id="@+id/cb_time"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@null"
   android:button="@null"
   android:drawablePadding="10dp"
   android:drawableRight="@drawable/seletor_stock_arrow"
   android:gravity="center"
   android:text="时间"
   android:textColor="@drawable/selector_text_stock"
   android:textSize="15sp"/>
 </LinearLayout>
 </LinearLayout>
</RelativeLayout>

Three parts are set, and each part uses Check@R_ 391_ 2419@, Check@R_ 391_ Two selectors are set in 2419 @ which are Android: drawableright = "@ drawable / seletor_ stock_ Arrow "and Android: textcolor =" @ drawable / selector_ text_ stock”

The settings are as follows:

Set seletor for arrow style_ stock_ arrow:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <!--选中的箭头样式-->
 <item android:drawable="@mipmap/arrow_up_blue" android:state_checked="true" />
 <!--未选中的箭头样式-->
 <item android:drawable="@mipmap/arrow_down_black" android:state_checked="false" />
</selector>

Set text style selector_ text_ stock:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:color="@color/gray" android:state_checked="false" />
 <item android:color="@color/colorPrimary" android:state_checked="true" />
</selector>

The implementation is very simple. Let's take a look at the effect:

The effect is good. The next step is to manage each in the code Check@R_ 391_ The status of 2419 @ is OK. Before doing this part, first deal with popwindow, and then cooperate Check@R_ 391_ 2419 @ to use

Popwindow is very simple. You can inherit popupwindow, customize some styles and layouts, and put the code directly:

CommonFilterPop:

package com.example.junweiliu.filterdemo.pop;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.example.junweiliu.filterdemo.R;
import com.example.junweiliu.filterdemo.adapter.CommPopAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by junweiliu on 16/11/7.
 */
public class CommonFilterPop extends PopupWindow {
 /**
 * 布局填充器
 */
 private LayoutInflater mInflater;
 /**
 * 上下文
 */
 private Context mContext;
 /**
 * 只显示String类型的数据
 */
 private List<String> mDatas = new ArrayList<>();
 /**
 * pop整体View
 */
 private View popupView;
 /**
 * 选择条件的list
 */
 private ListView contentLv;
 /**
 * 筛选条件选择后的回调
 */
 AdapterView.OnItemClickListener itemClickListener;
 /**
 * 适配器
 */
 CommPopAdapter adapter;

 /**
 * 构造函数
 *
 * @param context
 * @param mDatas
 */
 public CommonFilterPop(Context context,List<String> mDatas) {
 this.mInflater = LayoutInflater.from(context);
 this.mContext = context;
 this.mDatas = (mDatas);
 popupView = mInflater.inflate(
  R.layout.common_popup_list_dialog,null);
 //设置View
 this.setContentView(popupView);
 //设置弹出窗体的宽高
 this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
 this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
 //初始化控件
 initPopView();
 this.setFocusable(true);
 this.setTouchable(true);
 this.setOutsideTouchable(true);
 this.setBackgroundDrawable(new BitmapDrawable());
 //需要动画效果的话可以设置
 //this.setAnimationStyle(R.style.PopupWindowAnimation);
 this.update();
 }

 private void initPopView() {
 contentLv = (ListView) popupView.findViewById(R.id.lv_pop);
 adapter = new CommPopAdapter(mContext,mDatas);
 contentLv.setAdapter(adapter);
 }

 /**
 * listview点击事件
 *
 * @param itemClickListener
 */
 public void setOnItemSelectedListener(AdapterView.OnItemClickListener itemClickListener) {
 if (null != itemClickListener && null != contentLv) {
  contentLv.setOnItemClickListener(itemClickListener);
 }
 }
}

Adapter commpopadapter:

package com.example.junweiliu.filterdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.junweiliu.filterdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by junweiliu on 16/11/7.
 */
public class CommPopAdapter extends BaseAdapter {
 /**
 * 筛选条件数据
 */
 private List<String> mDatas = new ArrayList<>();
 /**
 * 布局加载器
 */
 private LayoutInflater mInflater;

 public CommPopAdapter(Context context,List<String> mDatas) {
 this.mDatas = mDatas;
 mInflater = LayoutInflater.from(context);
 }

 @Override
 public int getCount() {
 return mDatas.size();
 }

 @Override
 public Object getItem(int i) {
 return mDatas.get(i);
 }

 @Override
 public long getItemId(int i) {
 return i;
 }

 @Override
 public View getView(int i,View convertView,ViewGroup viewGroup) {
 ViewHolder viewHolder = null;
 if (convertView == null) {
  viewHolder = new ViewHolder();
  convertView = mInflater.inflate(
   R.layout.common_popup_list_item,null);
  viewHolder.mTitleTv = (TextView) convertView.findViewById(R.id.tv_common_listpop_title);
  convertView.setTag(viewHolder);
 } else {
  viewHolder = (ViewHolder) convertView.getTag();
 }
 viewHolder.mTitleTv.setText(mDatas.get(i));
 return convertView;
 }

 /**
 * vh
 */
 public class ViewHolder {
 /**
  * 筛选项文字tv
  */
 TextView mTitleTv;
 }

}

Related XML files:

Pop layout file common_ popup_ list_ dialog:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@android:color/transparent"
  android:gravity="bottom">

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@android:color/transparent"
  android:orientation="vertical">

 <View
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="@color/divider_line"/>

 <ListView
  android:id="@+id/lv_pop"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"
  android:divider="@null"/>

 <View
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="@color/divider_line"/>

 </LinearLayout>
</LinearLayout>

Layout common in adapter_ popup_ list_ item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/white"
  android:orientation="vertical"
>

 <RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/white"
 >
 <TextView
  android:id="@+id/tv_common_listpop_title"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:gravity="center"
  android:padding="15dip"
  android:text="@string/app_name"
 />
 </RelativeLayout>

 <View
  android:layout_width="match_parent"
  android:layout_height="0.5dip"
  android:background="@color/divider_line"
  android:scaleType="fitXY"/>

</LinearLayout>

The notes are complete and simple, so I won't explain more

The next step is to call popwindow. It's better to call popwinow there. Because a general filter column needs to be written, these public parts are extracted and put into baseactivity. The required activities directly inherit baseactivity for later use

Create a baseactivity and process popwindow in it. The code is as follows

BaseActivity:

package com.example.junweiliu.filterdemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.PopupWindow;

import com.example.junweiliu.filterdemo.pop.CommonFilterPop;

import java.util.List;

/**
 * Created by junweiliu on 16/11/7.
 */
public class BaseActivity extends AppCompatActivity {

 /**
 * 筛选pop
 */
 private CommonFilterPop mPopupWindow;
 /**
 * 当前上下文实例
 */
 protected Activity activity;

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 this.activity = this;
 }

 /**
 * 列表选择popupWindow
 *
 * @param parentView 父View
 * @param itemTexts  列表项文本集合
 * @param itemClickListener 列表项点击事件
 */
 public void showFilterPopupWindow(View parentView,List<String> itemTexts,AdapterView.OnItemClickListener itemClickListener,CustomerDismissListener dismissListener) {
 showFilterPopupWindow(parentView,itemTexts,itemClickListener,dismissListener,0);
 }

 /**
 * 列表选择popupWindow
 *
 * @param parentView 父View
 * @param itemTexts  列表项文本集合
 * @param itemClickListener 列表项点击事件
 * @param alpha  背景透明度
 */
 public void showFilterPopupWindow(View parentView,CustomerDismissListener dismissListener,float alpha) {

 // 判断当前是否显示
 if (mPopupWindow != null && mPopupWindow.isShowing()) {
  mPopupWindow.dismiss();
  mPopupWindow = null;
 }
 mPopupWindow = new CommonFilterPop(activity,itemTexts);
 mPopupWindow.setOnDismissListener(dismissListener);
 // 绑定筛选点击事件
 mPopupWindow.setOnItemSelectedListener(itemClickListener);
 // 如果透明度设置为0的话,则认设置为0.6f
 if (0 == alpha) {
  alpha = 0.6f;
 }
 // 设置背景透明度
 WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
 lp.alpha = alpha;
 activity.getWindow().setAttributes(lp);
 // 显示pop
 mPopupWindow.showAsDropDown(parentView);

 }

 /**
 * 自定义OnDismissListener
 */
 public class CustomerDismissListener implements PopupWindow.OnDismissListener {
 @Override
 public void onDismiss() {
  // 当pop消失的时候,重置背景色透明度
  WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
  lp.alpha = 1.0f;
  activity.getWindow().setAttributes(lp);
 }
 }

 /**
 * 隐藏pop
 */
 public void hidePopListView() {
 // 判断当前是否显示,如果显示则dismiss
 if (mPopupWindow != null && mPopupWindow.isShowing()) {
  mPopupWindow.dismiss();
  mPopupWindow = null;
 }
 }
}

The display disappearance of popwindow is handled in baseactivity. When popwindow is created, the background transparency is changed, and the customerdississlistener is rewritten to restore the background transparency when popwindow disappears

After finishing popwindow, let's combine Check@R_ 391_ 2419 @ let's use it. It should be like this:

// cb1操作
cb1.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton,boolean b) {
  // 将其他的cb设置为未选中,将自己设置为选中
  cb1.setChecked(true);
  cb2.setChecked(false);
  cb3.setChecked(false);
  showFilterPopupWindow(showView,showMes1,new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView,View view,int i,long l) {
    cb1.setText(showMes1.get(position));
    }
   },new CustomerDismissListener(){
    @Override
    public void onDismiss() {
    super.onDismiss();
    // 消失的时候,需要将当前的cb设置为未选中
    cb1.setChecked(false);
    }
   });
  }
 });

 // cb2操作
 cb2.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton,将自己设置为选中
  cb2.setChecked(true);
  cb1.setChecked(false);
  cb3.setChecked(false);
  showFilterPopupWindow(showView,showMes2,long l) {
    cb2.setText(showMes1.get(position));
    }
   },需要将当前的cb设置为未选中
    cb2.setChecked(false);
    }
   });
  }
 });
 // cb3操作
 cb3.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton,将自己设置为选中
  cb3.setChecked(true);
  cb1.setChecked(false);
  cb2.setChecked(false);
  showFilterPopupWindow(showView,showMes3,long l) {
    cb3.setText(showMes3.get(position));
    }
   },需要将当前的cb设置为未选中
    cb3.setChecked(false);
    }
   });
  }
 });

There are many repeated parts. For example, in the ondississ method, the current CB is set to the unselected state, and there is too much code redundancy in initializing the selected state. So let's encapsulate a method in the baseactivity

 /**
 * Tab筛选栏切换
 *
 * @param isChecked  选中状态
 * @param showView  展示pop的跟布局
 * @param showMes  展示选择的数据
 * @param itemClickListener 点击回调
 * @param tabs  所有的cb(需要几个输入几个就可以,cb1,cb2....)
 */
 public void filterTabToggle(boolean isChecked,View showView,List<String> showMes,final Check@R_391_2419@... tabs) {
 if (isChecked) {
  if (tabs.length <= 0) {
  return;
  }
  // 第一个check@R_391_2419@为当前点击选中的cb,其他cb进行setChecked(false);
  for (int i = 1; i < tabs.length; i++) {
  tabs[i].setChecked(false);
  }

  showFilterPopupWindow(showView,showMes,new CustomerDismissListener() {
  @Override
  public void onDismiss() {
   super.onDismiss();
   // 当pop消失时对第一个cb进行.setChecked(false)操作
   tabs[0].setChecked(false);
  }
  });
 } else {
  // 关闭check@R_391_2419@时直接隐藏popuwindow
  hidePopListView();
 }
}

In consideration of generality, the variable length parameter is used... List has been used before, but it is not easy to use. Each time you use it, you need to create a list and then assemble a list, which is very troublesome. In this way, you only need to put the selected CB in the first place of the variable length parameter, and others need to set the unselected CB later. Use the following:

// cb操作
cb1.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton,boolean isChecked) {
  filterTabToggle(isChecked,showView,new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapterView,int position,long l) {
   hidePopListView();
   cb1.setText(showMes1.get(position));
   }
  },cb2,cb3);
  }
 });

It's much more comfortable to use, but then I found an embarrassing problem. That is, the showmes that needs to be passed in needs to be a list < string >. Sometimes the data obtained is not a list of string types, but may be various types of list < bean >. What should I do? There are many solutions, such as:

 /**
 * 展示时间的数据
 */
 List<TimeBean> mTimes = new ArrayList<>();
 /**
 * 展示的时间str集合
 */
 List<String> mTimeStr = new ArrayList<>();

 /**
 * 筛选数据
 */
 public void forMatData(){
 // 初始化时间
 TimeBean timeBean1 = new TimeBean("1天内","去玩");
 TimeBean timeBean2 = new TimeBean("3天内","去购物");
 TimeBean timeBean3 = new TimeBean("10天内","去旅行");
 TimeBean timeBean4 = new TimeBean("30天内","去赚钱");
 mTimes.add(timeBean1);
 mTimes.add(timeBean2);
 mTimes.add(timeBean3);
 mTimes.add(timeBean4);
 // 获取时间中可用于筛选的数据
 for (TimeBean bean : mTimes) {
  mTimeStr.add(bean.getTimestr());
 }
 }

It's not too troublesome to extract the data showmes used from the data source, but is there a better way? After all, it's time-consuming to extract these data from different data sources every time. After considering these, think about it and come up with a good method to use wildcards?, Firstly, a public interface basefilter is proposed, in which a unified method for obtaining filter fields is specified as follows:

package com.example.junweiliu.filterdemo.bean;

/**
 * Created by junweiliu on 16/11/22.
 */
public interface BaseFilter {

 /**
 * 获取筛选的
 * @return
 */
 public String getFilterstr();
}

Then let the bean that needs to use the filtering function implement the getfilterstr method in this interface and implement it. Then modify the previous filtertabtoggle method as follows:

Bean:

package com.example.junweiliu.filterdemo.bean;

/**
 * Created by junweiliu on 16/11/22.
 */
public class TimeBean implements BaseFilter{
 /**
 * 时间str
 */
 String timeStr;
 /**
 * 时间事件
 */
 String timeEvent;

 public TimeBean(String timeStr,String timeEvent) {
 this.timeStr = timeStr;
 this.timeEvent = timeEvent;
 }

 public String getTimestr() {
 return timeStr;
 }

 public void setTimeStr(String timeStr) {
 this.timeStr = timeStr;
 }

 public String getTimeEvent() {
 return timeEvent;
 }

 public void setTimeEvent(String timeEvent) {
 this.timeEvent = timeEvent;
 }

 @Override
 public String getFilterstr() {
 return timeStr;
 }
}

Filtertabtoggle method:

 /**
 * Tab筛选栏切换
 *
 * @param isChecked  选中状态
 * @param showView  展示pop的跟布局
 * @param showMes  展示选择的数据源
 * @param itemClickListener 点击回调
 * @param tabs  所有的cb(需要几个输入几个就可以,cb2....)
 */
 public void filterTabToggleT(boolean isChecked,List<? extends BaseFilter> showMes,其他cb进行setChecked(false);
  for (int i = 1; i < tabs.length; i++) {
  tabs[i].setChecked(false);
  }
  // 从数据源中提取出展示的筛选条件
  List<String> showStr = new ArrayList<>();
  for (BaseFilter baseFilter : showMes) {
  showStr.add(baseFilter.getFilterstr());
  }
  showFilterPopupWindow(showView,showStr,new CustomerDismissListener() {
  @Override
  public void onDismiss() {
   super.onDismiss();
   // 当pop消失时对第一个cb进行.setChecked(false)操作
   tabs[0].setChecked(false);
  }
  });
 } else {
  // 关闭check@R_391_2419@时直接隐藏popuwindow
  hidePopListView();
 }
 }

In this way, when using, you can directly transfer the data of list < bean > type. In this way, the whole general filter column can be realized. Of course, it needs to be modified according to different needs, but the general idea and implementation are like this. Then you can use it like this:

// 选择时间cb
 mTimeCb.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton,boolean isChecked) {
  filterTabToggleT(isChecked,mTimeAll,mTimes,long l) {
   hidePopListView();
   mTimeCb.setText(mTimeStr.get(position));
   }
  },mTimeCb,mPlaceCb,mTypeCb);
  }
 });

Finally, post the complete mainactivity and baseactivity

MainActivity:

package com.example.junweiliu.filterdemo;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Check@R_391_2419@;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.example.junweiliu.filterdemo.bean.PlaceBean;
import com.example.junweiliu.filterdemo.bean.TimeBean;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
 /**
 * 展示城市的数据源
 */
 List<PlaceBean> mPopBeens = new ArrayList<>();
 /**
 * 展示类型的数据
 */
 List<String> mTypes = new ArrayList<>();
 /**
 * 展示时间的数据
 */
 List<TimeBean> mTimes = new ArrayList<>();
 /**
 * 展示的时间str集合
 */
 List<String> mTimeStr = new ArrayList<>();
 /**
 * 筛选地区整体
 */
 LinearLayout mPlaceAll;
 /**
 * 筛选城市cb
 */
 Check@R_391_2419@ mPlaceCb;
 /**
 * 筛选类型整体
 */
 LinearLayout mTypeAll;
 /**
 * 筛选类型整体
 */
 Check@R_391_2419@ mTypeCb;
 /**
 * 筛选时间整体
 */
 LinearLayout mTimeAll;
 /**
 * 筛选时间整体
 */
 Check@R_391_2419@ mTimeCb;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initDate();
 initView();
 }

 /**
 * 初始化数据
 */
 private void initDate() {
 // 初始化城市
 PlaceBean placeBean1 = new PlaceBean("天津");
 PlaceBean placeBean2 = new PlaceBean("北京");
 PlaceBean placeBean3 = new PlaceBean("上海");
 PlaceBean placeBean4 = new PlaceBean("深圳");
 PlaceBean placeBean5 = new PlaceBean("四川");
 PlaceBean placeBean6 = new PlaceBean("杭州");
 PlaceBean placeBean7 = new PlaceBean("苏州");
 mPopBeens.add(placeBean1);
 mPopBeens.add(placeBean2);
 mPopBeens.add(placeBean3);
 mPopBeens.add(placeBean4);
 mPopBeens.add(placeBean5);
 mPopBeens.add(placeBean6);
 mPopBeens.add(placeBean7);
 // 初始化类型
 mTypes.add("美食");
 mTypes.add("电影");
 mTypes.add("化妆品");
 mTypes.add("衣服");
 mTypes.add("玩具");
 mTypes.add("电器");
 mTypes.add("装饰");
 mTypes.add("超市");
 // 初始化时间
 TimeBean timeBean1 = new TimeBean("1天内","去赚钱");
 mTimes.add(timeBean1);
 mTimes.add(timeBean2);
 mTimes.add(timeBean3);
 mTimes.add(timeBean4);
 // 获取时间中可用于筛选的数据
 for (TimeBean bean : mTimes) {
  mTimeStr.add(bean.getTimestr());
 }
 }

 /**
 * 初始化控件
 */
 private void initView() {
 mPlaceAll = (LinearLayout) findViewById(R.id.ll_place_tab);
 mPlaceCb = (Check@R_391_2419@) findViewById(R.id.cb_place);
 mTypeAll = (LinearLayout) findViewById(R.id.ll_type);
 mTypeCb = (Check@R_391_2419@) findViewById(R.id.cb_type);
 mTimeAll = (LinearLayout) findViewById(R.id.ll_time);
 mTimeCb = (Check@R_391_2419@) findViewById(R.id.cb_time);
 // 点击选择城市整体
 mPlaceAll.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
  if (mPlaceCb.isChecked())
   mPlaceCb.setChecked(false);
  else
   mPlaceCb.setChecked(true);
  }
 });
 // 点击选择类型整体
 mTypeAll.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
  if (mTypeCb.isChecked())
   mTypeCb.setChecked(false);
  else
   mTypeCb.setChecked(true);
  }
 });
 // 点击选择时间整体
 mTimeAll.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
  if (mTimeCb.isChecked())
   mTimeCb.setChecked(false);
  else
   mTimeCb.setChecked(true);
  }
 });

 // 选择城市cb
 mPlaceCb.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton,mPlaceAll,mPopBeens,long l) {
   hidePopListView();
   mPlaceCb.setText(mPopBeens.get(position).getFilterstr());
   }
  },mTypeCb,mTimeCb);
  }
 });

 // 选择类型cb
 mTypeCb.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton,mTypeAll,mTypes,long l) {
   hidePopListView();
   mTypeCb.setText(mTypes.get(position));
   }
  },mTimeCb);
  }
 });
 // 选择时间cb
 mTimeCb.setOnCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton,mTimeStr,mTypeCb);
  }
 });

 }
}

BaseActivity:

package com.example.junweiliu.filterdemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Check@R_391_2419@;
import android.widget.PopupWindow;

import com.example.junweiliu.filterdemo.bean.BaseFilter;
import com.example.junweiliu.filterdemo.pop.CommonFilterPop;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by junweiliu on 16/11/7.
 */
public class BaseActivity extends AppCompatActivity {

 /**
 * 筛选pop
 */
 private CommonFilterPop mPopupWindow;
 /**
 * 当前上下文实例
 */
 protected Activity activity;

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 this.activity = this;
 }

 /**
 * 列表选择popupWindow
 *
 * @param parentView 父View
 * @param itemTexts  列表项文本集合
 * @param itemClickListener 列表项点击事件
 */
 public void showFilterPopupWindow(View parentView,0);
 }

 /**
 * 列表选择popupWindow
 *
 * @param parentView 父View
 * @param itemTexts  列表项文本集合
 * @param itemClickListener 列表项点击事件
 */
 public void showFilterPopupWindow(View parentView,则认设置为0.6f
 if (0 == alpha) {
  alpha = 0.6f;
 }
 // 设置背景透明度
 WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
 lp.alpha = alpha;
 activity.getWindow().setAttributes(lp);
 // 显示pop
 mPopupWindow.showAsDropDown(parentView);

 }

 /**
 * Tab筛选栏切换
 *
 * @param isChecked  选中状态
 * @param showView  展示pop的跟布局
 * @param showMes  展示选择的数据
 * @param itemClickListener 点击回调
 * @param tabs  所有的cb(需要几个输入几个就可以,new CustomerDismissListener() {
  @Override
  public void onDismiss() {
   super.onDismiss();
   // 当pop消失时对第一个cb进行.setChecked(false)操作
   tabs[0].setChecked(false);
  }
  });
 } else {
  // 关闭check@R_391_2419@时直接隐藏popuwindow
  hidePopListView();
 }
 }

 /**
 * Tab筛选栏切换
 *
 * @param isChecked  选中状态
 * @param showView  展示pop的跟布局
 * @param showMes  展示选择的数据源
 * @param itemClickListener 点击回调
 * @param tabs  所有的cb(需要几个输入几个就可以,new CustomerDismissListener() {
  @Override
  public void onDismiss() {
   super.onDismiss();
   // 当pop消失时对第一个cb进行.setChecked(false)操作
   tabs[0].setChecked(false);
  }
  });
 } else {
  // 关闭check@R_391_2419@时直接隐藏popuwindow
  hidePopListView();
 }
 }

 /**
 * 自定义OnDismissListener
 */
 public class CustomerDismissListener implements PopupWindow.OnDismissListener {
 @Override
 public void onDismiss() {
  // 当pop消失的时候,如果显示则dismiss
 if (mPopupWindow != null && mPopupWindow.isShowing()) {
  mPopupWindow.dismiss();
  mPopupWindow = null;
 }
 }
}

Source address: Android universal filter bar source code

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>