TabActivity的片段中的Switch Case无法正常工作

时间:2019-06-28 05:13:26

标签: android swipe tabactivity

我正在处理tabactivity的片段,但是遇到一个问题,例如当我滑动tab片段时,tab1的相同UI会出现在tab2上,即Switch大小写在占位符类中无法正常工作,而Tab1和Tab2具有不同的含义用户界面。 请帮助我,我是android的新手。

 public View onCreateView(
        @NonNull LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main_tab, container, false);


    View root = null;
    switch (1) {
        case 1:
            root = inflater.inflate(R.layout.fragment_main_tab, container, false);
            break;
        case 2:
            root = inflater.inflate(R.layout.fragment_team_screen_, container, false);
            break;
    }

    Next = (ImageButton) rootView.findViewById(R.id.btn_expand);
    Next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            updatedetails();
        }
    });

    return rootView;
}

2 个答案:

答案 0 :(得分:1)

因为您在swtich语句中犯了错误

switch (1) { // 1 for all the condition
        case 1:
            root = inflater.inflate(R.layout.fragment_main_tab, container, false);
            break;
        case 2:
            root = inflater.inflate(R.layout.fragment_team_screen_, container, false);
            break;
    }

您需要像这样通过此处

switch (position) { // here position of the fragment
        case 1:
            root = inflater.inflate(R.layout.fragment_main_tab, container, false);
            break;
        case 2:
            root = inflater.inflate(R.layout.fragment_team_screen_, container, false);
            break;
    }

答案 1 :(得分:0)

您需要传递制表符位置

switch (tabPosition) { // here position of the fragment to show
    case 1:
        // your code logic
        break;
    case 2:
        // your code logic
        break;
}