Android中使用Fragment时延迟初始化 [英] Delay initialization when using Fragment in Android

查看:44
本文介绍了Android中使用Fragment时延迟初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个片段 A 更改为另一个片段 B 时,它会调用 onCreate() 和其他一些函数来进行初始化.Fragment 改变后,当前片段附近的片段也会被初始化.例如:我有 5 个片段 A、B、C、D、E.当前片段是 A.当我更改为 D 时,D 连同 C 和 E 将被初始化.我的问题是:

When one fragment A is changed to another fragment B, it will call onCreate() and some other functions to do initialization. After the Fragment has changed, the fragments near the current fragment also will be initialized. For example: I have 5 fragments A, B, C, D, E. The current fragment is A. When I change to D, D along with C and E will be initialized. My Problem is:

有些代码执行起来很慢,所以我想初始化一部分数据而不是所有数据.也就是说,当片段可见时,就会初始化一些数据.当它不是当前片段时,初始化将被延迟.这是一张照片:

Some codes are very slow to execute so I want to initialize a part of the data instead of ALL of the data. That is, when the fragment is visible, then some data will be initialized. When its not the current fragment, the initialization will be delayed. Here is a pic:

我使用这些类:

android.widget.TabHost
android.support.v4.app.Fragment
android.support.v4.app.FragmentActivity
android.support.v4.app.FragmentPagerAdapter
android.support.v4.view.ViewPager

我阅读了 API,但在片段可见时没有调用任何方法.所以,我想我可以用 onTabChanged 做一些事情.但我不知道如何通过TabHost 获取内容片段.如果可以的话,我会调用自己的函数进行初始化.

I read the API but there is no method called when the fragment is VISIBLE. So, I think I can do something with onTabChanged. But I don't know how to get the content fragment through TabHost. If I can, then I will call my own function to do initialization.

  1. 我如何知道片段是隐藏的还是当前的片段?或
  2. 我可以在 TabHost 中获取内容片段吗?或
  3. 其他延迟初始化的方法.

这是一些代码:

    public class TabsAdapter extends FragmentPagerAdapter
          implements TabHost.OnTabChangeListener,
                     ViewPager.OnPageChangeListener{
        private final Context mContext;
        private final TabHost mTabHost;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        private static final class TabInfo {
            private final Class<?> mClss;
            private final Bundle mArgs;

            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                mClss = _class;
                mArgs = _args;
            }
        }
        public TabsAdapter(FragmentActivity activity, TabHost tabHost,     ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mTabHost = tabHost;
            mViewPager = pager;
            mTabHost.setOnTabChangedListener(this);
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        /** MainActivity call this func to add tabs */
        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            some code
        }

        public void onPageSelected(int position) {
            TabWidget widget = mTabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(position);
            widget.setDescendantFocusability(oldFocusability);
            Log.i("demo tab","onPageSelected tab change to---------   " + position);
        }
    }

主要活动:/** 这些代码是用来创建片段的 */

MainActivity: /** these code is to create fragment */

        FrameLayout tabContact = (FrameLayout) LayoutInflater.from(this).inflate(R.layout.tab_widget, null);
        ImageView imgBackContact = (ImageView) tabContact.findViewById(R.id.tab_widget_back_image);
        ImageView imgFrontContact = (ImageView) tabContact.findViewById(R.id.tab_widget_front_image);
        imgBackContact.setImageResource(R.drawable.ic_tab_contact_normal);
        imgFrontContact.setImageResource(R.drawable.ic_tab_contact_selected);

/** 这些代码就是把片段放入TabHost */

/** these code is to put fragments into TabHost */

mTabsAdapter.addTab(mTabHost.newTabSpec(XXX), A.class, null);

推荐答案

我的问题是:有些代码很慢所以我想初始化一个部分数据而不是全部.

My Problem Is: some codes are very slow so I want to initialize a part of the datas instead of ALL of them.

希望您不要在主 UI 线程上进行繁重的操作.

I hope you're not doing heavy operations on the main UI thread.

我怎么知道片段是隐藏的还是当前的片段?或者我可以吗在 TabHost 中获取内容片段?或 其他延迟初始化.

How can I know the fragment is hide or its the current one? OR Can I get the content fragment in TabHost? OR Other ways to delay the initialization.

您可以使用有关 FragmentPagerAdapter 的技巧.在您的 onPageSelected 方法中,您可以找出当前的 Fragment 并对其调用初始化方法:

You could use a trick regarding the FragmentPagerAdapter. In your onPageSelected method you could find out which is the current Fragment and call an initialization method on it:

public void onPageSelected(int position) {
    //...
    Fragment item = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.theIdOfTheViewPager + ":" + position);
    if (item != null && item.getView() != null) {
        item.initializeStuffForThisFragment();// you need to cast it to your Fragment class 
    }
}

这篇关于Android中使用Fragment时延迟初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆