=== 根本原因 ===
onItemSelected 工作正常,对于我在下面描述的问题,原因是我只是将一些代码分支到 ArrayAdapter.notifyDataSetInvalidated()。给我的教训是:
*当 AdapterView 出现“无响应”时,检查适配器。
感谢给我任何建议的朋友。
====================
我想在片段中有一个微调器,但是 onItemSelected 不起作用,请告诉我原因,非常感谢。
这是活动布局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>
连接器片段
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) {
}
});
}
}
ConnectorsFragment 的布局
<LinearLayout>
<Spinner
android:id="@+id/spinnerConnectors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
</LinearLayout>
活动
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
}
}
求大神帮忙,谢谢!