0

我正在使用一个片段来显示一个带有一些图像的 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 中使用层次结构视图时,我能够弄清楚。非常有用。

4

3 回答 3

1

尝试这个。

public class CarCheckFrameFragment extends Fragment implements View.OnClickListener  {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View rootView = inflater.inflate(R.layout.fragment_car_check_frame, container, false);

        Button button = rootView.findViewById(R.id.button);
        button.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.button:
                doSomething();
                break;
        }
    }
}
于 2014-04-18T02:45:43.707 回答
1

我想我有另一个 View 与我的按钮重叠,这就是它从未被调用的原因。

如此愚蠢!对不起。

于 2014-04-30T00:08:41.563 回答
0

getChildFragmentManager()在其他片段中使用片段时应该使用。

于 2014-03-26T15:32:00.297 回答