我该如何关闭片段?

时间:2015-03-17 08:32:43

标签: android android-fragments

我对活动有片段。片段有按钮。如果我点击按钮,片段必须关闭。我是怎么做到的?

public class ItemFragment extends Fragment{

    private ImageView btnApply;
    private ClickButton clickButton = new ClickButton();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.item_info, container, false);
        btnApply = (ImageView) rootView.findViewById(R.id.btnSendItem);
        btnApply.setOnClickListener(clickButton);
        return rootView;
    }

    private class ClickButton implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (R.id.btnSendItem == v.getId()) {
                Toast.makeText(getActivity(),"CLOSE",Toast.LENGTH_LONG).show();
                return;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:8)

没有关闭片段之类的东西,但你可以从堆栈中删除片段。要弹出片段,请使用以下内部按钮单击侦听器

getActivity().getFragmentManager().beginTransaction().remove(this).commit();