简体   繁体   English

单击RecyclerView中的项目后分段交易

[英]Fragment transaction after clicking item in RecyclerView

Here is my problem. 这是我的问题。 I want to make fragment transaction when I click a single item in my RecyclerView . 我想在我的RecyclerView单击一个项目时进行片段交易。 In pager adapter, I created rootListFragment which is a container for other Fragments . 在寻呼机适配器中,我创建了rootListFragment ,它是其他Fragments的容器。 When rootListFragment is created it contains my fragment with RecyclerView which I named ListFragment . 创建rootListFragment ,它包含带有RecyclerView片段,我将其命名为ListFragment When I click single item I want to replace rootListFragment to WebViewFragment which contains for now only single text "Hello blank fragment". 当我单击单个项目时,我想将rootListFragment替换为WebViewFragment ,该WebViewFragment目前仅包含单个文本“ Hello空白片段”。 After this transaction, my ListFragment is still in front view. 进行此事务后,我的ListFragment仍处于正视图中。 Do you know how to solve this problem? 你知道如何解决这个问题吗? Here's my code. 这是我的代码。

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

}

Part of RecyclerView with item click 单击项目的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 of app after item click: 单击项目后的应用程序屏幕: 屏幕

I think problem is in your onclick method get your Context/activity value in your Adapter Constructor like this 我认为问题是您的onclick方法像这样在您的适配器构造器中获取了上下文/活动值

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

}

and then in your onclick method put below code 然后在你的onclick方法中放下面的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM