Android刷新片段内的ViewPager

时间:2013-07-09 13:47:29

标签: android android-fragments android-viewpager

我曾尝试在发布之前搜索SO,但我无法理解如何刷新托管在片段内的ViewPager的内容。 你能帮帮我吗?

感谢您的时间

这是我的代码。 我在其中托管ViewPager的片段活动。它显示不同的小部件和ViewPager显示不同的图像,我从代码中的db“carosello”调用返回的图像列表。当我执行搜索时,我将refresh()方法称为来自托管此片段的主活动的回调,并再次设置所有视图。适用于片段中的所有信息,但不适用于ViewPager:始终显示最早(首次创建)的图像。

public class Page2FamigliaFragment extends Fragment {

//... variable declaration

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

    // fragment not when container null
    if (container == null) {
        return null;
    }
    // inflate view from layout
    View view = (LinearLayout)inflater.inflate(R.layout.famiglia_page2,container,false);

    //retrieve my object from the main view that host this fragment
    info = ((MainFamiglia)this.getActivity()).getMyObject();

   //run the setup of the view.    
   setUpMyPage2View(view);

   return view;
}

public void setUpMyPage2View(View view)
{
//... inizialization of some other objects and view - i do not show only because is out of topic for this matter
//retrive the image used in the page viewer
    carosello = info.getCarosello();
    parts = carosello.split(",");

    //how many page ??
    int i = parts.length / 3;
    int resto = parts.length % 3;
    int pages = 0;
    if (resto!=0) {
        pages = i + 1;
    } else {
        pages = i;
    }

List<Fragment> fragments = new ArrayList<Fragment>();

  String[] tempArray;
  int startIndex = 0;
  int endIndex = 3;


  if (parts.length <= 3) {

    fragments.add(myCarosello.newInstance(parts));

  } else {


    tempArray = Arrays.copyOfRange(parts, startIndex, endIndex);

    for (int p=0; p < pages; p++) {

        fragments.add(myCarosello.newInstance(tempArray));
        startIndex = endIndex;
        endIndex = ((endIndex+3) > parts.length) ? parts.length : (endIndex+3);
        tempArray = Arrays.copyOfRange(parts, startIndex, endIndex);

    }

  }

  pageAdapter = new MyPageAdapter(getChildFragmentManager(), fragments);

  pager = (ViewPager) view.findViewById(R.id.viewpager);
  pager.setAdapter(pageAdapter);
  pager.setOnTouchListener(new OnTouchListener() {

      @Override
      public boolean onTouch(View v, MotionEvent event) {
          if(
                  event.getAction() == MotionEvent.ACTION_DOWN &&
                  v instanceof ViewGroup
          ) {
                  ((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
          }
          return false;
      }
  });

}

//i call this method from a callback from the main view for refresh this fragment
public void refresh(){
    //recupero oggetto dalla activity principale
    info = ((MainFamiglia)this.getActivity()).getMyObject();
    pager.getAdapter().notifyDataSetChanged();
    pager.removeAllViews();
    setUpMyPage2View(this.getView());
}
}

这是pageAdapter的类

public class MyPageAdapter extends FragmentPagerAdapter {

// fragments to instantiate in the viewpager
   private List<Fragment> fragments;

   // constructor
   public MyPageAdapter(FragmentManager fm,List<Fragment> fragments) {
      super(fm);
      this.fragments = fragments;
   }

   // return access to fragment from position, required override
   @Override
   public Fragment getItem(int position) {
      return this.fragments.get(position);
   }

   // number of fragments in list, required override
   @Override
   public int getCount() {
      return this.fragments.size();
   }

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

}

这是pageviewer中imageview的类

public class myCarosello extends Fragment {

public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
private int numeroImmagini = 0;

public static final myCarosello newInstance(String[] pictures)
{
    myCarosello f = new myCarosello();
    Bundle bdl = new Bundle(1);
    bdl.putStringArray(EXTRA_MESSAGE, pictures);
    f.setArguments(bdl);

    return f;
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

    String[] pictures = getArguments().getStringArray(EXTRA_MESSAGE);
    numeroImmagini = pictures.length;

    View view = (LinearLayout)inflater.inflate(R.layout.carosello,container,false);

    ImageView imageView1 = (ImageView) view.findViewById(R.id.carosello1);
    ImageView imageView2 = (ImageView) view.findViewById(R.id.carosello2);
    ImageView imageView3 = (ImageView) view.findViewById(R.id.carosello3);

    switch (numeroImmagini) {
    case 1:
        imageView1.setImageResource(returnImageResource(pictures[0]));
        imageView2.setVisibility(View.GONE);
        imageView3.setVisibility(View.GONE);
    break;
    case 2:
        imageView1.setImageResource(returnImageResource(pictures[0]));
        imageView2.setImageResource(returnImageResource(pictures[1]));
        imageView3.setVisibility(View.GONE);
    break;
    case 3:
        imageView1.setImageResource(returnImageResource(pictures[0]));
        imageView2.setImageResource(returnImageResource(pictures[1]));
        imageView3.setImageResource(returnImageResource(pictures[2]));
    break;

    }  

    return view;

}

//return the correct resource name
private int returnImageResource(String image)
{
    image = image.replace(" ", "_");
    String filenameArray[] = image.split("\\.");
    image = "img_" + filenameArray[0].toLowerCase();
    int drawableImageId = getActivity().getResources().getIdentifier(image, "drawable", getActivity().getPackageName());
    return drawableImageId;
}
}

2 个答案:

答案 0 :(得分:32)

感谢this帖子,我已经解决了 将MyPageAdapter extends FragmentPagerAdapter更改为MyPageAdapter extends FragmentStatePagerAdapter

答案 1 :(得分:0)

片段方法中的第一个覆盖

@覆盖

public void setUserVisibleHint(boolean isVisibleToUser) {

    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser){

        actionView();
    }
    else{
        //no
    }

}