我正在尝试打开一个新片段,但收到错误:
无法解析getFragmentManager
我尝试了一些解决方案,但仍然遇到错误。我该如何解决?
这是我的适配器代码,我设置了OnClickListener
:
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ViewHolder> {
private Context context;
public ImageLoader mImageLoader;
//List of superHeroes
List<ListOfData> superHeroes;
public RecyclerViewDataAdapter(List<ListOfData> superHeroes, Context context) {
super();
//Getting all the superheroes
this.superHeroes = superHeroes;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_row, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListOfData superHero = superHeroes.get(position);
mImageLoader = MyApplication.getInstance().getImageLoader();
mImageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView1, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
holder.imageView1.setImageUrl(superHero.getImageUrl(), mImageLoader);
holder.textViewName1.setText(superHero.getName());
//holder.textViewName2.setText(superHero.getName());
//holder.textViewRank.setText(String.valueOf(superHero.getRank()));
//holder.textViewRealName.setText(superHero.getRealName());
//holder.textViewCreatedBy.setText(superHero.getCreatedBy());
//holder.textViewFirstAppearance.setText(superHero.getFirstAppearance());
String powers = "";
/* for(int i = 0; i<superHero.getPowers().size(); i++){
powers+= superHero.getPowers().get(i);
}*/
//holder.textViewPowers.setText(powers);
holder.imageView1.setOnClickListener(new View.OnClickListener() {
public Activity context;
@Override
public void onClick(View v) {
BehindTheLayoutFragment nextFrag= new BehindTheLayoutFragment();
this.getFragmentManager().beginTransaction()
.replace(R.id.behind_the_layout_fragment, nextFrag)
.addToBackStack(null)
.commit();
}
});
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public NetworkImageView imageView1;
public TextView textViewName1;
public TextView textViewName2;
public TextView textViewRank;
public TextView textViewRealName;
public TextView textViewCreatedBy;
public TextView textViewFirstAppearance;
public TextView textViewPowers;
public ViewHolder(View itemView) {
super(itemView);
imageView1 = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
textViewName1 = (TextView) itemView.findViewById(R.id.category_title_one);
/* textViewRank= (TextView) itemView.findViewById(R.id.textViewRank);
textViewRealName= (TextView) itemView.findViewById(R.id.textViewRealName);
textViewCreatedBy= (TextView) itemView.findViewById(R.id.textViewCreatedBy);
textViewFirstAppearance= (TextView) itemView.findViewById(R.id.textViewFirstAppearance);
textViewPowers= (TextView) itemView.findViewById(R.id.textViewPowers);
*/
}
}
答案 0 :(得分:0)
getFragmentManager
上的activity
实例上调用{p> RecyclerViewDataAdapter
。
将此类用作Activity中的内部类,或使用您在RecyclerViewDataAdapter的构造函数中传递的引用来获取片段管理器
答案 1 :(得分:0)
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ViewHolder>
{
recycleListener mListener;
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.imageView1.setOnClickListener(new View.OnClickListener() {
public Activity context;
@Override
public void onClick(View v) {
mListener.onClick();
}
});
public interface recycleListener
{
void onClick();
}
}
public class MyActivity extends AppCompatActivity implements RecyclerViewDataAdapter.recycleListener
{
public void onClick()
{
BehindTheLayoutFragment nextFrag= new BehindTheLayoutFragment();
this.getFragmentManager().beginTransaction()
.replace(R.id.behind_the_layout_fragment, nextFrag)
.addToBackStack(null)
.commit();
}
}
答案 2 :(得分:0)
注意事项!!
1。)在活动上下文中调用getFragment manager,而在片段上下文中调用getChildFragmentManager
2。)这是指父类。在适配器类中,这将引用适配器类,因此您无法执行
this.getFragmentManager().beginTransaction()
.replace(R.id.behind_the_layout_fragment, nextFrag)
.addToBackStack(null)
.commit();
为什么呢?如第1点所述.getFragmentManager被称为onactivity context。但是,嘿,你已经在适配器中传递了上下文。所以简单地做
context.getFragmentManager().beginTransaction()
.replace(R.id.behind_the_layout_fragment, nextFrag)
.addToBackStack(null)
.commit();