片段首次被选中时会“覆盖”另一个片段

时间:2019-05-08 13:21:59

标签: java android android-fragments

我有一个底部导航,可以在3个片段之间进行切换:“ ConnectFragment”,“ DashboardFragment”和“ ChatFragment”。

从“连接”切换为“聊天”,反之亦然,但是,当我选择“仪表板”时,会导致一个错误,导致在导航中选择“聊天”时出现“仪表板”,这是什么原因造成的?

所有3个片段都具有相同的功能和布局,因此我认为问题出在MainActivity。

MainActivity:

public class MainActivity extends AppCompatActivity implements DashboardFragment.FragmentDashListener, ChatFragment.FragmentChatListener, ConnectFragment.FragmentConnListener {

    FragmentManager fm = getSupportFragmentManager();
    Fragment active;

    Fragment FragmentConnect = new ConnectFragment();
    Fragment FragmentDashboard = new DashboardFragment();
    Fragment FragmentChat = new ChatFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);


        fm.beginTransaction().add(R.id.fragment_container, FragmentChat).hide(FragmentChat).commit();        //Ustvari vse 3 fragmente, skrije 2 da se nena vedno znova kreirajo
        fm.beginTransaction().add(R.id.fragment_container, FragmentDashboard).hide(FragmentDashboard).commit();
        fm.beginTransaction().add(R.id.fragment_container, FragmentConnect).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            active = FragmentConnect;

            switch (menuItem.getItemId()) {
                case R.id.nav_connect:
                    fm.beginTransaction().hide(active).show(FragmentConnect).commit();
                    active = FragmentConnect;
                    return true;

                case R.id.nav_dashboard:
                    fm.beginTransaction().hide(active).show(FragmentDashboard).commit();
                    active = FragmentDashboard;
                    return true;

                case R.id.nav_send:
                    fm.beginTransaction().hide(active).show(FragmentChat).commit();
                    active = FragmentChat;
                    return true;
            }
            return false;
        }
    };

    @Override
    public void onInputChatSent(CharSequence input) {
        ConnectFragment.updateEditText(input);
    }

    @Override
    public void onInputConnSent(CharSequence input) {
        DashboardFragment.updateEditText(input);
    }

    @Override
    public void onInputDashSent(CharSequence input) {
        ChatFragment.updateEditText(input);
    }

}

片段:

所有3个片段的代码都相同,以下分别是“仪表板”和“聊天”。

public class DashboardFragment extends Fragment {

    private FragmentDashListener listener;
    private static EditText editText;
    private Button ButtonOk;

    public interface FragmentDashListener{
        void onInputDashSent (CharSequence input);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_dashboard, container, false);
        editText = v.findViewById(R.id.edit_text);
        ButtonOk = v.findViewById(R.id.Button_Ok);
        ButtonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CharSequence input = editText.getText();
                listener.onInputDashSent(input);
            }
        });
        return v;
    }

    public static void updateEditText(CharSequence newText){
        editText.setText(newText);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(context instanceof FragmentDashListener){
            listener = (FragmentDashListener) context;
        }   else {
            throw new RuntimeException(context.toString()+"must implement FragmentDashListener");
        }
    }
}
public class ChatFragment extends Fragment {

    private FragmentChatListener listener;
    private static EditText editText;
    private Button ButtonOk;

    public interface FragmentChatListener{
        void onInputChatSent (CharSequence input);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_chat, container, false);
        editText = v.findViewById(R.id.edit_text);
        ButtonOk = v.findViewById(R.id.Button_Ok);
        ButtonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CharSequence input = editText.getText();
                listener.onInputChatSent(input);
            }
        });
        return v;
    }

    public static void updateEditText(CharSequence newText){
        editText.setText(newText);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(context instanceof FragmentChatListener){
            listener = (FragmentChatListener) context;
        }   else {
            throw new RuntimeException(context.toString()+"must implement FragmentChatListener");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

删除第一行

active = FragmentConnect;

来自onNavigationItemSelected方法。这将解决问题

或如下修改

if(active == null) {
    active = FragmentConnect;
}