我正在使用一个片段来显示一个带有一些图像的 GridView 和一个添加新图像的按钮。GridView 工作正常,但我的 Button 的 onClick 方法从未被调用。
这是我的 Fragment 的 onCreateView 方法:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_picture_grid_view, container, false);
addPictureButton = (Button) rootView.findViewById(R.id.fragment_grid_view_add_button);
addPictureButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CameraOrAlbumDialogFragment dialogFragment = new CameraOrAlbumDialogFragment();
dialogFragment.show(mContext.getSupportFragmentManager(), "CameraPicker");
}
});
imageGridView = (GridView) rootView.findViewById(R.id.fragment_grid_view);
imageGridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
}
在我的 onActivityCreated 方法中,我为我的 GridView 做了所有的适配器工作,它可以正常工作,它可以捕获我的点击和 longClicks。唯一不起作用的是 Button 的 onCLick 方法。当我触摸它时,我可以看到 Button 图标发生变化,但我的 onClickListener 方法中的函数从未被调用。有什么帮助吗?
编辑
这是我的片段布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/fragment_grid_view"
android:layout_width="match_parent"
android:layout_height="150dp"
android:numColumns="auto_fit" >
</GridView>
<Button
android:id="@+id/fragment_grid_view_add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Add Picture" />
</LinearLayout>
编辑2
原来有另一个按钮与我的按钮重叠,因此该按钮正在接收 onClick 事件而不是我的按钮。当我在 Eclipse 中使用层次结构视图时,我能够弄清楚。非常有用。