当我们在Android开发中需要实现下拉选择功能时,可以使用自定义的Spinner控件来实现。Spinner控件是一个下拉列表框,可以显示多个选项供用户选择,并在用户选择后显示所选项的文本。
为了方便使用和扩展,我们可以对Spinner进行封装,创建一个自定义的Spinner控件。自定义Spinner可以具备以下特性:
- 点击展开和收起:通过点击Spinner,可以展开或收起下拉列表框。
- 自定义样式:可以根据项目需求,自定义Spinner的外观样式,如背景颜色、字体颜色、箭头图标等。
- 支持数据源:可以传入数据源,将数据显示在下拉列表框中供用户选择。
- 默认选择项:可以指定一个默认选择项,在下拉列表框展开时,该选项将被高亮显示。
- 选择监听:可以设置选择监听器,监听用户的选择操作,并进行相应的处理。
通过封装Spinner,我们可以将其功能与外观进行统一管理,并提供更加简洁和易用的接口供其他开发者使用。这样,其他开发者在使用时只需关注数据源和监听回调即可,无需关心Spinner的内部实现细节。
自定义Spinner的封装可以提高代码的可维护性和可复用性,减少重复代码的编写,同时也使代码结构更加清晰和易于理解。
总之,封装Spinner可以帮助我们更高效地实现下拉选择功能,并提供灵活性和可扩展性,使代码更加优雅和易于维护。
使用第三方控件:implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.47'
图片示例:
自定义控件封装 定义类CustomSpinner继承LinearLayout以下是示例:
点击展开查看CustomSpinner代码
package com.example.demo.view;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.demo.R;
import java.util.Collection;
import java.util.List;
/**
@author: xtxiaolu
@date: 2023/7/7
描述: 自定义公共 下拉选择控件
*/
public class CustomSpinner extends LinearLayout {
private TextView textView;
private ImageView imageView;
private PopupWindow popupWindow;
private List dataList;
private boolean isExpanded = false;
private OnItemSelectedListener itemSelectedListener;
public CustomSpinner(Context context) {
super(context);
init();
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
LayoutInflater.from(getContext()).inflate(R.layout.custom_spinner_layout, this, true);
textView = findViewById(R.id.ll_list_default_txt);
imageView = findViewById(R.id.ll_list_default_icon);
setClickable(true);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isExpanded) {
collapse();
} else {
expand();
}
}
});
}
public void setData(List dataList) {
this.dataList = dataList;
}
/**
刷新页面
@param data
*/
public void replaceData(@NonNull Collection<? extends T> data) {
// 不是同一个引用才清空列表
if (data != dataList) {
dataList.clear();
dataList.addAll(data);
}
}
public void setTextViewValue(String value) {
textView.setText(value);
}
public TextView getTextViewValue() {
return textView;
}
private void expand() {
if (dataList == null || dataList.isEmpty()) {
return;
}
textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_order_black));
imageView.setImageResource(R.drawable.expand_arrows_fold);
View popupView = LayoutInflater.from(getContext()).inflate(R.layout.popup_selector, null);
RecyclerView recyclerView = popupView.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
CustomAdapter popAdapter = new CustomAdapter<>(R.layout.item_listview_popwin, dataList);
popAdapter.setSelectedPosition(0);
popAdapter.setOnItemClickListener(new CustomAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
T selectedItem = dataList.get(position);
if (itemSelectedListener != null) {
itemSelectedListener.onItemSelected(position, selectedItem);
}
popupWindow.dismiss();
}
@Override
public void convertView(BaseViewHolder holder, Object item, boolean isSelected) {
if (itemSelectedListener != null) {
itemSelectedListener.onItemCallBackData(holder, item);
}
}
});
recyclerView.setAdapter(popAdapter);
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
popupWindow = new PopupWindow(popupView, width, height);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
collapse();
}
});
int[] location = new int[2];
getLocationOnScreen(location);
int x = location[0];
int y = location[1] + getHeight();
popupWindow.showAtLocation(this, Gravity.NO_GRAVITY, x, y);
isExpanded = true;
}
private void collapse() {
textView.setTextColor(ContextCompat.getColor(getContext(), R.color.colorTextBlue));
imageView.setImageResource(R.drawable.expand_arrows_unfold);
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
isExpanded = false;
}
public void setOnItemSelectedListener(OnItemSelectedListener listener) {
this.itemSelectedListener = listener;
}
public interface OnItemSelectedListener {
void onItemSelected(int position, Object item);
void onItemCallBackData(BaseViewHolder holder, Object item);
}
}
setClickable(true);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isExpanded) {
collapse();
} else {
expand();
}
}
});
textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_order_black));
imageView.setImageResource(R.drawable.expand_arrows_fold);
View popupView = LayoutInflater.from(getContext()).inflate(R.layout.popup_selector, null);
RecyclerView recyclerView = popupView.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
CustomAdapter popAdapter = new CustomAdapter<>(R.layout.item_listview_popwin, dataList);
popAdapter.setSelectedPosition(0);
popAdapter.setOnItemClickListener(new CustomAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
T selectedItem = dataList.get(position);
if (itemSelectedListener != null) {
itemSelectedListener.onItemSelected(position, selectedItem);
}
popupWindow.dismiss();
}
@Override
public void convertView(BaseViewHolder holder, Object item, boolean isSelected) {
if (itemSelectedListener != null) {
itemSelectedListener.onItemCallBackData(holder, item);
}
}
});
recyclerView.setAdapter(popAdapter);
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
popupWindow = new PopupWindow(popupView, width, height);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
collapse();
}
});
int[] location = new int[2];
getLocationOnScreen(location);
int x = location[0];
int y = location[1] + getHeight();
popupWindow.showAtLocation(this, Gravity.NO_GRAVITY, x, y);
isExpanded = true;
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
isExpanded = false;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
上面是控件代码 下面是适配器代码都是使用范型来传输数据这样方便通用!
展开查看CustomAdapter
package com.example.demo.view;
import android.view.View;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import java.util.List;
/**
@author: xtxiaolu
@date: 2023/7/7
描述:
*/
public class CustomAdapter extends BaseQuickAdapter<T, BaseViewHolder> {
private int selectedPosition = -1;
private OnItemClickListener itemClickListener;
public CustomAdapter(@LayoutRes int layoutResId, @Nullable List data) {
super(layoutResId, data);
}
public void setSelectedPosition(int position) {
this.selectedPosition = position;
notifyDataSetChanged();
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.itemClickListener = listener;
}
@Override
protected void convert(BaseViewHolder holder, T item) {
if (itemClickListener != null) {
itemClickListener.convertView(holder, item, selectedPosition == holder.getBindingAdapterPosition());
}
}
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null) {
itemClickListener.onItemClick(holder.getBindingAdapterPosition());
}
}
});
}
public interface OnItemClickListener {
void onItemClick(int position);
void convertView(BaseViewHolder holder, Object item, boolean isSelected);
}
}
void convertView(BaseViewHolder holder, Object item, boolean isSelected);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
如果本文对您有所帮助请点赞收藏给予支持,谢谢有您的支持我将不惜再接再厉!
下面是代码链接: 🔥点击下载代码⬇️