刷卡时Viewpager非常慢

时间:2016-04-21 10:59:05

标签: android android-fragments android-viewpager

我有一个ViewPager,其中包含我生成的几个Fragments并提供了一个独特的布局。这些Fragments仅仅是用户不应该与之交互的静态布局的占位符。 (与预览页面类似)。

以下是包含FragmentsViewPager的活动:

public class KidPreviewActivity extends FragmentActivity implements IKidPreviewView, PreviewFragment.OnFragmentInteractionListener {

private KidPreviewPresenter mPresenter;
private ViewPager mPager;
private PreviewSlidePagerAdapter mPagerAdapter;
private static final int NUM_PAGES = 6;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kid_preview);

    // hide navigation bar
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);


    Button returnToLauncherBtn = (Button) findViewById(R.id.kidPreviewReturnToLauncherBtn);
    EditText emailET = (EditText) findViewById(R.id.kidPreviewParentEmail);

    mPresenter = new KidPreviewPresenter(getApplicationContext());
    mPresenter.attachView(this);

    returnToLauncherBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPresenter.backToLauncher();
        }
    });
    mPager = (ViewPager) findViewById(R.id.previewPager);
    mPagerAdapter = new PreviewSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    CircleIndicator indicator = (CircleIndicator) findViewById(R.id.cIndicator);
    indicator.setViewPager(mPager);

} //-onCreate

@Override
protected void onDestroy() {
    super.onDestroy();
    if (isFinishing()) {
        mPresenter.destroy();
    }
}


@Override
public void onError(Message msg) {
    Utils.DisplayError(this, msg);
}

@Override
public void onDisconnected() {
    ActivityHelper.handleDisconnected(this);
}


@Override
public void backToLauncher() {
    finish();
}

@Override
public void notifyParents() {

}

@Override
public void onFragmentInteraction(Uri uri) {

}

private class PreviewSlidePagerAdapter extends FragmentStatePagerAdapter {
    public PreviewSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        PreviewFragment currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_one);
        switch (position) {
            case 0:
                currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_one);
                break;
            case 1:
                currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_two);
                break;
            case 2:
                currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_three);
                break;
            case 3:
                currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_four);
                break;
            case 4:
                currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_five);
                break;
            case 5:
                currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_six);
                break;
        }
        return currentFrag;
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}

但是,在加载此活动时,我在滑动/滑动viewpager项目时会非常慢。

使用以下消息:

 04-21 06:56:13.238 1897-1897/com.myApp.myAppDev I/Choreographer: Skipped 97 frames!  The application may be doing too much work on its main thread.
04-21 06:56:14.933 1897-2281/com.myApp.myAppDev E/OpenGLRenderer: GL error:  Out of memory!

有什么方法可以解决这个问题/优化这个以便它不会锁定活动吗?

我不确定为什么它在主线程上做了“太多工作”,据我所知,我在主线程上没有做太多工作?

Fragment newInstance

   public static PreviewFragment newInstance(int layoutSelected){
        PreviewFragment fragment = new PreviewFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_LAYOUT, layoutSelected);
        fragment.setArguments(args);
        return fragment;
    }

其他PreviewFragment方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        layoutSelected = getArguments().getInt(ARG_LAYOUT);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(layoutSelected, container, false);


}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

2 个答案:

答案 0 :(得分:0)

您似乎每次都在创建片段(在getItem函数中)。 相反,您希望使用片段列表创建适配器,将它们存储在适配器中,并更改getItem以从该列表返回片段。

private class PreviewSlidePagerAdapter extends FragmentStatePagerAdapter {
  private List<Fragment>  mFragments;
  public PreviewSlidePagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    mFragments = fragments;
  }

  @Override
  public Fragment getItem(int position) {
    if (mFragments == null) {
      return (null);
    }
    return mFragments.get(position);
  }
}

并从MainActivity创建适配器:

List<Fragment> fragmentsList = new ArrayList<>();
fragmentsList.add(PreviewFragment.newInstance(R.layout.kid_preview_flipper_one));
            fragmentsList.add(PreviewFragment.newInstance(R.layout.kid_preview_flipper_two));
            fragmentsList.add(PreviewFragment.newInstance(R.layout.kid_preview_flipper_three));
            fragmentsList.add(PreviewFragment.newInstance(R.layout.kid_preview_flipper_four));
            fragmentsList.add(PreviewFragment.newInstance(R.layout.kid_preview_flipper_five));
            fragmentsList.add(PreviewFragment.newInstance(R.layout.kid_preview_flipper_six));
mPagerAdapter = new PreviewSlidePagerAdapter(getSupportFragmentManager(), fragmentsList);
mPager.setAdapter(mPagerAdapter);

答案 1 :(得分:0)

尝试像这样更改你的适配器,它将保存一个调用newInstance

 public ActionResult Download()
        {
            var filepath = @"\\Netdrive\Uploads\somefile.txt";
            byte[] filedata = System.IO.File.ReadAllBytes(filepath);
            string contentType = MimeMapping.GetMimeMapping(filepath);
            var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = filepath,
                Inline = false,
            };
            Response.AppendHeader("Content-Disposition", cd.ToString());
            return File(filedata, contentType);
        }

如果您发布newInstance方法代码,那么它将非常有用。我认为他们在UI线程中做了很多工作

<强>更新

正如@ user3505534所说,尝试将父类从 @Override public Fragment getItem(int position) { PreviewFragment currentFrag = null; switch (position) { case 1: currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_two); break; case 2: currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_three); break; case 3: currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_four); break; case 4: currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_five); break; case 5: currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_six); break; default: currentFrag = PreviewFragment.newInstance(R.layout.kid_preview_flipper_one); } return currentFrag; } 更改为FragmentStatePagerAdapter