我正在使用RecyclerView来显示数据。在回收站视图中,我有方法onClick,我想在片段之间进行替换。我唯一的问题是我不能从静态方法中获取getSupportFragmentManager或getChildFragmentManager。 RecyclerView位于片段内,而不是活动。
这是RecyclerView代码
public static class TypeMusicListAdapter extends RecyclerView.Adapter<TypeMusicListViewHolder>{
private LayoutInflater inflater;
private Context context;
private List<String> data;
public TypeMusicListAdapter(Context context, List<String> data){
this.context = context;
this.inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public TypeMusicListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.type_music_item,parent,false);
return new TypeMusicListViewHolder(v);
}
@Override
public void onBindViewHolder(TypeMusicListViewHolder holder, int position) {
String songType = data.get(position);
//holder.musicType.setText(songType.getSongName());
holder.musicType.setText(songType);
}
@Override
public int getItemCount() {
return data.size();
}
}
public static class TypeMusicListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView musicType;
private FragmentManager fm;
public TypeMusicListViewHolder(View itemView,FragmentManager fm) {
super(itemView);
musicType = (TextView) itemView.findViewById(R.id.musicType);
this.fm = fm;
musicType.setOnClickListener(this);
}
@Override
public void onClick(View view) {
getSupportFragmentManager().
beginTransaction().
replace(R.id.container, new GenresFragment()).
commit();
}
}
在getSupportFragmentManager ofcourse中给我错误... 我该怎么办..?
答案 0 :(得分:0)
更改您的视图持有者类,使其具有您的活动的成员变量。
public static class TypeMusicListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView musicType;
private FragmentManager fm;
AppCompatActivity activity;
public TypeMusicListViewHolder(View itemView,FragmentManager fm,AppCompatActivity act) {
super(itemView);
musicType = (TextView) itemView.findViewById(R.id.musicType);
this.fm = fm;
activity = act;
musicType.setOnClickListener(this);
}
@Override
public void onClick(View view) {
activity.getSupportFragmentManager().
beginTransaction().
replace(R.id.container, new GenresFragment()).
commit();
}
}
答案 1 :(得分:0)
您应该使用回调接口。
CALLBACK CLASS
public interface MyCallback{
void onButtonClick();
}
<强> ADAPTER 强>
public static class TypeMusicListAdapter extends RecyclerView.Adapter<TypeMusicListViewHolder> implements View.OnClickListener{
private LayoutInflater inflater;
private Context context;
private List<String> data;
public TypeMusicListAdapter(Context context, List<String> data){
this.context = context;
this.inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public TypeMusicListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.type_music_item,parent,false);
return new TypeMusicListViewHolder(v);
}
@Override
public void onBindViewHolder(TypeMusicListViewHolder holder, int position) {
String songType = data.get(position);
//holder.musicType.setText(songType.getSongName());
holder.musicType.setText(songType);
holder.musicType.setOnClickListener(this);
}
@Override
public void onClick(View view) {
((YourActivity) context).onButtonClick();
}
@Override
public int getItemCount() {
return data.size();
}
}
public static class TypeMusicListViewHolder extends RecyclerView.ViewHolder {
private TextView musicType;
private FragmentManager fm;
public TypeMusicListViewHolder(View itemView,FragmentManager fm) {
super(itemView);
musicType = (TextView) itemView.findViewById(R.id.musicType);
this.fm = fm;
}
}
你的活动/片段
(...)
void onButtonClick(){
getSupportFragmentManager().
beginTransaction().
replace(R.id.container, new GenresFragment()).
commit();
}