【问题标题】:Android - Fragment inside a fragment - Preserve stateAndroid - 片段内的片段 - 保留状态
【发布时间】:2013-02-04 12:32:36
【问题描述】:

我正在尝试在滑动视图片段(名为 BROWSE)中添加标签片段。当访问 BROWSE 时,与远程服务器的连接完成,数据被检索并显示在选项卡内的 gridview 中,当单击不同的选项卡时,建立新连接,并且 gridview 填充新数据。

问题是我无法保存选项卡的状态,我不想每次都建立连接,只有在第一次单击选项卡时才建立与数据库的连接,如果单击选项卡再次,我希望 gridview 保留状态。

这里是BROWSE片段的onCreate方法,在这个方法中,我正在初始化一个加载器来从远程服务器获取数据:

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




            if (t == 0) {
            Log.v("BROWSE","++++++++ Inside onCreate t = 0 ++++++++");

            tweets = Collections.emptyList();
            // This is our REST action.

            Uri twitterSearchUri = new Uri.Builder()
            .scheme("http")
            .authority(ip)
            .path("browse.php")
            .appendQueryParameter("SpecialOffer", "0")
            .build();

                Bundle params = new Bundle();
            //    params.putString("q", "android");
                params.get("salim slem");
                // These are the loader arguments. They are stored in a Bundle because
                // LoaderManager will maintain the state of our Loaders for us and
                // reload the Loader if necessary. This is the whole reason why
                // we have even bothered to implement RESTLoader.
                Bundle args = new Bundle();
                args.putParcelable(ARGS_URI, twitterSearchUri);
                args.putParcelable(ARGS_PARAMS, params);


            //For loading Images    
            //--------------------------------------------------    

                options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.stub_image)
                .showImageForEmptyUri(R.drawable.image_for_empty_url)
                .cacheInMemory()
                .cacheOnDisc()
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();


            //--------------------------------------------------    



                // Initialize the Loader.
                getSupportLoaderManager().initLoader(LOADER_TWITTER_SEARCH, args, this);

            }

            if(t == 1) {

                Log.v("BROWSE","++++++++ Inside onCreate  t = 1 ++++++++");

                tweets = Collections.emptyList();
                // This is our REST action.

                Uri twitterSearchUri = new Uri.Builder()
                .scheme("http")
                .authority(ip)
                .path("browse.php")
                .appendQueryParameter("SpecialOffer", "1")
                .build();

                    Bundle params = new Bundle();
                //    params.putString("q", "android");
                    params.get("salim slem");
                    // These are the loader arguments. They are stored in a Bundle because
                    // LoaderManager will maintain the state of our Loaders for us and
                    // reload the Loader if necessary. This is the whole reason why
                    // we have even bothered to implement RESTLoader.
                    Bundle args = new Bundle();
                    args.putParcelable(TAB1_ARGS_URI, twitterSearchUri);
                    args.putParcelable(TAB1_ARGS_PARAMS, params);


                //For loading Images    
                //--------------------------------------------------    

                    options = new DisplayImageOptions.Builder()
                    .showStubImage(R.drawable.stub_image)
                    .showImageForEmptyUri(R.drawable.image_for_empty_url)
                    .cacheInMemory()
                    .cacheOnDisc()
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .build();

                //--------------------------------------------------    

                    // Initialize the Loader.
                    getSupportLoaderManager().initLoader(TAB1_LOADER_TWITTER_SEARCH, args, this);
                }
        }

这里是 onCreateView 方法,其中选项卡与网格视图一起创建

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        Log.v("BROWSE", "Inside OnCreateView");

        View view = inflater.inflate(R.layout.gridview, container, false);

        gridView = (GridView) view.findViewById(R.id.gridViewImage);

        gridView.setAdapter(new ImageTextAdapter(view.getContext(), LOADING));

        // When an item in the gridview is clicked
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                FragmentPosition = 2;
                Log.v("BROWSE: POSITION OF ITEM IN GRIDVIEW","## "+position+" ##");
                DialogFragment newFragment = new BrowseDialogFragment(position);
                newFragment.show(getSupportFragmentManager(), "missiles");

            }
        });

        // creating tabs
        mTabHost = (TabHost) view.findViewById(R.id.testtabhost1); 
        mTabHost.setup();
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.id.gridViewImage)); 
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.gridViewImage)); 
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.gridViewImage)); 
        mTabHost.setCurrentTab(0); 



        mTabHost.setOnTabChangedListener(new OnTabChangeListener() {


            @Override
            public void onTabChanged(String tabId) {

            int i = mTabHost.getCurrentTab();
             Log.i("@@@@@@@@ ANN CLICK TAB NUMBER", "------" + i);

             if (i ==0) {
                 Bundle tempBundle = new Bundle();
                 t = 0;
                onCreate(tempBundle);
        }

             if (i ==1) {
                 Bundle tempBundle = new Bundle();     
                 t = 1;
                 onCreate(tempBundle);
                }

              }
            });

        return view;
    }

我想我的问题是我不能将“savedInstanceState”从 onCreateView 提供给 onCreate。我的问题是,如何在选项卡中保存状态?也许使用提供给 onCreate 的包,但我还没有找到怎么做!

提前感谢您的帮助!!

这就是我说的 UI:

这就是再次按下选项卡时的 UI:

【问题讨论】:

    标签: android android-fragments android-viewpager android-tabs


    【解决方案1】:

    尝试在这种情况下使用数据库。很容易意识到: tutorial

    那么你应该怎么做:

    1. 获取远程数据
    2. 将其保存在 SQLite DB 中
    3. 创建一个方法来检查数据库中数据的存在时间。
    4. 例如,如果数据超过 10 分钟,则应获取远程数据并更新表。

    我希望这会有所帮助。

    最好的问候

    野生动物园

    【讨论】:

      【解决方案2】:

      将 savedInstanceState 变量传递给 onViewCreated 方法,该方法在 onCreateView 之后立即调用。

      您应该尝试仅在 onCreateView(gridView、mTabHost 等)中扩展您的布局并初始化您的 View 字段,并在 onViewCreated 中恢复状态(或设置默认状态)

      【讨论】:

        猜你喜欢
        • 2018-01-21
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 2013-04-04
        • 2014-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多