FragmentStatePagerAdapter具有未知数量的片段的性能问题

时间:2014-04-08 21:00:26

标签: android android-fragments android-viewpager

很长一段时间以来,我一直在寻找可靠的答案来解决我的表现问题。但是,就我看到每个人都对此感到困惑而无法找到并回答。

基本上,我在一个活动中有一个新闻列表(在列表视图中显示为头条新闻),在我的详细活动中有一个新闻对象的详细信息。我已经更改了detail活动以保存扩展FragmentStatePagerAdapter的viewpager。在我的主要标题活动中,我通常有20/25的新闻。

当用户点击单个列表视图项时,它会将新闻列表传递给下一个活动:

Intent intent = new Intent(getApplicationContext(), Detail.class);
bundle.putInt("pos", i);
intent.putExtra("news", newsList);
intent.putExtras(bundle);
startActivity(intent);

我的详细活动从标题活动中获取捆绑数据并准备适配器:

fm = getSupportFragmentManager();
news = (ArrayList) getIntent().getSerializableExtra("news");
current = getIntent().getExtras().getInt("pos");
fragments2 = new ArrayList<Fragment>();

vpPager = (ViewPager) findViewById(R.id.vpPager);

adapterViewPager = new ViewFlipperAdapter(current, getApplicationContext(), fm, news, handler, version());
vpPager.setAdapter(adapterViewPager);
vpPager.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

vpPager.setCurrentItem(current);
vpPager.setOnPageChangeListener(Detail.this);

我的viewpager适配器:

public class ViewFlipperAdapter extends FragmentStatePagerAdapter {
    private List<News> news;
    private Handler handler;
    private boolean version;
    private Context context;
    private int current;

    public ViewFlipperAdapter(int current, Context context, FragmentManager fm, List<News> news,
                              Handler handler, boolean version) {
        super(fm);
        this.news = news;
        this.handler = handler;
        this.version = version;
        this.context = context;
        this.current = current;
    }

    @Override
    public Fragment getItem(int position) {
        return DetailFragment.newInstance(current, context, news.get(position), handler, version);
    }

    @Override
    public int getCount() {
        return this.news.size();
    }
}

最后我的片段:

public class DetailFragment extends Fragment implements View.OnClickListener {

    public DetailFragment() {
    }

    //view initialization
    boolean version;
    String detailTitle;
    String detailSpot;
    String detailImage;
    String detailText;
    String video;
    String url;
    //get holder
    ViewHolder holder = new ViewHolder();

    // newInstance constructor for creating fragment with arguments
    public static DetailFragment newInstance(int current, Context c, News n, Handler h, boolean version) {
        DetailFragment fragmentFirst = new DetailFragment();
        handler = h;
        cont = c;
        // Supply num input as an argument.
        Bundle args = new Bundle();
        //initalise everything related to a news object inside bundle.
        args.putString(Statics.detailTitle, n.getTitle());
        args.putString(Statics.detailSpot, n.getSpot());
        args.putString(Statics.detail_image_inner, n.getImage());
        args.putString(Statics.playButton, n.getVideo());
        args.putString(Statics.detailText, n.getText());
        args.putString(Statics.category_detail, n.getCategory());
        args.putString(Statics.button_fb, n.getWeb());
        args.putBoolean(Statics.version, version);
        args.putInt(Statics.current, current);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }
// Inflate the view for the fragment based on layout XML
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.details, container, false);

        //adview related.
        containerWidth = cont.getResources().getDisplayMetrics().widthPixels;
        ((WindowManager) cont.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
        containerWidth = cont.getResources().getDisplayMetrics().widthPixels;
        width = dm.widthPixels;


        holder.detailTitle = (TextView) view.findViewById(R.id.detailTitle);
        holder.detailSpot = (TextView) view.findViewById(R.id.detailSpot);
        holder.detailText = (TextView) view.findViewById(R.id.detailText);
        holder.progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        holder.detailImage = (ImageView) view.findViewById(R.id.detailImageInner);
        holder.detailImage.setAdjustViewBounds(true);
        holder.playImage = (ImageView) view.findViewById(R.id.playButton);
        holder.progressBar2 = (ProgressBar) view.findViewById(R.id.progressBar2);
        holder.shareButton = (Button) view.findViewById(R.id.buttonFB);
        holder.playImage.setOnClickListener(this);

        //some setters for image and textviews...

        return view;
    }

问题是,当我点击单个列表项时,它会显示单个新闻项的详细信息。直到这里我没有看到任何错误。但是当我在视图之间滑动时,应用程序一次又一次地处理视图变得非常慢。此外,我跳过的页面不会保留我设置的数据。 (向后滑动到上一个视图会导致重新加载视图。)

我怎样才能克服这一点? (顺便说一下,我使用fedorvlasov的延迟加载lib和dfp sdk,它们是针对每个视图单独初始化的。)

-PS。我尝试过ViewPager.setOffScreenPageLimit(),但效果不高。

提前致谢。

0 个答案:

没有答案