[英]Fragment transaction after clicking item in RecyclerView
這是我的問題。 我想在我的RecyclerView
單擊一個項目時進行片段交易。 在尋呼機適配器中,我創建了rootListFragment
,它是其他Fragments
的容器。 創建rootListFragment
,它包含帶有RecyclerView
片段,我將其命名為ListFragment
。 當我單擊單個項目時,我想將rootListFragment
替換為WebViewFragment
,該WebViewFragment
目前僅包含單個文本“ Hello空白片段”。 進行此事務后,我的ListFragment
仍處於正視圖中。 你知道如何解決這個問題嗎? 這是我的代碼。
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();
}
}
}
我認為問題是您的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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.