【问题标题】:How to set visibility of imageview to VISIBLE of the item selected of Spinner如何将图像视图的可见性设置为 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_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链接
  • 更新了代码

标签: android android-spinner


【解决方案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
猜你喜欢
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 2014-08-21
  • 2013-12-07
  • 2017-05-24
  • 1970-01-01
相关资源
最近更新 更多