gpt4 book ai didi

java - 如何从 PagerAdapter 启动 fragment

转载 作者:行者123 更新时间:2023-12-02 02:33:27 24 4
gpt4 key购买 nike

我想在我的自定义寻呼机适配器中启动 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 类没有 getSupportFragmentManager() 方法)到其构造函数,然后执行 myActivity.getSupportFragmentManager()

关于java - 如何从 PagerAdapter 启动 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46743255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com