2017-08-19 36 views
0

我想在我的recyclerview適配器中打開片段表單onClick並傳遞數據。我知道如何打開一個活動(檢查下面的代碼),但我怎樣才能打開片段相同的方式。檢查下面的代碼以獲得更好的理解。我是新來的機器人,並嘗試從免費資源學習。對不起,如果有什麼不對。如何從onClick打開片段

CODE

@Override 
     public void onClick(View v) { 
      int position = getAdapterPosition(); 
      PlaylistDetailsItem playlistDetailsItems = this.playlistDetailsItems.get(position); 
      Intent intent = new Intent(this.context, VideoActivity.class); 

      intent.putExtra("videoHeading", playlistDetailsItems.getPlaylistDetailsSnippet() 
        .getPlaylistDetailsTitle()); 

      intent.putExtra("videoDesc", playlistDetailsItems.getPlaylistDetailsSnippet() 
        .getPlaylistDetailsDescription()); 

      intent.putExtra("videoID", playlistDetailsItems.getPlaylistDetailsSnippet() 
        .getPlaylistDetailsResourceId().getVideoId()); 

      this.context.startActivity(intent); 

     } 
    } 
+0

https://stackoverflow.com/q/42055690/2979171。檢查這一點,併發送數據,你可以使用構造函數 – lvl4fi4

+0

我正在使用它在recyclerview適配器和getFragmentManager()表示無法resolvemethod'getFragmentManager' – P595

回答

0

添加下面的代碼在你的onClick

FirstFragment firstFrag = new FirstFragment(); 
this.getFragmentManager().beginTransaction() 
     .replace(R.id.layout_container, firstFrag, TAG_FRAGMENT) 
     .addToBackStack(null) 
     .commit(); 
0
Bundle b = new Bundle(); 
    replaceFragmentInternal(new fragment,b,true,false); 

//將數據放入包,並傳遞它的方法。在頂層活動中使用此方法,以便不必一次又一次地寫入相同的代碼。

replaceFragmentInternal(Fragment fragment, Bundle bundle, boolean addToBackStack, boolean anim) { 
     if (bundle != null) 
      fragment.setArguments(bundle); 

     tag = fragment.getClass().getSimpleName(); 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     //ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
     //ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right); 
     ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right); 
     ft.replace(R.id.container_internal, fragment, tag); 
     fragment.setRetainInstance(true); 

     if (addToBackStack) 
      ft.addToBackStack(tag); 

     try { 
      ft.commit(); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
      ft.commitAllowingStateLoss(); 
     } 
    } 
0

首先你要喜歡這個創建一個片段類,記住導入android.support.v4.app.Fragment; (支持V4片段)

public class MyFragment extends Fragment { 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.your_xml_file, container, false); 
    return view; 
} 

}

並調用像

MyFragment myfragment = new Fragment(); 

//pass data 
     Bundle bundle = new Bundle(); 
     bundle.putString("KEY","DATA"); 
     myfragment.setArguments(bundle); 

    FragmentTransaction fragmentManager = activity.getSupportFragmentManager().beginTransaction(); 
      fragmentManager.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 

       fragmentManager.addToBackStack(null); 

      fragmentManager.replace(R.id.content, mFragment, TAG).commit(); 
0

片段你在年底創建Viewholder如果沒有,然後粘貼下面的回收鑑於轉接器內關閉之前適配器(在底部「}」之前)

public ViewHolder(View itemView) { 
     super(itemView); 
     mView = itemView; 
     mIdView = (TextView) itemView.findViewById(R.id.id); 
     mContentView = (TextView) itemView.findViewById(R.id.content); 

     mView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       final int position = getAdapterPosition(); 
       if (null != listener) {  
        listener.onListFragmentInteraction(mValues.get(position)); 
       } 
      } 
    }); 
} 

另請參閱本教程的發送數據: https://developer.android.com/training/basics/fragments/communicating.html

這一切看起來preatty很難,但它這是學習的,也看起來很專業