【发布时间】: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_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/orange"
android:layout_marginStart="8dp"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:layout_toRightOf="@+id/image"
android:layout_centerVertical="true"
android:layout_marginStart="8dp"/>
<ImageView
android:id="@+id/selected"
android:layout_width="16dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_height="16dp"
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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.customspinners.CustomSpinner
android:id="@+id/spinner_fruits"
android:layout_width="match_parent"
android:layout_height="40dp"
android:dropDownVerticalOffset="40dp"
android:layout_margin="16dp"
android:background="@drawable/bg_spinner_fruit"/>
</RelativeLayout>
【问题讨论】:
-
你能添加代码成为可重现的代码吗?使用所有必要的代码来帮助我们运行您的代码?或者github链接
-
更新了代码