意图将PagerAdapter的putExtra改为Fragment

时间:2015-06-16 18:10:02

标签: java android android-intent android-fragments

我需要从ThePagerAdapter.javaSliderHome.java传递值。我该如何实现呢?我已经尝试了很多,但没有成功。

这是我的代码:

ThePagerAdapter.java

public class ThePagerAdapter extends PagerAdapter {
Context context;

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

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

public ThePagerAdapter(Context context,List<Slider> list) {
    this.list = list;
    this.context = context;
}

@Override
public Object instantiateItem(final ViewGroup container, int position) {
    // Declare Variables
    ImageView imgflag;

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.image_item, container, false);

    // Capture position and set to the TextViews
    final String id =  list.get(position).getId();

    imgflag = (ImageView) itemView.findViewById(R.id.imageView);

    imgflag.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent i = new Intent(context, SliderMore.class);
            i.putExtra("slider_id", id);
            context.startActivity(i);
        }
    });

    ((ViewPager) container).addView(itemView);

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // Remove viewpager_item.xml from ViewPager
    ((ViewPager) container).removeView((RelativeLayout) object);

}
}

从上面PagerAdapter我使用Fragment

将值发送给下面描述的另一个Intent.putExtra

SliderHome.java

public class SliderHome extends Fragment {

ViewPager viewPager;
ImageView iv2,iv3;
String slider_id;

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

    View rootView = inflater.inflate(R.layout.slider_more, container, false);

    viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
    iv2 = (ImageView) rootView.findViewById(R.id.imageView2);
    iv3 = (ImageView) rootView.findViewById(R.id.imageView3);

    return rootView;
}

有解决方案吗?提前致谢。

1 个答案:

答案 0 :(得分:1)

在这种情况下,我喜欢使用工厂方法模式。

示例:

public static MatchDetailSummaryFragment newInstance(String matchIdString, int position) {
    MyFragment fragment = new MyFragment();
    Bundle args = new Bundle();
    args.putString(ARGS_MY_ID, matchIdString);
    args.putInt(ARG_POSITION,position);
    fragment.setArguments(args);
    return fragment;
}

public MyFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.mPosition = getArguments().getInt(ARG_POSITION);
    this.id = getArguments().getString(ARGS_MY_ID) != null)
}

你可以调用它:

public void fillMyPagerAdapter()
{

    mChildFragments = new Hashtable<Integer,GxuBaseFragment>();
    int count = 0;

        MyFragment a = MyFragment.newInstance(Integer.toString(my.getId()),count);
        mChildFragments.put(count,a);
        count += 1;

    mViewPagerAdapter.setTabHolderScrollingContent(this);
    mPager.setAdapter(mViewPagerAdapter);
    mPagerSlidingTabStrip.setViewPager(mPager);
    mPagerSlidingTabStrip.setOnPageChangeListener(this);

}

这段代码可能无效,因为它是我生产代码的一些摘录,但它会解释主要的想法。