从最近几天开始,我试图让 Spinner 在 ViewGroup 中工作但没有任何成功。:-{。在自定义 ViewGroup 中,我放置了片段容器。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:boxMenu="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity"
android:id="@+id/frame" >
<com.imper.boxmenulibrary.BoxMenuLayout
android:id="@+id/box_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
boxMenu:boxSize="40dp"
boxMenu:directionY="bottomToTop"
boxMenu:directionX="rightToLeft"
boxMenu:showStart="false"
android:padding="5dp"
android:background="#ffffff00">
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:orientation="vertical">
</LinearLayout>
</com.imper.boxmenulibrary.BoxMenuLayout>
在那个片段容器中,我像这样替换片段:
FragmentManager fm = ((Activity) context).getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new TestFragment());
ft.commit();
我的 TestFragment xml 布局如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/test_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
TestFragment 实现如下所示:
public class TestFragment extends BoxLayout implements OnItemSelectedListener {
Spinner testSpinner;
public TestFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
View view = inflater.inflate(R.layout.test_view, container, false);
testSpinner = (Spinner) view.findViewById(R.id.test_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(container.getContext(), R.array.day, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
testSpinner.setAdapter(adapter);
testSpinner.setOnItemSelectedListener(this);
return view;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getActivity(), "Bingo", Toast.LENGTH_SHORT).show();
Log.d("Bingo", "onItemSelected");
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(getActivity(), "Bingo Nothing", Toast.LENGTH_SHORT).show();
Log.d("Bingo", "onNothingSelected");
}
}
现在,问题是当我尝试从 Spinner 下拉列表中选择某些内容时,没有选择任何内容,根本不会触发 OnItemSelected 事件。
当 FragmentContainer 从我的自定义 ViewGroup 中移出时,相同的代码工作正常。为什么会这样???如何让 Spinner 在自定义视图组中正常工作?