Android sherlock FragmentTab清除数据

时间:2013-08-05 05:42:42

标签: android android-fragments actionbarsherlock android-viewpager

public class PropertyAddActivity extends SherlockFragmentActivity implements OnPageChangeListener, TabListener {
private String TAG="AddActivity: ";
private FileCache fileCache;
private ViewPager mPager;
private ActionBar ab;
private static final int COUNT = 3;
static ArrayList<Integer> mSelectedPropertyType = new ArrayList<Integer>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.property_add);

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    mPager.setOnPageChangeListener(this);
    ab = getSupportActionBar();

    ab.setDisplayShowTitleEnabled(false);
    ab.setDisplayShowHomeEnabled(false);
    ab.setNavigationMode(NAVIGATION_MODE_TABS);

    GlobalProperty.getInstance().product=new PropertyAdd();

    ab.addTab(ab.newTab().setText("Property").setTabListener(this));
    ab.addTab(ab.newTab().setText("Property Detail 1").setTabListener(this));
    ab.addTab(ab.newTab().setText("Property Detail 2").setTabListener(this));

}       

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    //listAttachImage.clear();
    mSelectedPropertyType.clear();
    super.onDestroy();
}

@Override
public void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) {
    //AppLog.logString(TAG+"onPageScrolled");
}

@Override
public void onPageSelected(int position) {
    //AppLog.logString(TAG+"onPageSelected position"+position);
    getSupportActionBar().setSelectedNavigationItem(position);
}

@Override
public void onPageScrollStateChanged(int state) {
    //AppLog.logString(TAG+"onPageScrollStateChanged ");
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    //AppLog.logString(TAG+"onTabSelected position: "+tab.getPosition());
    mPager.setCurrentItem(tab.getPosition());
    //ft.replace(R.id.fragment_container, fragment);
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    //AppLog.logString(TAG+"onTabUnselected");
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    //AppLog.logString(TAG+"onTabReselected");
}

public class MyAdapter extends FragmentStatePagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        //AppLog.logString(TAG+"COUNT: "+COUNT);
        return COUNT;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment f = new Fragment();
        AppLog.logString(TAG+"position: "+position);
        switch (position) {
        case 0:
            AppLog.logString(TAG+"FRAGGGG1111111");
            f = Property1AddFragmentActivity.newInstance(position);
            break;
        case 1:
            AppLog.logString(TAG+"FRAGGGG2222222");
            f = Property2AddFragmentActivity.newInstance(position);
            break;
        case 2:
            AppLog.logString(TAG+"FRAGGGGG333333");
            f = Property3AddFragmentActivity.newInstance(position);
            break;


        default:
            AppLog.logString(TAG+"Default");
            break;
        }
        return f;
    }
}}

我添加了添加3选项卡现在的问题是,当我在tab1视图中设置一些数据并移动到tab2时,重新选择tab1,tab1上的数据是原样但当我移动到tab3到tab1时,tab1数据是清楚的。你有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

目前,您的代码似乎是部分操作栏标签和部分普通片段。

如果您尝试使用操作栏标签,则需要标签监听器。在onTabSelected中你应该添加/替换,在onTabUnselected中你应该删除。

请参阅http://developer.android.com/guide/topics/ui/actionbar.html “添加导航标签”部分是一个很好的例子。