我想在自定义寻呼机适配器中启动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;
}
答案 0 :(得分:2)
如果您想在FragmentManager
中使用SlideShowAdapter
:
SlideShowAdapter
构造函数Activity
作为FragmentActivity
或AppCompatActivity
(Activity
类没有getSupportFragmentManager()
方法)传递给其构造函数然后执行{ {1}}。答案 1 :(得分:0)
我根据@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;
}