TabPage更改时重新加载片段

时间:2012-07-31 05:29:52

标签: android android-fragments

我尝试Jake Wharton's ViewPagerIndicator样本。 我的应用就是这样:

开始时的活动1:

|------------------------
|Page1  Page2  Page3  Pa|
|-------_____-----------|
|                       |
|                       |
|     fragment 2        |
|                       |
|                       |

这里选择了Page2。 标签页标题已从表格行动态加载。 示例我启动Acitivity2为Acitivity1添加更多标签页。但我这里有问题。 标签页加载正常,片段加载不正确。这就像:

|------------------------
|Page0  Page1  Page2  Pa|
|_____------------------|
|                       |
|                       |
|     fragment 1        |
|                       |
|                       |

我尝试了mAdapter.notifyDataSetChanged()但没有得到预期的结果。 如何解决我的问题? 实际上,当选择tabpage时,它不会在片段上获取正确的数据。示例:select tab Page0,所有数据必须是fragment0,这里是fragment1。

这是我的代码

public class WeatherActivity extends FragmentActivity {

    Cursor cursor, cursor2,cursorMyLocation;
    SQLiteDatabase dh;
    LocationsCustomCursorAdapter myCursorAdapter;
    static int hasMyLocation;

    String strLocation;
    ArrayList<String> arrayLocations;
    static ArrayList<String> arrayLocationReq;      

    LocationsTabsFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;
    TabPageIndicator mTabPage;

    static String[] CONTENT;
    static String[] CONTENT2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidContext.setContext(this);
        dh=DatabaseHelper.getInstance().getDb();
        getLocation();

        setContentView(R.layout.activity_weather);

        mAdapter = new MyLocationsTabsAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.viewPager);
        mPager.setAdapter(mAdapter);

        mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);   
    }

    class MyLocationsTabsAdapter extends LocationsTabsFragmentAdapter {
        public MyLocationsTabsAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return LocationsTabsFragment.newInstance(WeatherActivity.CONTENT2[position % WeatherActivity.CONTENT2.length]);
        }

        @Override
        public int getCount() {
            return WeatherActivity.CONTENT.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return WeatherActivity.CONTENT[position % WeatherActivity.CONTENT.length];
        }

    }

    // NOTE: String[] CONTENT,CONTENT2 = getLocation();
    public void getLocation (){
        ...
    }

    // Method mAdapter.notifyDataSetChanged() not working. 
    // Only mIndicator.notifyDataSetChanged() work fine.

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        // TODO Auto-generated method stub

        super.onActivityResult(arg0, arg1, arg2);

        if(arg0==2){
            getLocation();
            mIndicator.notifyDataSetChanged();
            mAdapter.notifyDataSetChanged();

        }
    }  
}

public class LocationsTabsFragment extends Fragment {

    private String mContent = "";

    private static final String KEY_CONTENT = "LocationsTabsFragment:Content";

    public static LocationsTabsFragment newInstance(String location) {

        LocationsTabsFragment fragment = new LocationsTabsFragment();
        StringBuilder builder = new StringBuilder();
        builder.append(location);
        fragment.mContent = builder.toString();
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        new GetWeather().execute();
        v = inflater.inflate(R.layout.weather, container, false);

        return v;
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT, mContent);
    }


    // working with server and fetch data on fragment.
    public class GetWeather extends AsyncTask<Void, Boolean, Weather>{
    }

}

0 个答案:

没有答案