2017-05-09 48 views
0

這是我的問題。當我點擊RecyclerView中的單個項目時,我想進行片段交易。在尋呼機適配器中,我創建了rootListFragment,這是其他Fragments的容器。當創建rootListFragment時,它包含我的片段RecyclerView,我將其命名爲ListFragment。當我單擊單個項目時,我想將rootListFragment替換爲WebViewFragment,其中僅包含單個文本「Hello blank fragment」。在此次交易之後,我的ListFragment仍在前方。你知道如何解決這個問題嗎?這是我的代碼。點擊RecyclerView中的項目後的片段交易

RootListFragment:

public class RootListFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_root_list, container, false); 

     FragmentManager fragmentManager = getChildFragmentManager(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.replace(R.id.root_list_fragment, new ListFragment()).commit(); 

     return view; 
    } 

} 

RecyclerView與項目部分點擊

public static class DataViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

     ... 

     public DataViewHolder(View view) { 
      super(view); 

      view.setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 

      AppCompatActivity activity = (AppCompatActivity) v.getContext(); 
      activity.getSupportFragmentManager().beginTransaction().replace(R.id.root_list_fragment, new WebViewFragment()) 
        .commit(); 
     } 
    } 
} 

屏幕應用程序的項目後點擊: screen

+0

你得到任何錯誤或同時RecyclerView –

+0

項目上點擊你是不是能夠取代片段有沒有錯誤。在RecyclerView中單擊項目後,rootListFragment仍包含RecyclerView。 –

+0

爲什麼你想讓你的ListFragment被破壞?因爲在調用替換方法後它不會被銷燬?你的主要目標是什麼? –

回答

0

我認爲問題出在你的onclick方法讓你的上下文/您的適配器構造函數中的活動值像這樣

public ViewAdapter(Context context, List<Information> data) 
{ 
    inflater=LayoutInflater.from(context); 
    this.data=data; 
    this.context=context; 

} 

,然後在你的onclick方法,把下面的代碼

context.getSupportFragmentManager().beginTransaction().replace(R.id.root_list_fragment, new WebViewFragment()) 
       .commit();