Android如何实现多级筛选,代码演示
时间: 2024-05-06 07:20:42 浏览: 151
Android实现多级筛选可以使用RecyclerView和PopupWindow来实现。以下是一个简单的演示代码:
1. 首先创建一个RecyclerView用于展示筛选条件:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/filter_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
2. 创建一个PopupWindow用于展示筛选条件的选项:
```java
public class FilterPopupWindow extends PopupWindow {
private Context mContext;
private RecyclerView mRecyclerView;
private FilterAdapter mAdapter;
private List<String> mData;
public FilterPopupWindow(Context context, List<String> data) {
super(context);
mContext = context;
mData = data;
initView();
}
private void initView() {
View view = LayoutInflater.from(mContext).inflate(R.layout.popup_filter, null);
mRecyclerView = view.findViewById(R.id.filter_option_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
mAdapter = new FilterAdapter(mData);
mRecyclerView.setAdapter(mAdapter);
setContentView(view);
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setFocusable(true);
setOutsideTouchable(true);
setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
public void setOnItemClickListener(FilterAdapter.OnItemClickListener listener) {
mAdapter.setOnItemClickListener(listener);
}
}
```
3. 创建一个筛选条件的Adapter:
```java
public class FilterAdapter extends RecyclerView.Adapter<FilterAdapter.ViewHolder> {
private List<String> mData;
private int mSelectedPosition = -1;
private OnItemClickListener mItemClickListener;
public FilterAdapter(List<String> data) {
mData = data;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_filter_option, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.mOption.setText(mData.get(position));
holder.mOption.setSelected(position == mSelectedPosition);
holder.itemView.setOnClickListener(v -> {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(position);
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public void setSelectedPosition(int position) {
mSelectedPosition = position;
notifyDataSetChanged();
}
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mItemClickListener = listener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView mOption;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mOption = itemView.findViewById(R.id.filter_option);
}
}
}
```
4. 在Activity中使用RecyclerView和PopupWindow实现多级筛选:
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView mFilterList;
private FilterAdapter mFilterAdapter;
private List<String> mFilterData;
private FilterPopupWindow mPopupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFilterList = findViewById(R.id.filter_list);
initFilterData();
mFilterAdapter = new FilterAdapter(mFilterData);
mFilterList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
mFilterList.setAdapter(mFilterAdapter);
mFilterAdapter.setOnItemClickListener(position -> {
mFilterAdapter.setSelectedPosition(position);
showPopupWindow(position);
});
}
private void initFilterData() {
mFilterData = new ArrayList<>();
mFilterData.add("品牌");
mFilterData.add("价格");
mFilterData.add("颜色");
}
private void showPopupWindow(int position) {
if (mPopupWindow == null) {
mPopupWindow = new FilterPopupWindow(this, Arrays.asList("华为", "小米", "苹果"));
mPopupWindow.setOnDismissListener(() -> mFilterAdapter.setSelectedPosition(-1));
mPopupWindow.setOnItemClickListener(position1 -> {
mPopupWindow.dismiss();
// TODO: 处理筛选结果
});
} else {
mPopupWindow.dismiss();
}
int[] location = new int[2];
mFilterList.getChildAt(position).getLocationInWindow(location);
mPopupWindow.showAtLocation(mFilterList, Gravity.NO_GRAVITY, location[0], location[1] + mFilterList.getChildAt(position).getHeight());
}
}
```
以上是一个简单的多级筛选的演示代码,可以根据具体需求进行修改和优化。
阅读全文