gpt4 book ai didi

java - 阻止 Android Spinner 删除列表

转载 作者:太空宇宙 更新时间:2023-11-04 10:35:36 24 4
gpt4 key购买 nike

我有一个 Android 微调器,当我的列表中的项目少于一项时,我想禁用它。我的意思是我希望显示标题,但我不希望在单击它时下拉列表。

我已经尝试过 isClickable = false 和 isEnabled = false 就像许多帖子中那样,但我仍然无法让它工作。

我成功地使用了微调器下拉图标,但我仍然可以单击文本并获取列表。

class CustomSpinnerAdapter(private val ctx: Context, val locations: List<Location>) : ArrayAdapter<Location>(ctx, R.layout.list_item_spinner_view) {
override fun isEmpty() = locations.isEmpty()

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val context = parent.context
val inflater = LayoutInflater.from(context)
var view = convertView
if (view == null)
view = inflater.inflate(R.layout.list_item_spinner_view, parent, false)
view!!.location_spinner_name.text = locations[position].name
if (count < 2) {
view.location_spinner.visibility = View.GONE
view.better_name.isEnabled = false
view.better_name.isClickable = false
}

view.location_spinner_name.typeface = Typeface.createFromAsset(ctx.assets, ctx.getString(R.string.font_bold))
return view
}

这是我的标题的 View

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/better_name">

<TextView
android:id="@+id/location_spinner_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"/>

<com.ge.cbyge.view.TintableImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_arrow_drop_down_black"
android:tint="@color/medium_gray"
android:paddingTop="@dimen/activity_home_spinner_dropdown_padding_top"/>

</LinearLayout>

最佳答案

希望这会有所帮助。它不是对您的应用程序的重写,但也许是您可以集成的想法:

您可以像这样创建自己的 Spinner 类:

class MySpinner extends AppCompatSpinner {


public MySpinner(Context context) {
super(context);
}

public MySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean performClick() {
if ( getCount() == 0 ) {
return true; // we do nothing because adapter contents = 0
}
else {
return super.performClick(); // we proceed as normal
}
}

}

然后将 XML 引用从“Spinner”更改为“[yourPackageName].MySpinner”

关于java - 阻止 Android Spinner 删除列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49523641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com