如何管理片段之间的事务

时间:2013-01-11 12:37:13

标签: android

在我的活动中,我有两个片段在右边,一个在左边。在某些时候我需要删除一个并添加另一个。但是当我删除左边时,那个未被移除的片段扩展并且第二个是添加在右边而不是左边。我会给你看一个屏幕截图,这样你就可以理解我的意思。我怎样才能阻止蓝色片段改变位置? 我使用了rightFrag.setRetainInstance(true); 但它不会工作。谢谢你的时间。

enter image description here

  public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {

private final Handler handler = new Handler();
private RoundedColourFragment leftFrag1;
private RoundedColourFragment leftFrag2;
private RoundedColourFragment leftFrag3;
private RoundedColourFragment leftFrag4;
private RoundedColourFragment currentFragment =null;
private RightFragment rightFrag;
private boolean useLogo = false;
private boolean showHomeUp = false;
private FragmentTransaction ft;
private static boolean onLoad=true;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ActionBar ab = getSupportActionBar();

    // set defaults for logo & home up
    ab.setDisplayHomeAsUpEnabled(showHomeUp);
    ab.setDisplayUseLogoEnabled(useLogo);

    // set up tabs nav
    for (int i = 1; i <= 4; i++) {
        ab.addTab(ab.newTab().setTabListener(this).setText("Tab "+ i));
    }


    // set up list nav
    ab.setListNavigationCallbacks(ArrayAdapter
            .createFromResource(this, R.array.sections,
                    R.layout.sherlock_spinner_dropdown_item),
            new OnNavigationListener() {
                public boolean onNavigationItemSelected(int itemPosition,
                        long itemId) {
                    // FIXME add proper implementation
                    rotateLeftFrag();
                    return false;
                }
            });

    // default to tab navigation
    showTabsNav();

    // create a couple of simple fragments as placeholders
    final int MARGIN = 16;
    leftFrag1 = new RoundedColourFragment(getResources().getColor(
            R.color.abs__bright_foreground_holo_dark), 1f, MARGIN, MARGIN / 2, MARGIN, MARGIN,R.layout.tab1);
    leftFrag2 = new RoundedColourFragment(getResources().getColor(
            R.color.abs__bright_foreground_holo_dark), 4f, MARGIN, MARGIN / 2, MARGIN, MARGIN,R.layout.tab2);
    leftFrag3 = new RoundedColourFragment(getResources().getColor(
            R.color.abs__bright_foreground_holo_dark), 1f, MARGIN, MARGIN / 2, MARGIN, MARGIN,R.layout.tab3);
    leftFrag4 = new RoundedColourFragment(getResources().getColor(
            R.color.abs__bright_foreground_holo_dark), 1f, MARGIN, MARGIN / 2, MARGIN, MARGIN,R.layout.tab4);

    rightFrag = new RightFragment(getResources().getColor(
            R.color.honeycombish_blue), 2f, MARGIN / 2, MARGIN, MARGIN,
            MARGIN);

    ft = getSupportFragmentManager().beginTransaction();

    ft.add(R.id.root, leftFrag1);


   ft.add(R.id.root, rightFrag);


    ft.commit();


    onLoad=false;
    currentFragment=leftFrag1;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main_menu, menu);

    // set up a listener for the refresh item
    final MenuItem refresh = (MenuItem) menu.findItem(R.id.menu_refresh);
    refresh.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        // on selecting show progress spinner for 1s
        public boolean onMenuItemClick(MenuItem item) {
            // item.setActionView(R.layout.progress_action);
            handler.postDelayed(new Runnable() {
                public void run() {
                    refresh.setActionView(null);
                }
            }, 1000);
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // TODO handle clicking the app icon/logo
        return false;
    case R.id.menu_refresh:
        // switch to a progress animation
        item.setActionView(R.layout.indeterminate_progress_action);
        return true;
    case R.id.menu_both:
        // rotation animation of green fragment
        rotateLeftFrag();
        return true;
    case R.id.menu_text:
        // alpha animation of blue fragment
        ObjectAnimator alpha = ObjectAnimator.ofFloat(rightFrag.getView(),
                "alpha", 1f, 0f);
        alpha.setRepeatMode(ObjectAnimator.REVERSE);
        alpha.setRepeatCount(1);
        alpha.setDuration(800);
        alpha.start();
        return true;
    case R.id.menu_logo:
        useLogo = !useLogo;
        item.setChecked(useLogo);
        getSupportActionBar().setDisplayUseLogoEnabled(useLogo);
        return true;
    case R.id.menu_up:
        showHomeUp = !showHomeUp;
        item.setChecked(showHomeUp);
        getSupportActionBar().setDisplayHomeAsUpEnabled(showHomeUp);
        return true;
    case R.id.menu_nav_tabs:
        item.setChecked(true);
        showTabsNav();
        return true;
    case R.id.menu_nav_label:
        item.setChecked(true);
        showStandardNav();
        return true;
    case R.id.menu_nav_drop_down:
        item.setChecked(true);
        showDropDownNav();
        return true;
    case R.id.menu_bak_none:
        item.setChecked(true);
        getSupportActionBar().setBackgroundDrawable(null);
        return true;
    case R.id.menu_bak_gradient:
        item.setChecked(true);
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ad_action_bar_gradient_bak));
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

private void rotateLeftFrag() {
    if (currentFragment!= null) {
        ObjectAnimator.ofFloat(currentFragment.getView(), "rotationY",0,360).setDuration(500).start();

    }
}

private void showStandardNav() {
    ActionBar ab = getSupportActionBar();
    if (ab.getNavigationMode() != ActionBar.NAVIGATION_MODE_STANDARD) {
        ab.setDisplayShowTitleEnabled(true);
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    }
}

private void showDropDownNav() {
    ActionBar ab = getSupportActionBar();
    if (ab.getNavigationMode() != ActionBar.NAVIGATION_MODE_LIST) {
        ab.setDisplayShowTitleEnabled(false);
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    }
}

private void showTabsNav() {
    ActionBar ab = getSupportActionBar();
    if (ab.getNavigationMode() != ActionBar.NAVIGATION_MODE_TABS) {
        ab.setDisplayShowTitleEnabled(false);
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    }
}

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    // FIXME add a proper implementation, for now just rotate the left
    // fragment

    System.out.println(tab.getPosition());

    if(!onLoad){

        ft = getSupportFragmentManager().beginTransaction();
        ft.remove(currentFragment);

        if(tab.getPosition()==0){
            ft.add(R.id.root, leftFrag1);
            currentFragment=leftFrag1;
        }else if(tab.getPosition()==1){
            ft.add(R.id.root, leftFrag2);
            currentFragment=leftFrag2;
        }else if(tab.getPosition()==2){
            ft.add(R.id.root, leftFrag3);
            currentFragment=leftFrag3;
        }else{
            ft.add(R.id.root, leftFrag4);
            currentFragment=leftFrag4;
        }


        ft.commit();
       // rotateLeftFrag();

    }
}

public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    // FIXME implement this
}

public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    // FIXME implement this
}

}

我还有另外两个与片段有关的课程,如果你认为我应该发布它们,请告诉我。

0 个答案:

没有答案