PopupWindow?RecyclerView實(shí)現(xiàn)下拉選擇Spinner示例解析
對(duì)Spinner進(jìn)行封裝
當(dāng)我們?cè)贏ndroid開發(fā)中需要實(shí)現(xiàn)下拉選擇功能時(shí),可以使用自定義的Spinner控件來實(shí)現(xiàn)。Spinner控件是一個(gè)下拉列表框,可以顯示多個(gè)選項(xiàng)供用戶選擇,并在用戶選擇后顯示所選項(xiàng)的文本。
自定義的Spinner控件
為了方便使用和擴(kuò)展,我們可以對(duì)Spinner進(jìn)行封裝,創(chuàng)建一個(gè)自定義的Spinner控件。自定義Spinner可以具備以下特性:
- 點(diǎn)擊展開和收起:通過點(diǎn)擊Spinner,可以展開或收起下拉列表框。
- 自定義樣式:可以根據(jù)項(xiàng)目需求,自定義Spinner的外觀樣式,如背景顏色、字體顏色、箭頭圖標(biāo)等。
- 支持?jǐn)?shù)據(jù)源:可以傳入數(shù)據(jù)源,將數(shù)據(jù)顯示在下拉列表框中供用戶選擇。
- 默認(rèn)選擇項(xiàng):可以指定一個(gè)默認(rèn)選擇項(xiàng),在下拉列表框展開時(shí),該選項(xiàng)將被高亮顯示。
- 選擇監(jiān)聽:可以設(shè)置選擇監(jiān)聽器,監(jiān)聽用戶的選擇操作,并進(jìn)行相應(yīng)的處理。
通過封裝Spinner,我們可以將其功能與外觀進(jìn)行統(tǒng)一管理,并提供更加簡(jiǎn)潔和易用的接口供其他開發(fā)者使用。這樣,其他開發(fā)者在使用時(shí)只需關(guān)注數(shù)據(jù)源和監(jiān)聽回調(diào)即可,無需關(guān)心Spinner的內(nèi)部實(shí)現(xiàn)細(xì)節(jié)。
自定義Spinner的封裝可以提高代碼的可維護(hù)性和可復(fù)用性,減少重復(fù)代碼的編寫,同時(shí)也使代碼結(jié)構(gòu)更加清晰和易于理解。
總之,封裝Spinner可以幫助我們更高效地實(shí)現(xiàn)下拉選擇功能,并提供靈活性和可擴(kuò)展性,使代碼更加優(yōu)雅和易于維護(hù)。
使用第三方控件:implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.47'
自定義控件封裝
定義類CustomSpinner繼承LinearLayout以下是示例:
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<T> extends LinearLayout { private TextView textView; private ImageView imageView; private PopupWindow popupWindow; private List<T> 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<T> dataList) { this.dataList = dataList; } /** * 刷新頁面 * * @param data */ public void replaceData(@NonNull Collection<? extends T> data) { // 不是同一個(gè)引用才清空列表 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); } }
上面是控件代碼
適配器代碼
下面是適配器代碼都是使用范型來傳輸數(shù)據(jù)這樣方便通用!
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<T> extends BaseQuickAdapter<T, BaseViewHolder> { private int selectedPosition = -1; private OnItemClickListener itemClickListener; public CustomAdapter(@LayoutRes int layoutResId, @Nullable List<T> 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); } }
下面是代碼鏈接:https://gitee.com/xtxiaolu/XiaoluDemo.git
以上就是PopupWindow RecyclerView實(shí)現(xiàn)下拉選擇Spinner示例解析的詳細(xì)內(nèi)容,更多關(guān)于PopupWindow RecyclerView下拉選擇Spinner的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
ActivityManagerService之Service啟動(dòng)過程解析
這篇文章主要為大家介紹了ActivityManagerService之Service啟動(dòng)過程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Mac Android Studio 3.0 Terminal 中文亂碼問題處理
本文給大家分享的是在更新Android Studio 3.0之后,使用Terminal時(shí),發(fā)現(xiàn) git log 命令查看歷史 log會(huì)亂碼,以及最后的解決方法,推薦給小伙伴們2017-11-11- 究其為啥需要多線程的本質(zhì)就是異步處理,直觀一點(diǎn)說就是不要讓用戶感覺到“很卡”為了提高用戶體驗(yàn)?zāi)鞘潜仨氁褂玫?/div> 2013-06-06
Flutter 插件url_launcher簡(jiǎn)介
最近項(xiàng)目需求是打開一個(gè)連接跳轉(zhuǎn)到安卓或蘋果默認(rèn)的瀏覽器。雖然開始一個(gè)簡(jiǎn)單的要求,其中的一個(gè)細(xì)節(jié)就是執(zhí)行打開網(wǎng)頁這一操作后,不能看上去像在應(yīng)用內(nèi)部打開,看上去要在應(yīng)用外部打開,今天小編給大家介紹Flutter 插件url_launcher的相關(guān)知識(shí),感興趣的朋友一起看看吧2020-04-04Android TagCloudView云標(biāo)簽的使用方法
這篇文章主要為大家詳細(xì)介紹了Android TagCloudView云標(biāo)簽的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android實(shí)現(xiàn)搜索保存歷史記錄功能
這篇文章主要介紹了Android實(shí)現(xiàn)搜索保存歷史記錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05最新評(píng)論