[英]Remove DropDownViewResource from a Spinner - java Android Studio
I have a spinner that I create like this: 我有一个像这样创建的微调器:
operatorSpinner = new Spinner(this);
operatorSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, operatorSpinnerArray);
operatorSpinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
operatorSpinner.setAdapter(operatorSpinnerArrayAdapter);
The layout, spinner_item is this: 布局spinner_item是这样的:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="28dip"
android:layout_height="28dip"
android:gravity="center"
android:background="@drawable/circle"
android:textColor="#a4a3a3" />
My reason for applying this layout is to add a circle to the background of each item in the Spinner's dropdown list. 我应用此布局的原因是在微调框的下拉列表中为每个项目的背景添加一个圆圈。 However, I only want the circle background applied under certain conditions, so I need a way to programmatically remove either the DropDownViewResource or the layout background within that resource. 但是,我只希望在特定条件下应用圆圈背景,因此我需要一种以编程方式删除该资源中的DropDownViewResource或布局背景的方法。
I tried this: 我尝试了这个:
operatorSpinnerArrayAdapter.setDropDownViewResource(0);
but it resulted in the app crashing when that code executes. 但执行该代码后导致应用崩溃。 I found a way to achieve what I want by replacing the adapter with a new adapter like this: 我找到了一种通过用新的适配器替换适配器来实现我想要的方法:
operatorSpinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, operatorSpinnerArray);
operatorSpinner.setAdapter(operatorSpinnerArrayAdapter);
However, my 'feeling' is that creating a new adapter every time I want to change its background is not the best approach. 但是,我的“感觉”是,每次我想更改其背景时都创建一个新适配器不是最佳方法。 This is my first app I'm trying to develop so I'm not experienced, but I suspect this approach may eat up resources if repeated many times. 这是我尝试开发的第一个应用程序,因此我没有经验,但是我怀疑这种方法如果重复很多次可能会消耗资源。 Can anybody suggest a better approach or confirm/falsify my concerns? 有人可以提出更好的方法或确认/伪造我的担忧吗? Thanks 谢谢
I recommend you to make a custom adapter by which you can play with whatever the customization you want. 我建议您制作一个自定义适配器,通过该适配器您可以使用所需的任何自定义。
Take a look at http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/ 看看http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/
Don't extend your custom adapter to Array adapter but BaseAdapter.Yo need to pass a RowItem like 不要将自定义适配器扩展为Array适配器,但是BaseAdapter.Yo需要传递RowItem之类的
class RowItem{
...
boolean isConditionSatisfied;
...
}
and in you adapter override the getView() as 并在您的适配器中覆盖getView()为
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
if(isConditionSatisfied){
convertView.setBackground(R.drawable.your_drawable);
}
return convertView;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.