如何从 PagerAdapter 开始片段

How to start fragment from PagerAdapter

我想在我的自定义寻呼机适配器中启动 Fragment。但是,我不知道如何让 getSupportFragmentManager() 开始交易。

非常感谢您在这件事上的时间和帮助。

这是我的代码,寻呼机的自定义适配器:

public static class SlideShowAdapter extends PagerAdapter {


    private ArrayList<Movie> popularMovieList;
    private LayoutInflater inflater;
    private Context context;

    public SlideShowAdapter(Context context, ArrayList<Movie> popularMovieList) {
        this.context = context;
        this.popularMovieList =popularMovieList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

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

    @Override
    public Object instantiateItem(ViewGroup view, final int position) {
        View myImageLayout = inflater.inflate(R.layout.slide, view, false);
        ImageView myImage = (ImageView) myImageLayout
                .findViewById(R.id.slideShowImg);
        Picasso.with(context).load(popularMovieList.get(position).getImage()).into(myImage);
        view.addView(myImageLayout, 0);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MovieFragment movieFragment = MovieFragment
                        .newInstance(popularMovieList.get(position),popularMovieList.get(position).getGenre());
                /***These code below does not work*/
                getActivity().getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fragArea,movieFragment)
                        .addToBackStack(null)
                        .commit();
            }
        });
        return myImageLayout;
    }

如果你想在SlideShowAdapter中使用FragmentManager

  • 一种方法是将其实例传递给 SlideShowAdapter 构造函数
  • 另一种方法是将您的 Activity 作为 FragmentActivityAppCompatActivity 传递(Activity class 没有 getSupportFragmentManager() 方法)到它的构造函数然后做 myActivity.getSupportFragmentManager().

我根据@Amin Mousavi 的建议得到了解决方案。

public static class SlideShowAdapter extends PagerAdapter {


    private ArrayList<Movie> popularMovieList;
    private LayoutInflater inflater;
    private FragmentActivity context;

    public SlideShowAdapter(FragmentActivity context, ArrayList<Movie> popularMovieList) {
        this.context = context;
        this.popularMovieList =popularMovieList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

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

    @Override
    public Object instantiateItem(ViewGroup view, final int position) {
        View myImageLayout = inflater.inflate(R.layout.slide, view, false);
        ImageView myImage = (ImageView) myImageLayout
                .findViewById(R.id.slideShowImg);
        Picasso.with(context).load(popularMovieList.get(position).getImage()).into(myImage);
        view.addView(myImageLayout, 0);
        myImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MovieFragment movieFragment = MovieFragment
                        .newInstance(popularMovieList.get(position),popularMovieList.get(position).getGenre());
                /***These code below does not work*/
                context.getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fragArea,movieFragment)
                        .addToBackStack(null)
                        .commit();

            }
        });
        return myImageLayout;
    }