简体   繁体   English

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

[英]Fragment “overwrites” another fragment when first selected

I have Bottom Navigation with which I switch between 3 fragments: "ConnectFragment", "DashboardFragment" and "ChatFragment". 我有底部导航,可以在3个片段之间进行切换:“ ConnectFragment”,“ DashboardFragment”和“ ChatFragment”。

Switching from Connect to Chat and vice versa works OK, but when I select Dashboard it causes a bug that makes Dashboard appear when selecting Chat in navigation, what is causing this? 从“连接”切换为“聊天”,反之亦然,但是,当我选择“仪表板”时,会导致一个导致在导航中选择“聊天”时出现“仪表板”的错误,这是什么原因引起的?

All 3 fragments have identical functionality and layout, so i assume the problem lays in MainActivity. 所有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);
    }

}

Fragments: 片段:

All 3 fragments have identical code, below are Dashboard and Chat. 所有3个片段的代码都相同,以下分别是Dashboard和Chat。

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");
        }
    }
}

Remove the first line 删除第一行

active = FragmentConnect;

from onNavigationItemSelected method. 从onNavigationItemSelected方法。 This will fix the issue 这将解决问题

or modify it as below 或如下修改

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当其他片段更改第一个片段模型时刷新片段 - Refresh fragment when other fragment changes the first's fragment model 当我转到另一个片段时片段重复 - Fragment duplicates when I go to another Fragment 当替换为第二个片段时,第一个片段上的按钮移出屏幕 - Button on first fragment moves out of screen when replaced with second fragment 在viewpager中选择时更改片段尺寸 - Change fragment dimensions when selected in viewpager 在另一个片段中更改数据时如何刷新一个片段中的RecyclerView - How to refresh RecyclerView in one fragment when data changed in another fragment 当 SQLite 数据库在另一个 Fragment 中更新时更新 Fragment 中的 ListView - Update ListView in Fragment when SQLite database is updated in another Fragment 单击将片段更改为另一个按钮时,片段不会进行交易 - fragment wont transaction when click on the button which change the fragment to another 单击对话框时,在另一个片段上方替换一个片段(viewpager) - Replace a fragment above another fragment when dialog is clicked (viewpager) 使用导航切换到另一个片段时保存片段数据 - Save Fragment data when switch to another fragment using Navigation 从android(Java)中的另一个片段切换回来时恢复片段 - Resume fragment when switching back from a another fragment in android(Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM