不会触发自定义微调器setOnItemSelectedListener

时间:2018-05-15 03:20:54

标签: android android-layout android-spinner android-adapter

我有一个微调器和一个适配器。

Spinner spinner = findViewById(R.id.spinner);
SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.spinner_item, getList());
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
    {
        Log.e("PROGRAM", "Selected |" + position + "|");
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView)
    {
    }
});

这是我的SpinnerAdapter下拉列表创建

@Override
public View getDropDownView(final int position, final View convertView, final ViewGroup parent)
{
    MyObject object = getItem(position);

    View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_object_item, parent, false);
    ((TextView) layout.findViewById(R.id.name)).setText(object.getName());

    return layout;
}

my_object_item xml是

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/item"
              android:clickable="true"
              android:focusable="true">

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/icon"/>

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"/>

</LinearLayout>

由于某种原因,我无法触发setOnItemSelectedListener。我应该在my_object_item的每个组件中添加监听器,并从那里触发setOnItemSelectedListener吗?

1 个答案:

答案 0 :(得分:1)

如上所述,您可以尝试手动执行操作,方法是添加一个侦听器,该侦听器侦听TextView或ImageView上的点击(旋转器下拉视图中每个项目的组件),然后更新微调器视图根据所选择的内容。

1)MainActivity.class:------------

public class MainActivity extends AppCompatActivity implements Listener {

private Spinner sp;
private SpinnerAdapter spinnerAdapter;

private String[] name = {
        "N1",
        "N2",
        "N3",
        "N4"
};

private int[] icon = {
        R.drawable.icon,
        R.drawable.icon,
        R.drawable.icon,
        R.drawable.icon
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    sp = (Spinner) findViewById(R.id.sp);
    spinnerAdapter = new SpinnerAdapter(getApplicationContext() , name , icon , name[0] , icon[0] , MainActivity.this); // TODO: Every time the activity (spinner) is created, the item selected is changed
    //TODO to the initial name[0] and icon[0]. In order to solve this issue you can save the selected item (name and icon) value in sharedPreferences and use the saved value every time the spinner is opened.
    sp.setAdapter(spinnerAdapter);
}

@Override
public void clicked(int position) {
    if(spinnerAdapter != null){
        spinnerAdapter.setCurrentNameIcon(name[position] , icon[position]);
    }
}

2)SpinnerAdapter.class:---------------

public class SpinnerAdapter extends BaseAdapter {

private Context mContext;
private LayoutInflater layoutInflater;
private Listener callback;
private String current_name;
private int current_icon;

private String[] name;
private int[] icon;


public SpinnerAdapter(@NonNull Context context , String[] name_ , int[] icon_ , String inital_name_ , int initial_icon_ , Listener l) {

    mContext = context;
    layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(l != null){
        callback = l;
    }

    name = name_;
    icon = icon_;

    current_name = inital_name_;
    current_icon = initial_icon_;

}

public int getCount() {
    return name.length;
}

public Object getItem(int position) {
    return name[position];
}

public long getItemId(int position) {
    return position;
}

@NonNull
public View getView(final int position, View convertView, ViewGroup parent) {

    View view = convertView;
    final TextView tv;
    ImageView iv;

    if (convertView == null) {
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.my_object_item, parent , false);
        }
    }

    tv = (TextView) view.findViewById(R.id.name);
    tv.setText(current_name);


    iv = (ImageView) view.findViewById(R.id.icon);
    iv.setBackground(mContext.getResources().getDrawable(current_icon));


    return view;
}

@Override
public View getDropDownView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {

    View view = convertView;
    final TextView tv;
    ImageView iv;

    if (convertView == null) {
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.my_object_item, parent , false);
        }
    }

    tv = (TextView) view.findViewById(R.id.name);
    tv.setText(name[position]);
    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(callback != null){

                //https://stackoverflow.com/questions/7287195/android-spinner-close?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
                View root = parent.getRootView();
                root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
                root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

                callback.clicked(position);
            }
        }
    });


    iv = (ImageView) view.findViewById(R.id.icon);
    iv.setBackground(mContext.getResources().getDrawable(icon[position]));
    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(callback != null){

                //https://stackoverflow.com/questions/7287195/android-spinner-close?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
                View root = parent.getRootView();
                root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
                root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

                callback.clicked(position);
            }
        }
    });

    return view;

}


public void setCurrentNameIcon(String name , int icon){
    current_name = name;
    current_icon = icon;
    this.notifyDataSetChanged();
}
}

3)监听器界面:------------

public interface Listener {

public void clicked(int position);
}

4)main_activity.xml:---------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/sp">
</Spinner>

</android.support.constraint.ConstraintLayout>

5)my_object_item.xml:----------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/item"
android:clickable="true"
android:focusable="true">

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/icon"
    android:layout_gravity="center_vertical"
    android:id="@+id/icon"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    android:text="HI"
    android:id="@+id/name"/>

</LinearLayout>