简体   繁体   English

片段中包含onItemSelected时不触发

[英]onItemSelected not trigged when it is included in a fragment

=== Root Reason === ===根本原因===

onItemSelected WORKS FINE, for issue I described below, the reason is I just branches some code to ArrayAdapter.notifyDataSetInvalidated(). onItemSelected的方法不错,对于下面我要描述的问题,原因是我只是将一些代码分支到ArrayAdapter.notifyDataSetInvalidated()。 The lesson to me is: 给我的教训是:

* When "No response" happens on AdapterView, check the adapter. *当AdapterView上发生“无响应”时,请检查适配器。

Thanks to the friends who gave me any advice. 感谢给我任何建议的朋友。

=================== ===================

I want to have a spinner in a fragment, but the onItemSelected does not work, could you please tell me the reason, thanks a lot. 我想在片段中放入微调框,但是onItemSelected无法正常工作,请您告诉我原因,非常感谢。

Here is the activity layout xml: 这是活动布局xml:

<LinearLayout>
 <!-- onItemSelected works fine when the spinnerConnector defined in activity layout -->
 <Spinner
    android:id="@+id/spinnerConnectors"
    android:spinnerMode="dropdown" />
<!-- move spinnerConnectors into this fragment will cause the problem - onItemSelected not work
<fragment
    android:id="@+id/fragmentConnectors"
    android:name="com.sharework.transproxy.android.ConnectorsFragment"
    tools:layout="@layout/list_fragment1" />
-->
</LinearLayout>

ConnectorsFragment 连接器片段

public class ConnectorsFragment extends android.app.Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
                // create adapter ...
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.connectors_fragment, null);
    Spinner spinnerConnectors = (Spinner) view
            .findViewById(R.id.spinnerConnectors);
    spinnerConnectors.setAdapter(adapter);
    return view;
}

@Override
public void onActivityCreated(Bundle bundle) {
    super.onActivityCreated(bundle);
    Spinner spinnerConnectors = (Spinner) getView().findViewById(
            R.id.spinnerConnectors);
            spinnerConnectors
            .setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // **** code to handle onItemSelected but not trigged
}

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

} }

layout of ConnectorsFragment 连接器的布局

<LinearLayout>

<Spinner
    android:id="@+id/spinnerConnectors"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dropdown" />

</LinearLayout>

Activity 活动

public class FactoryMonitorActivity TransProxyActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_factory_monitor);
    // invoke ConnectorsFragment.start to query for connectors and fill into the adapter
      }
}

Coule anyone help, thanks! 可以帮助任何人,谢谢!

Remove everything related to the spinner from the Activity (maybe you missed to delete something during your test) and then make spinnerConnectors a class variable of the fragment. 从“活动”中删除与微调器相关的所有内容(也许您在测试期间错过了删除操作),然后使spinnerConnectors成为该片段的类变量。 So you have to find for the view just one time (this operation has an impact on performances) and also next times you use the spinner you don't have to declare a local variable within a method. 因此,您只需要为视图查找一次(此操作会影响性能),并且下次您使用微调器时,不必在方法内声明局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 回到 Fragment 时,Android Spinner 的 onItemSelected 执行两次 - Android Spinner's onItemSelected executed twice when going back to Fragment 如何在Fragment中重写OnItemSelected方法 - How to override OnItemSelected method in Fragment Android Spinner onItemSelected未在Fragment中调用 - Android Spinner onItemSelected not being called inside Fragment 从未调用片段微调器onItemSelected方法 - Fragment spinner onItemSelected method never called 如何在Android中的微调器onItemSelected上刷新片段 - How to refresh fragment on spinner onItemSelected in android 当包含为片段时,视图之间的布局约束不保留 - Layout constraint between Views not preserved when included as Fragment 当我包含首选项标头时,无法在首选项片段中启动活动 - Unable to start activity in preference fragment when I included preference headers 当活动中包含多个片段时,RecyclerViewClickListener onClick 在片段中不起作用 - RecyclerViewClickListener onClick not working in fragment when several fragments included in an activity 当活动开始时,android onItemSelected会触发它自己 - android onItemSelected triggers itself when activity starts Android 11 - 按下主页按钮时触发 foregroundService 的 onTaskRemoved - Android 11 - foregroundService's onTaskRemoved is trigged when home button pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM