[英]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.