如何使用viewpagerindicator创建片段并不是重内存?

时间:2013-12-27 10:48:02

标签: android android-fragments viewpagerindicator

请帮帮我,我的应用程序有6个片段,来自服务器的自定义列表视图。在启动时,此活动看起来所有片段都在加载。这可能是造成沉重记忆的原因。那么如何制作片段viewpagerindicator并不是重内存?

这是我的代码定义viewpager:

@Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.layout_viewpager_tab);

        adapter = new ViewAdapterEvent(getApplicationContext(), getSupportFragmentManager(), page_titles);

        vp = (ViewPager)findViewById(R.id.pager);

    vp.setOffscreenPageLimit(6);
        vp.setAdapter(adapter);

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

       // ContentActivity.varkategoritab= page_titles[vp.getCurrentItem()];

        mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {                     

            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

这是我的代码适配器viewpager:

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class ViewAdapter extends FragmentPagerAdapter{

    private Context _context;
    String[] page_titles;

    public ViewAdapter(Context context, FragmentManager fm, String[] page_title){
        super(fm);
        _context = context;
        this.page_titles = page_title;
    }



    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub

        Fragment f = new Fragment();

        switch(position){
        case 0:
            f = ContentActivity.newInstance(_context);
            break;

        case 1:
            f = ContentActivity.newInstance(_context);
            break;
        case 2:
            f = ContentActivity.newInstance(_context);
            break;
             case 3:
            f = ContentActivity.newInstance(_context);
            break;
        case 4:
            f = ContentActivity.newInstance(_context);
            break;
                    case 5:
            f = ContentActivity.newInstance(_context);
            break;
        }

        return f;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return page_titles.length;
    }

    public CharSequence getPageTitle(int position) {
        return page_titles[position];
    }
}

0 个答案:

没有答案