CustomFiltControl是一款基于Android平台的可筛选的弹窗控件,能够极大地提高用户选择筛选条件的效率和便捷性。下面将为大家提供CustomFiltControl的完整攻略。
一、控件使用说明
1.1 导入依赖库
首先,在您的Android项目中的build.gradle文件中添加以下依赖:
implementation 'com.github.xlxsam:CustomFiltControl:1.0.3'
1.2 引入控件
在您需要使用CustomFiltControl控件的xml布局文件中添加以下代码:
<com.sam.customfilterproject.view.CustomFiltControlView
android:id="@+id/customFiltControlView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:background_color="@color/bg_common_white"
app:arrow_color="@color/colorAccent"
app:arrow_width="12dp"
app:arrow_height="6dp"
app:text_color="@color/tv_common_black"
app:text_size="14sp"
app:spacing="6dp"
app:checked_bg_color="#c054bfaf"
app:unchecked_bg_color="#c0601931" />
1.3 设置数据源
在Activity或Fragment的代码中,设置CustomFiltControl控件的数据源,这里提供两个示例:
示例一
CustomFiltControlView customFiltControlView = findViewById(R.id.customFiltControlView);
final List<FilterTab> filterTabList = new ArrayList<>();
FilterTab filterTab1 = new FilterTab(1, "标签1");
FilterTab filterTab2 = new FilterTab(2, "标签2");
FilterTab filterTab3 = new FilterTab(3, "标签3");
filterTabList.add(filterTab1);
filterTabList.add(filterTab2);
filterTabList.add(filterTab3);
List<FilterItem> filterItemList = new ArrayList<>();
FilterItem filterItem1 = new FilterItem(10, "选项1", 1);
FilterItem filterItem2 = new FilterItem(11, "选项2", 1);
FilterItem filterItem3 = new FilterItem(12, "选项3", 1);
FilterItem filterItem4 = new FilterItem(13, "选项4", 2);
FilterItem filterItem5 = new FilterItem(14, "选项5", 2);
FilterItem filterItem6 = new FilterItem(15, "选项6", 3);
filterItemList.add(filterItem1);
filterItemList.add(filterItem2);
filterItemList.add(filterItem3);
filterItemList.add(filterItem4);
filterItemList.add(filterItem5);
filterItemList.add(filterItem6);
customFiltControlView.init(filterTabList, filterItemList);
示例二
CustomFiltControlView customFiltControlView = findViewById(R.id.customFiltControlView);
final List<FilterTab> filterTabList = new ArrayList<>();
FilterTab filterTab1 = new FilterTab(1, "标签1");
FilterTab filterTab2 = new FilterTab(2, "标签2");
FilterTab filterTab3 = new FilterTab(3, "标签3");
filterTabList.add(filterTab1);
filterTabList.add(filterTab2);
filterTabList.add(filterTab3);
List<FilterItem> filterItemList1 = new ArrayList<>();
FilterItem filterItem1 = new FilterItem(10, "选项1", 1);
FilterItem filterItem2 = new FilterItem(11, "选项2", 1);
FilterItem filterItem3 = new FilterItem(12, "选项3", 1);
filterItemList1.add(filterItem1);
filterItemList1.add(filterItem2);
filterItemList1.add(filterItem3);
List<FilterItem> filterItemList2 = new ArrayList<>();
FilterItem filterItem4 = new FilterItem(13, "选项4", 2);
FilterItem filterItem5 = new FilterItem(14, "选项5", 2);
filterItemList2.add(filterItem4);
filterItemList2.add(filterItem5);
List<FilterItem> filterItemList3 = new ArrayList<>();
FilterItem filterItem6 = new FilterItem(15, "选项6", 3);
filterItemList3.add(filterItem6);
List<List<FilterItem>> filterItemListList = new ArrayList<>();
filterItemListList.add(filterItemList1);
filterItemListList.add(filterItemList2);
filterItemListList.add(filterItemList3);
customFiltControlView.init(filterTabList, filterItemListList);
二、控件使用示例
2.1 示例一
在CustomFiltControlView的筛选条件发生改变时,可以通过事件回调获取筛选条件的具体内容:
customFiltControlView.setOnItemChildClickListener(new CustomFiltControlView.OnItemChildClickListener() {
@Override
public void onItemChildClick(View view, int tabIndex, int itemId, String itemTitle, boolean isChecked) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("tabIndex: " + tabIndex + "\n");
stringBuilder.append("itemId: " + itemId + "\n");
stringBuilder.append("itemTitle: " + itemTitle + "\n");
stringBuilder.append("isChecked: " + isChecked);
Toast.makeText(MainActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onTabClick(View view, int tabIndex, boolean isChecked) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("tabIndex: " + tabIndex + "\n");
stringBuilder.append("isChecked: " + isChecked);
Toast.makeText(MainActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
});
2.2 示例二
在CustomFiltControlView的多选和确定按钮功能自定义时,可以通过事件回调获取筛选条件的具体内容:
customFiltControlView.setOnClickChangeListener(new CustomFiltControlView.OnClickChangeListener() {
@Override
public void confirmClick(View view, Map<Integer, List<Integer>> map) {
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
int tabIndex = entry.getKey();
List<Integer> itemList = entry.getValue();
String title = "";
for (Integer itemId : itemList) {
title += customFiltControlView.getItemTitle(tabIndex, itemId) + "、";
}
title = title.substring(0, title.length() - 1);
stringBuilder.append(customFiltControlView.getTabText(tabIndex) + ": " + title + "\n");
}
Toast.makeText(MainActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void cancelClick(View view) {
Toast.makeText(MainActivity.this, "取消操作", Toast.LENGTH_SHORT).show();
}
});
以上就是CustomFiltControl的完整攻略和两个示例的讲解,希望对大家有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android可筛选的弹窗控件CustomFiltControl - Python技术站