2017-11-11 190 views
-2

我正在使用RecyclerView來顯示數據。在回收站視圖中,我有方法onClick,我想在碎片之間進行替換。我唯一的問題是,我無法贊成getSupportFragmentManager或getChildFragmentManager靜態方法。 RecyclerView位於片段內而不是活動。如何從靜態方法調用getSupportFragmentManager?

這是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

更改您的視圖持有者類,使其具有您的活動的成員變量。

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(); 
    } 
} 
+0

我應該如何初始化AppCom當我從回收站帶到外面的時候拍攝活動。順便說一句,它在片段而不是活動。 –

0

您應該使用回調接口。

回調類

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(); 
}