如何在 ViewPager2 中实现 Spinner

How to implement a Spinner in ViewPager2

如何在 ViewPager2 中为微调器设置 onItemSelected

我已经在 ViewHolder() 中设置了微调器,但不知道如何实现 @Override onItemSelected

ViewPager2

ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewHolder> {
    Context mContext;

    ViewPagerAdapter(Context context) {
        this.mContext = context;
    }

ViewHolder

public class ViewHolder extends RecyclerViewViewHolder {
    private Spinner mSpinner;

    ViewHolder(View itemView) {
        mSpinner = itemView.findViewById(R.id.mySpinner);
        ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(mContext, R.array.myArray, android.R.layout.simple_spinner_item);
        spinnerAdapter.setDropDownViewResrouce(android.R.layout.simple_spinner_dropdown_item);
       mSpinner.setAdapter(spinnerAdapter);
       mSpinner.setOnItemSelectedListener((AdapterView.OnItemSelectedListener)mContext);

OnItemSelected ##

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
   Log.d(LOG_TAG, "Spinner Changed");
}

我希望在选择新的微调项后在日志中看到 "Spinner Changed" 条目。

据你所说,

尝试 运行 应用程序时出现以下错误,错误:方法未覆盖或实现超类型的方法。

Have you tried

mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
   Log.d(LOG_TAG, "Spinner Changed");
}
});