我在Android Studio中使用自定义布局Spinner
:
用户可以选择具有自定义布局的项目(图像+文本)
我需要在选中后仅查看微调器上的文本
没有完整的布局
不喜欢这个:
我的自定义Adapter
:
public class ColorAdapter2 extends ArrayAdapter<colorItem> {
LayoutInflater inflater;
ArrayList< colorItem> items;
public ColorAdapter2(Context context, int textViewResourceId,
ArrayList< colorItem> objects , LayoutInflater inflater) {
super(context, textViewResourceId, objects);
this.inflater = inflater;
items = objects;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.color_item, parent, false);
TextView label = (TextView) row.findViewById(R.id.nameColor);
label.setText(items.get(position).getName());
ImageView icon = (ImageView) row.findViewById(R.id.iconColor);
icon.setImageResource(items.get(position).getImageId());
return row;
}
}
...和行布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/edittext"
android:padding="10dp"
android:layout_margin="10dp">
<ImageView
android:id="@+id/iconColor"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="30dp"
android:src="@drawable/blue_image"/>
<TextView
android:id="@+id/nameColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
答案 0 :(得分:1)
请尝试通过
手动隐藏元素spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
view.findViewById(R.id.viewYouWantToHide).setVisibility(View.GONE);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
并且您可能需要恢复隐藏的元素,以便在使用其他元素重新选择微调器或不选择任何元素时可见
答案 1 :(得分:0)
使用事件提供的视图将默认文本或微调框上的选定文本或静态文本的隐藏/可见性设置为零。
public void onItemSelected(AdapterView<?> parentView, View view, int position, long id) {
TextView tv = (TextView) view;
tv.setVisibility(View.GONE);
}
答案 2 :(得分:-1)
选择微调器项目隐藏您的Imageview
并将其设置为Nothing Selected
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
selectedItemView.findViewById(R.id.iconColor).setVisibility(View.GONE);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
答案 3 :(得分:-1)
因此,您可以收听itemSelectedEvent,然后从中检索ImageView,然后将其设置为&#34; GONE&#34;。
以下是您将如何做到这一点的示例。
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View view, int position, long id) {
// your code here
ImageView imageView = view.findViewById(R.id.iconColor);
imageView.setVisibility(View.GONE);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});