<small id='C0ADY'></small><noframes id='C0ADY'>

          <bdo id='C0ADY'></bdo><ul id='C0ADY'></ul>

        <tfoot id='C0ADY'></tfoot>

      1. <i id='C0ADY'><tr id='C0ADY'><dt id='C0ADY'><q id='C0ADY'><span id='C0ADY'><b id='C0ADY'><form id='C0ADY'><ins id='C0ADY'></ins><ul id='C0ADY'></ul><sub id='C0ADY'></sub></form><legend id='C0ADY'></legend><bdo id='C0ADY'><pre id='C0ADY'><center id='C0ADY'></center></pre></bdo></b><th id='C0ADY'></th></span></q></dt></tr></i><div id='C0ADY'><tfoot id='C0ADY'></tfoot><dl id='C0ADY'><fieldset id='C0ADY'></fieldset></dl></div>
      2. <legend id='C0ADY'><style id='C0ADY'><dir id='C0ADY'><q id='C0ADY'></q></dir></style></legend>

        在恢复活动时刷新 FragmentStatePagerAdapter 上的图像

        时间:2023-10-04
      3. <tfoot id='H32th'></tfoot>
        <legend id='H32th'><style id='H32th'><dir id='H32th'><q id='H32th'></q></dir></style></legend>

        <small id='H32th'></small><noframes id='H32th'>

              <tbody id='H32th'></tbody>
            <i id='H32th'><tr id='H32th'><dt id='H32th'><q id='H32th'><span id='H32th'><b id='H32th'><form id='H32th'><ins id='H32th'></ins><ul id='H32th'></ul><sub id='H32th'></sub></form><legend id='H32th'></legend><bdo id='H32th'><pre id='H32th'><center id='H32th'></center></pre></bdo></b><th id='H32th'></th></span></q></dt></tr></i><div id='H32th'><tfoot id='H32th'></tfoot><dl id='H32th'><fieldset id='H32th'></fieldset></dl></div>
              • <bdo id='H32th'></bdo><ul id='H32th'></ul>
                1. 本文介绍了在恢复活动时刷新 FragmentStatePagerAdapter 上的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我创建了一个使用 FragmentStatePagerAdapter 提供小型画廊的活动.但是,当活动恢复时(例如,从其他活动回来后),我无法让它刷新.每次前两张图片都是空白的,只有在我将两张图片滑到一边后,它们才会刷新.我找到的答案都没有工作(尤其是覆盖 getItemPosition())

                  I have created an activity that uses FragmentStatePagerAdapter to provide small gallery. However, I can't get it to refresh when activity resumes (after coming back from other activity, for example). Every time first two pictures will be blank, and only after i swipe two pictures to the side, they get refreshed. None of the answers I've found work (especially overriding getItemPosition())

                  我是这样设置的:

                  mPagerAdapter = new PhotosPagerAdapter(getSupportFragmentManager());
                  mPager = (ViewPager) findViewById(R.id.photosViewPager);
                  mPager.setAdapter(mPagerAdapter);
                  

                  然后我有 FragmentStatePagerAdapter 类:

                  Then I have FragmentStatePagerAdapter class:

                  private class PhotosPagerAdapter extends FragmentStatePagerAdapter{
                  
                      public PhotosPagerAdapter(FragmentManager fm) {
                          super(fm);
                      }
                  
                      @Override
                      public int getCount() {
                          return photos.size();   
                      }
                  
                      @Override
                      public Fragment getItem(int position) {
                          ImageFragment f = new ImageFragment(position);
                          return f;
                      }
                  
                      @Override
                      public int getItemPosition(Object object) {
                          throw new RuntimeException();
                          //return POSITION_NONE;
                      }
                  
                  }
                  

                  您可能已经注意到,我在 getItemPosition 中抛出 RuntimeException,因为我想检查它何时被调用.直到我添加一些包含我的图片的列表时才会调用它.然后是 ImageFragment 类:

                  As you probably noticed, I throw RuntimeException in getItemPosition, because I wanted to check when it's called. And it isn't called until I add something to list containing my pictures. Then ImageFragment class:

                  public class ImageFragment extends Fragment{
                  
                      int position;
                      Bitmap mBitmap;
                      int width;
                      int height;
                      ImageView img;
                  
                      public ImageFragment(){
                      }
                  
                      public ImageFragment(int position){
                          this.position = position;
                      }
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
                  
                          img = new ImageView(container.getContext());
                          img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                          width = container.getWidth();
                          height = container.getHeight();
                  
                          loadBitmap();
                  
                          return img;
                      }       
                  
                      public void loadBitmap(){
                          if (img == null){
                              return;
                          }
                          final BitmapFactory.Options options = new BitmapFactory.Options();
                          options.inJustDecodeBounds = true;
                          BitmapFactory.decodeFile(photos.get(position), options);
                          options.inSampleSize = calculateInSampleSize(options, width/2, height/2);
                          options.inJustDecodeBounds = false;
                          mBitmap = BitmapFactory
                                  .decodeFile(photos.get(position), options);         
                  
                          img.setImageBitmap(mBitmap);
                      }
                  
                  
                      @Override
                      public void onDestroyView() {
                          mBitmap.recycle();
                          super.onDestroyView();
                      }       
                  }
                  

                  在我尝试修复它之后代码有点乱......但是:删除 onDestroyView() 不起作用.我已将 mPagerAdapter.notifyDataSetChanged() 放在几个必须调用的地方(如 onResume()),但没有结果.我对此感到有些绝望.

                  Code is kinda messy after I tried to fix it... But: removing onDestroyView() doesn't work. I have put mPagerAdapter.notifyDataSetChanged() in several places that must be called (like onResume()), with no result. I'm getting kinda desperate with that.

                  推荐答案

                  处理片段分页器适配器可以是 PITA.

                  Dealing with fragment pager adapters can be a PITA.

                  以下是一些有用的提示:

                  Here are a few helpful tips:

                  ViewPager PagerAdapter 不更新视图

                  动态更新 ViewPager?

                  一般来说,这个有 99% 的时间都在工作......

                  Generally speaking this one works 99% of the time...

                  像这样覆盖 PagerAdapter 中的 getItemPosition:

                  Override getItemPosition in your PagerAdapter like this:

                  @Override
                  public int getItemPosition(Object object) {
                      return POSITION_NONE;
                  }
                  

                  有时即使这些都不起作用,我必须使用蛮力"方法并重新创建整个视图(从 onCreate 开始)...

                  Sometimes even those don't work, I have to use the 'brute force' method and recreate the entire view again (from onCreate onwards)...

                  这篇关于在恢复活动时刷新 FragmentStatePagerAdapter 上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android 中的 Viewpager 可以无休止地在几天之间切换 下一篇:在 ViewPager android 的中心对齐子视图

                  相关文章

                    • <bdo id='n7rbr'></bdo><ul id='n7rbr'></ul>
                    <i id='n7rbr'><tr id='n7rbr'><dt id='n7rbr'><q id='n7rbr'><span id='n7rbr'><b id='n7rbr'><form id='n7rbr'><ins id='n7rbr'></ins><ul id='n7rbr'></ul><sub id='n7rbr'></sub></form><legend id='n7rbr'></legend><bdo id='n7rbr'><pre id='n7rbr'><center id='n7rbr'></center></pre></bdo></b><th id='n7rbr'></th></span></q></dt></tr></i><div id='n7rbr'><tfoot id='n7rbr'></tfoot><dl id='n7rbr'><fieldset id='n7rbr'></fieldset></dl></div>

                    <legend id='n7rbr'><style id='n7rbr'><dir id='n7rbr'><q id='n7rbr'></q></dir></style></legend>

                    <small id='n7rbr'></small><noframes id='n7rbr'>

                  1. <tfoot id='n7rbr'></tfoot>