0

我尝试了 Jake Wharton 的 ViewPagerIndicator示例。我的应用程序是这样的:

Activity1 开始时像:

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

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

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

我尝试了 mAdapter.notifyDataSetChanged() 但没有得到预期的结果。如何解决我的问题?Extractly, it not fetch data correct on fragment when select tabpage. 示例:选择标签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>{
    }

}
4

0 回答 0