2017-11-11 2 views
-2

RecyclerView를 사용하여 데이터를 표시하고 있습니다. 리사이클러보기 내에서 메서드 onClick 및 파편을 바꾸려면 싶습니다. 내가 가진 유일한 문제는 정적 메서드에서 getSupportFragmentManager 또는 getChildFragmentManager accs 수 없습니다. 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 나에게 오류를 제공입니다 ... 어떻게해야 ..?

답변

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 patActivity 내가 그것을 Recycler에서 꺼낼 때. Btw in Fragment 및 Activity가 아닙니다. –

0

콜백 인터페이스를 사용해야합니다.

콜백 클래스

public interface MyCallback{ 

    void onButtonClick(); 
} 

어댑터

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


} 

활동/FRAGMENT

(...) 

void onButtonClick(){ 
    getSupportFragmentManager(). 
      beginTransaction(). 
      replace(R.id.container, new GenresFragment()). 
      commit(); 
} 
관련 문제