将抽象视图添加到布局中,并以编程方式确定特定类



>我有一个带有片段查看页器的应用程序。

每个片段要么是 A 型,包含 5 个CheckBoxes的列表,要么是 B 型,包含 5 个Radiobuttons

目前,我有一个AbstractFragment,它有 2 个子类FragmentAFragmentB,我设置了一个带有列表或另一个的布局。

我想简化这一点,只有一个Fragment类,列表为 5 CompoundButton ,如果CompoundButtonCheckBox还是Radiobutton,则以编程方式设置。

既然CompoundButton是抽象的,我怎么能实现这样的事情呢?

PS :当然,以编程方式添加按钮不是我想要做的,因为布局有点复杂,设置它会很乏味。

public class ComponentFragment extends Fragment {
    private ComponentFragmentMode mode = ComponentFragmentMode.CHECKBOX;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (mode == ComponentFragmentMode.CHECKBOX) {
            return inflater.inflate(R.layout.checkbox_layout, container, false);
        } else {
            return inflater.inflate(R.layout.radiobutton_layout, container, false);
        }
}

最新更新