如何将图像视图的可见性设置为 Spinner 所选项目的可见性
Posted
技术标签:
【中文标题】如何将图像视图的可见性设置为 Spinner 所选项目的可见性【英文标题】:How to set visibility of imageview to VISIBLE of the item selected of Spinner 【发布时间】:2021-10-16 17:02:15 【问题描述】:我想在选择项目时将可见性设置为 VISIBLE,对于未选择的项目设置为 GONE。
目前我有一个适配器类,其代码如下:-
public class FruitAdapter extends BaseAdapter
private Context context;
private List<Fruit> fruitList;
public FruitAdapter(Context context, List<Fruit> fruitList)
this.context = context;
this.fruitList = fruitList;
@Override
public int getCount()
return fruitList != null ? fruitList.size() : 0;
@Override
public Object getItem(int i)
return i;
@Override
public long getItemId(int i)
return i;
@Override
public View getView(int i, View view, ViewGroup viewGroup)
View rootView = LayoutInflater.from(context)
.inflate(R.layout.item_fruit, viewGroup, false);
TextView txtName = rootView.findViewById(R.id.name);
ImageView image = rootView.findViewById(R.id.image);
ImageView selected = rootView.findViewById(R.id.selected);
txtName.setText(fruitList.get(i).getName());
image.setImageResource(fruitList.get(i).getImage());
return rootView;
以及下面的xml文件:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<ImageView
android:id="@+id/image"
android:layout_
android:layout_
android:src="@drawable/orange"
android:layout_marginStart="8dp"/>
<TextView
android:id="@+id/name"
android:layout_
android:layout_
android:text="Name"
android:layout_toRightOf="@+id/image"
android:layout_centerVertical="true"
android:layout_marginStart="8dp"/>
<ImageView
android:id="@+id/selected"
android:layout_
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_
android:visibility="gone"
android:src="@drawable/ic_selected"
android:layout_marginEnd="30dp"/>
</RelativeLayout>
在 MainActivity 中,我可以获得所选项目的位置。但我无法将值传递给适配器以实现我的结果。
public class MainActivity extends AppCompatActivity implements CustomSpinner.OnSpinnerEventsListener
private CustomSpinner spinner_fruits;
private FruitAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner_fruits = findViewById(R.id.spinner_fruits);
spinner_fruits.setSpinnerEventsListener(this);
adapter = new FruitAdapter(MainActivity.this, Data.getFruitList());
spinner_fruits.setAdapter(adapter);
@Override
public void onPopupWindowOpened(Spinner spinner)
spinner_fruits.setBackground(getResources().getDrawable(R.drawable.bg_spinner_fruit_up));
@Override
public void onPopupWindowClosed(Spinner spinner)
spinner_fruits.setBackground(getResources().getDrawable(R.drawable.bg_spinner_fruit));
和xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity">
<com.example.customspinners.CustomSpinner
android:id="@+id/spinner_fruits"
android:layout_
android:layout_
android:dropDownVerticalOffset="40dp"
android:layout_margin="16dp"
android:background="@drawable/bg_spinner_fruit"/>
</RelativeLayout>
【问题讨论】:
你能添加代码成为可重现的代码吗?使用所有必要的代码来帮助我们运行您的代码?或者github链接 更新了代码 【参考方案1】:您可以在 Adapter 类中创建接口。当您的项目单击时,您会通过调用其方法来触发它。你可以通过实现这个接口在你的activity类中使用它
public class FruitAdapter extends BaseAdapter
private Context context;
private List<Fruit> fruitList;
private OnClickListener onClick;
public FruitAdapter(Context context, List<Fruit> fruitList, OnClickListener onClick)
this.context = context;
this.fruitList = fruitList;
this.onClick= onClick;
@Override
public int getCount()
return fruitList != null ? fruitList.size() : 0;
@Override
public Object getItem(int i)
return i;
@Override
public long getItemId(int i)
return i;
@Override
public View getView(int i, View view, ViewGroup viewGroup)
View rootView = LayoutInflater.from(context)
.inflate(R.layout.item_fruit, viewGroup, false);
TextView txtName = rootView.findViewById(R.id.name);
ImageView image = rootView.findViewById(R.id.image);
ImageView selected = rootView.findViewById(R.id.selected);
txtName.setText(fruitList.get(i).getName());
image.setImageResource(fruitList.get(i).getImage());
rootView.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
onClick.onClick();
);
return rootView;
interface OnClickListener
void onClick();
public class MainActivity extends AppCompatActivity implements CustomSpinner.OnSpinnerEventsListener, FruitAdapter.OnClickListener
private CustomSpinner spinner_fruits;
private FruitAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner_fruits = findViewById(R.id.spinner_fruits);
spinner_fruits.setSpinnerEventsListener(this);
adapter = new FruitAdapter(MainActivity.this, Data.getFruitList());
spinner_fruits.setAdapter(adapter);
@Override
public void onPopupWindowOpened(Spinner spinner)
spinner_fruits.setBackground(getResources().getDrawable(R.drawable.bg_spinner_fruit_up));
@Override
public void onPopupWindowClosed(Spinner spinner)
spinner_fruits.setBackground(getResources().getDrawable(R.drawable.bg_spinner_fruit));
@Override
public void onClick()
【讨论】:
了解,但我想在从未处于活动状态的适配器中选择时将可见性设置为 VISIBLE以上是关于如何将图像视图的可见性设置为 Spinner 所选项目的可见性的主要内容,如果未能解决你的问题,请参考以下文章