在主要活动中调用片段

时间:2018-11-01 13:11:41

标签: android android-fragments

我为每个类别设置了不同的片段集,并希望在从抽屉中选择它们时显示它们

private static final String ARG_POSITION = "position";

    private int position;
    private LinearLayout layout;
    private TextView icon;

    public static WizardUniversalFragment newInstance(int position) {
        WizardUniversalFragment f = new WizardUniversalFragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        return f;
    }

我该如何解决?

1 个答案:

答案 0 :(得分:1)

  

您通过每个片段的位置来调用它们。

     

从您的代码中,我了解您的职位声明   参数使用它来调用每个片段

 @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            position = getArguments().getInt(your_pos);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_wizard_universal,
                    container, false);
            layout = (LinearLayout) rootView
                    .findViewById(R.id.Your_fragment);
            icon = (TextView) rootView
                    .findViewById(R.id.Your_fragment_icon);

            if (position == 0) {
                layout.setBackgroundColor(ContextCompat.getColor(getContext(),
                        R.color.#));
                layout.invalidate();
                icon.setText(R.string.#);
            } 

            ViewCompat.setElevation(rootView, 50);
            return rootView;
        }
  

设置它,您就可以开始了