将Fragment One的RecyclerView项目位置传递给Fragment Two的另一个RecyclerView适配器

时间:2018-03-05 07:08:53

标签: android android-recyclerview fragment

我在Recyclerview中设置了Fragment。我想将RecyclerView项目的位置从Fragment one的RecyclerView Adapter发送到Fragment Two的RecyclerView Adapter,以在Fragment 2中设置RecyclerView的特定项目。登记/> 这是Fragment one Adapter的代码

@Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
           final CurrentStatusEntry current=filterList.get(position);
          // holder.number.setText(current.getNo());
           holder.Name.setText(current.getName());
           holder.People.setText(current.getPeople());
           holder.Time.setText(current.getTime());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int position=holder.getAdapterPosition();

                    AppCompatActivity activity=(AppCompatActivity)v.getContext();
                    activity.getSupportFragmentManager().beginTransaction().replace(R.id.frame,satusInfo).addToBackStack("info").commit();
                }
            });  

如何通过这个职位?

2 个答案:

答案 0 :(得分:0)

您可以使用Bundle进行此尝试,如下例所示:

在片段一setArguments中:

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

在第二段片段getArguments中:

Bundle bundle = this.getArguments();
if (bundle != null) {
    int myInt = bundle.getInt(key, defaultValue);
}

参考Android Doc.

答案 1 :(得分:0)

在片段1的片段后片段实例中片段化两个

getFragmentManager().beginTransaction()
                                .replace(R.id.ftHolder, fragmentTwo.newInstance(getAdapterPosition()))
                                .addToBackStack("info")
                                .commit();
片段2中的

添加此功能

public static fragmentTwo newInstance(int position) {
    fragmentTwo fragment = new fragmentTwo();
    Bundle bdl = new Bundle();
    bdl.putInt("position", position);
    fragment.setArguments(bdl);
    return fragment;
}

在onCreate()或onActivityCreated()中获取并在任何地方使用

position = getArguments().getInt("position");