如何在ViewPager2中实现微调

时间:2019-09-10 04:40:02

标签: android android-viewpager android-spinner

如何为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");
}

我希望在选择新的微调器项目后在日志中看到“更改了微调器”条目。

1 个答案:

答案 0 :(得分:1)

根据您所说的话,

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

  

您尝试过

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