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

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

问题描述

在一个片段一个被改变到另一个片段B,它会调用的onCreate()和其他一些功能做初始化。在片段已经改变了,当前片段附近的片段也将被初始化。例如:我有5个片段A,B,C,D,E的当前片段是A.当我更改为D,D以及C和E将被初始化。我的问题是:

有些codeS的执行速度很慢,所以我想初始化的数据,而不是所有的数据的一部分。即,当所述片段是可见的,那么一些数据将被初始化。当其不是当前片段,初始化将被延迟。这里是一个事先知情同意:

我使用这些类:

  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 的内容片段。如果可以的话,我会打电话给我自己的函数来完成初始化。

  1. 我怎样才能知道这个片段是隐藏或它当前的?或
  2. 我能在TabHost的内容片段?或
  3. 的其他方式延迟初始化。

下面是一些code:

 公共类TabsAdapter扩展FragmentPagerAdapter
          实现TabHost.OnTabChangeListener,
                     ViewPager.OnPageChangeListener {
        私人最终语境mContext;
        私人最终TabHost mTab​​Host;
        私人最终ViewPager mViewPager;
        私人最终的ArrayList< TabInfo> mTabs =新的ArrayList< TabInfo>();

        私有静态final类TabInfo {
            私人最终类别<> mClss;
            私人最终捆绑mArgs;

            TabInfo(字符串_tag,类<> _class,捆绑_args){
                mClss = _class;
                mArgs = _args;
            }
        }
        公共TabsAdapter(FragmentActivity活动,TabHost tabHost,ViewPager寻呼机){
            超级(activity.getSupportFragmentManager());
            mContext =活动;
            mTabHost = tabHost;
            mViewPager =寻呼机;
            mTabHost.setOnTabChangedListener(本);
            mViewPager.setAdapter(本);
            mViewPager.setOnPageChangeListener(本);
        }

        / ** MainActivity电话本功能使添加标签* /
        公共无效addTab(TabHost.TabSpec则tabspec,类<> CLSS,捆绑参数){
            一些code
        }

        公共无效onPageSelected(INT位置){
            TabWidget插件= mTab​​Host.getTabWidget();
            INT oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(位置);
            widget.setDescendantFocusability(oldFocusability);
            Log.i(演示片,onPageSelected Tab切换到---------+位置);
        }
    }
 

MainActivity: / **这些code是创建片段* /

 的FrameLayout tabContact =(的FrameLayout)LayoutInflater.from(本).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);
 

/ **这些code是把碎片进入TabHost * /

  mTab​​sAdapter.addTab(mTabHost.newTabSpec(XXX)的A.class,NULL);
 

解决方案
  

我的问题是:C $ CS约$速度很慢,所以我想初始化的一部分   的DATAS,而不是全部。

我希望你不这样做重操作的主UI线程。

  

我怎么能知道的片段是隐藏或它当前的?或者,我可以   得到TabHost的内容片段?或其他方式来延迟   初始化。

您可以使用关于 FragmentPagerAdapter 一招。在你的 onPageSelected 方法,你可以找出哪些是当前片段键,就可以调用初始化方法:

 公共无效onPageSelected(INT位置){
    // ...
    片段项= getSupportFragmentManager()findFragmentByTag(机器人:切换:+ R.id.theIdOfTheViewPager +:+位置)。
    如果(项目= NULL和放大器;!&安培;!item.getView()= NULL){
        item.initializeStuffForThisFragment(); //你需要将它转换为你的片段类
    }
}
 

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:

I use these classes:

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

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. How can I know the fragment is hide or its the current one? OR
  2. Can I get the content fragment in TabHost? OR
  3. Other ways to delay the initialization.

Here is some code:

    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);

/** 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.

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

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.

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中使用片段时,延迟初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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