2017-10-14 171 views
0

我想在自定義尋呼機適配器中啓動Fragment。但是,我不知道如何讓getSupportFragmentManager()開始交易。如何從PagerAdapter開始片段

非常感謝您在這件事上的時間和協助。

這是尋呼機我的代碼,自定義適配器:

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

回答

2

如果你想使用FragmentManagerSlideShowAdapter

  • 一種方法是將它的實例傳遞給SlideShowAdapter構造
  • 另一個一種是通過你的Activity作爲FragmentActivityAppCompatActivityActivity類沒有getSupportFragmentManager()方法)t o其構造函數,然後執行myActivity.getSupportFragmentManager()
+0

我試過第二種方法,它是正確的。但是,當我點擊該項目時,它沒有出現碎片 – DonDave

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