切换标签时保持片段状态

时间:2018-06-06 10:11:42

标签: java android android-fragments

我有4个标签A,B,C和D.我想保留以前的标签视图。例如:标签A(默认) - > B - > A.返回标签A时,我希望标签A的视图与之前的视图相同

Graph: Fragment A (or B or C or D) on top of 4 tabs (A, B, C, D)

这是我的代码:

function arrayToTable(tableData) {
var myTable = $('<table></table>');
$(tableData).each(function (i, rowData) {
    var row = $('<tr class="rownum-' + [i] +'"></tr>');
    $(rowData).each(function (j, cellData) {
       // change here
       // add conditional statement here. 
       var cell = '<td>'+cellData+'</td>';
       if (i === 0) {
         cell = '<th>'+cellData+'</th>';
       }
        row.append($(cell));
    });
    myTable.append(row);
});
 return myTable;
}

标签A从标签B切换到标签时继续重新创建

我知道另一种方法是使用private void replaceFragment(Constants.BottomTab bottomTab) { Fragment fragment = getSupportFragmentManager().findFragmentByTag(bottomTab.getValue()); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if (fragment == null) { switch (bottomTab) { case A: fragment = new A(); break; case B: fragment = new B(); break; case C: fragment = new C(); break; case D: fragment = new D(); break; } transaction.replace(R.id.main_container, fragment,bottomTab.getValue() ); transaction.addToBackStack(bottomTab.getValue()); // Tag transaction.commit(); Log.v(">>>", "Create fragment: " + fragment.getTag()); } else { // pop from stack transaction.replace(R.id.main_container, fragment, bottomTab.getValue()); transaction.commit(); Log.v(">>>", "Found fragment: " + fragment.getTag()); } } ViewPager,但上面代码的解决方案是什么?

有人可以帮忙吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

作为简单的解决方案,我建议您保留对这些片段的引用,并仅在它们为空时创建它们。所以它可能是这样的:

private Fragment aFrag;
private Fragment bFrag;
private Fragment cFrag;
private Fragment dFrag;

private void replaceFragment(Constants.BottomTab bottomTab) {
    Fragment fragment = getSupportFragmentManager().findFragmentByTag(bottomTab.getValue());
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if (fragment == null) {
        switch (bottomTab) {
            case A:
                if (aFrag == null) aFrag = new A();
                fragment = aFrag;
                break;
            case B:
                if (bFrag == null) bFrag = new B();
                fragment = bFrag;
                break;
            case C:
                if (cFrag == null) cFrag = new C();
                fragment = cFrag;
                break;
            case D:
                if (dFrag == null) dFrag = new D();
                fragment = dFrag;
                break;
        }
        transaction.replace(R.id.main_container, fragment,bottomTab.getValue() );
        transaction.addToBackStack(bottomTab.getValue()); // Tag
        transaction.commit();
        Log.v(">>>", "Create fragment: " + fragment.getTag());
    } else { // pop from stack
        transaction.replace(R.id.main_container, fragment, bottomTab.getValue());
        transaction.commit();
        Log.v(">>>", "Found fragment: " + fragment.getTag());
    }
}