如何通过按钮在片段之间切换

时间:2016-10-24 18:58:53

标签: android git android-fragments github

我正在构建一个应用程序。我将我的概览屏幕作为片段持有者,我想在其他屏幕上使用片段:组/聊天/搜索/设置。 以下是我的OverviewActivity

public class Overview extends AppCompatActivity implements View.OnClickListener{
    ImageButton btn_search, btn_groups, btn_chat, btn_settings;
    android.app.FragmentManager manager = getFragmentManager();
    android.app.FragmentTransaction transaction = manager.beginTransaction();
    SettingsFrg frg = new SettingsFrg();
    ChatFrg chfrg = new ChatFrg();
    GrouFrg grpfrg = new GrouFrg(); 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overview);
        loadUI();
        transaction.add(R.id.frg_holder,frg,"SRCHFRG");

    }

    private void loadSettingsFrg() {
        transaction.add(R.id.frg_holder,frg, "SRCHFRG");
        transaction.addToBackStack(null);
        transaction.commit();
    }

    private void loadUI() {
        btn_chat = (ImageButton) findViewById(R.id.chatBtn);
        btn_search = (ImageButton)findViewById(R.id.searchBtn);
        btn_settings = (ImageButton)findViewById(R.id.settingsBtn);
        btn_groups = (ImageButton) findViewById(R.id.groupsBtn);
        btn_chat.setOnClickListener(this);
        btn_groups.setOnClickListener(this);
        btn_settings.setOnClickListener(this);
        btn_search.setOnClickListener(this);

    }


    public void onClick(View view){
    switch (view.getId()){
        case R.id.chatBtn:
            loadChatFrg();
            Toast.makeText(Overview.this, "go to chat", Toast.LENGTH_SHORT).show();
            break;
        case R.id.searchBtn:
            loadSettingsFrg();
            Toast.makeText(Overview.this, "go to search", Toast.LENGTH_SHORT).show();
            break;
        case R.id.settingsBtn:
            Toast.makeText(Overview.this, "go to settings", Toast.LENGTH_SHORT).show();
            break;
        case R.id.groupsBtn:
            loadGrouFrg();
            Toast.makeText(Overview.this, "go to groups", Toast.LENGTH_SHORT).show();
            break;

    }

    }

    private void loadGrouFrg() {

        transaction.replace(R.id.frg_holder,grpfrg,"GRPFRG");
        transaction.addToBackStack(null);
        transaction.commit();
    }

    private void loadChatFrg() {
        transaction.replace(R.id.frg_holder,chfrg,"CHTFRG");
        transaction.addToBackStack(null);
        transaction.commit();
    }
}

到目前为止,我只能加载一个片段,每当我尝试替换它时,应用程序崩溃了。 链接到git

1 个答案:

答案 0 :(得分:0)

尝试使用此功能切换片段

public void switchFragment(final Fragment fragment, final String tag) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (fragment != null) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.frg_holder, fragment, tag).addToBackStack(null).commitAllowingStateLoss();
            }
        }
    });
}