編輯:關於 Android 程式設計
在不少列表頁與搜尋場景裡,我們會看到「一級選單切換、二級選項連動更新」的篩選設計。本文以 PopupWindow 搭配雙 ListView 為基礎,示範如何打造這樣一個層級聯動的篩選面板。
先看效果:



整體拆成三個部分:負責彈窗本身的 PopuWindowWidget、定義互動回呼的 PopuWindowListener 介面,以及承載資料與邏輯的 PopuWindowView。
首先是彈窗控制元件:
package com.example.checklistview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
@SuppressLint("ViewConstructor")
public class PopuWindowWidget {
private static PopuWindowWidget popuWindow = null;
private Context context;
public PopuWindowWidget(Context context) {
this.context = context;
}
public static PopuWindowWidget getInstance(Context context) {
if (popuWindow == null) {
popuWindow = new PopuWindowWidget(context);
}
return popuWindow;
}
public void onCreatePopuWindow(View view, PopuWindowListener listener) {
PopupWindow pop = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
pop.setOutsideTouchable(false);
pop.setFocusable(false);
pop.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.background_trans));
listener.initialization(pop);
}
}
接著是回呼介面,用來把彈窗的初始化、顯示狀態與點選結果回傳給外部:
package com.example.checklistview;
import android.widget.PopupWindow;
public interface PopuWindowListener {
void isShow(boolean isShow, String message);
void initialization(PopupWindow popuWindow);
void onClickItem(Object obj);
}
最後是核心的 PopuWindowView。左側 ListView 是一級分類(發布時間、期望月薪、工作經驗…),右側 ListView 則對應各分類下的細項;點選左側會刷新右側清單,點選右側則把結果彙整到 checkString 中:
package com.example.checklistview;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.checklistview.CheckAdapter.NameOnClickListener;
public class PopuWindowView implements NameOnClickListener {
private static PopuWindowListener listener;
private View view;
private Activity activity;
private TextView tv;
private ListView listView_bar;
private ListView listView_data;
private ArrayList<CheckBean> data = null;
private ArrayList<CheckBean> bar = null;
private HashMap<Integer, ArrayList<CheckBean>> map = new HashMap<Integer, ArrayList<CheckBean>>();
private HashMap<String, String> checkString = new HashMap<String, String>();
private CheckAdapter adapter_bar;
private DataCheckAdapter adapter_data;
private ViewHolder vhs;
private TextView check_value;
private Button sure;
public static PopuWindowView getInstance(Activity activity, PopuWindowListener popuWindowListener, int layout) {
listener = popuWindowListener;
return new PopuWindowView(activity, layout);
}
public PopuWindowView(Activity activity, int layout) {
this.view = activity.getLayoutInflater().inflate(layout, null);
this.activity = activity;
initialization();
}
private void initialization() {
check_value = (TextView) view.findViewById(R.id.TextView_check_value);
sure = (Button) view.findViewById(R.id.sure);
listView_bar = (ListView) view.findViewById(R.id.action_bar);
listView_data = (ListView) view.findViewById(R.id.listview_data);
initData();
adapter_bar = new CheckAdapter(activity, bar, this);
adapter_data = new DataCheckAdapter(activity, map.get(0), this);
listView_bar.setAdapter(adapter_bar);
listView_data.setAdapter(adapter_data);
sure.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.isShow(false, checkString.toString());
}
});
}
private void initData() {
bar = new ArrayList<CheckBean>();
bar.add(new CheckBean(0, "發布時間", true));
bar.add(new CheckBean(1, "期望月薪", false));
bar.add(new CheckBean(2, "工作經驗", false));
data = new ArrayList<CheckBean>();
data.add(new CheckBean(0, "所有", false));
data.add(new CheckBean(1, "今天", false));
data.add(new CheckBean(2, "最近三天", false));
data.add(new CheckBean(3, "最近一週", false));
data.add(new CheckBean(4, "最近一個月", false));
map.put(0, data);
data = new ArrayList<CheckBean>();
data.add(new CheckBean(0, "面議", false));
data.add(new CheckBean(1, "3000元以下", false));
data.add(new CheckBean(2, "3000-5000元", false));
data.add(new CheckBean(3, "5000-8000元", false));
data.add(new CheckBean(4, "8000-10000元", false));
data.add(new CheckBean(5, "10000元以上", false));
map.put(1, data);
data = new ArrayList<CheckBean>();
data.add(new CheckBean(0, "不限", false));
data.add(new CheckBean(1, "無工作經驗", false));
data.add(new CheckBean(2, "1年以下", false));
data.add(new CheckBean(3, "1-3年工作經驗", false));
data.add(new CheckBean(4, "3-5年工作經驗", false));
data.add(new CheckBean(5, "5-10年工作經驗", false));
data.add(new CheckBean(6, "10年以上工作經驗", false));
map.put(2, data);
data = new ArrayList<CheckBean>();
data.add(new CheckBean(0, "全部", false));
data.add(new CheckBean(1, "全職", false));
data.add(new CheckBean(2, "兼職", false));
data.add(new CheckBean(3, "實習", false));
map.put(3, data);
}
public View getView() {
return view;
}
@Override
public void callbackPosition(int type, int position) {
if (type == 1) {
// 切換一級分類,同步刷新右側清單
for (int i = 0; i < adapter_bar.getList().size(); i++) {
adapter_bar.getList().get(i).setCheck(adapter_bar.getList().get(i).getId() == position);
}
adapter_data.setList(map.get(position));
adapter_bar.notifyDataSetChanged();
adapter_data.notifyDataSetChanged();
listView_data.post(new Runnable() {
@Override
public void run() {
for (int i = 0; i < adapter_data.getList().size(); i++) {
if (adapter_data.getList().get(i).isCheck()) {
listView_data.setSelectionFromTop(i, 0);
return;
}
}
listView_data.setSelectionFromTop(0, 0);
}
});
} else if (type == 2) {
// 選定二級細項
int index = -1;
for (int i = 0; i < adapter_data.getList().size(); i++) {
if (adapter_data.getList().get(i).getId() == position) {
adapter_data.getList().get(i).setCheck(true);
index = i;
} else {
adapter_data.getList().get(i).setCheck(false);
}
}
adapter_data.notifyDataSetChanged();
for (int i = 0; i < adapter_bar.getList().size(); i++) {
if (adapter_bar.getList().get(i).isCheck()) {
for (int j = 0; j < map.get(i).size(); j++) {
if (j == index) {
map.get(i).get(index).setCheck(true);
} else {
map.get(i).get(j).setCheck(false);
}
}
}
}
for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < map.get(i).size(); j++) {
if (map.get(i).get(j).isCheck()) {
checkString.put(adapter_bar.getList().get(i).getName(), map.get(i).get(j).getName());
}
}
}
check_value.setText("您目前選中:" + checkString.toString());
}
}
}
關鍵設計在於以 HashMap<Integer, ArrayList> 保存每個一級分類對應的二級清單,切換時只需替右側 Adapter 換一份資料來源即可,做到「一動全動」的連鎖效果。
資源下載:https://download.csdn.net/detail/anddroid_lanyan/8229077
Android Support Design Library 常用控件入門(一)
Google 推出 Material Design 已經有一段時間了。為了讓開發者更容易做出符合材料設計的介面,官方提供了 Android Support Design
Android EditText 為什麼只剩下面一條橫線?改回傳統樣式的方法
從 Android 4.0 開始,EditText 的預設外觀被換成了 Material 之前的「只有底線」風格——輸入框本身完全透明,只在下方留一條細線。做的時候覺得很
Android AlertDialog 對話框詳解與多種實例
Android AlertDialog 全面解析 Android 主要提供四種對話框: 1. AlertDialog:功能最豐富、應用最廣泛的對話框; 2. Prog
Android 學習筆記:Activity 狀態保存與主題皮膚設定
本篇筆記整理兩個重點:Activity 的狀態保存機制,以及如何透過 android:theme 為 Activity 設定主題皮膚。 一、Activity 狀態的保存