jehee choi
jehee choi

Reputation: 175

how to add fragment in recycler adapter?

the question is that I wanted to add fragment each row data in recycler view and tried adding fragment in XML and set in view holder. but the error occurs that said Duplicate id 0x7f070075, tag null, or parent id 0xffffffff with another fragment for SomeFragment. and tried the second way that is add frame layout dynamically in viewholder and put unique id each frame layout and add fragment dynamically. the problem is error that is no view found

Caused by: java.lang.IllegalArgumentException: Binary XML file line #0: Duplicate id 0x7f070075, tag null, or parent id 0xffffffff with another fragment for com.example.my.samefragmenttest.TestFragment 

when tried for debugging, work fine though, not run time.. my question is i want to recycle a fragment in list row. any ideas?? thank you..

sorry for late, here is my code

public class MainActivity extends AppCompatActivity {
RecyclerViewPager mRecyclerView;
List<Person> peopleList = new ArrayList<Person>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initialData();

    mRecyclerView = (RecyclerViewPager) findViewById(R.id.list);
    mRecyclerView.setLayoutManager(new 
LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
    mRecyclerView.hasFixedSize();
    final RecyclerAdapter recyclerAdapter = new 
RecyclerAdapter(this,peopleList,this.getSupportFragmentManager());
    mRecyclerView.setAdapter(recyclerAdapter);

 }
}






/**
 * Created by MY on 2018-03-11.
 */

public class RecyclerAdapter extends 
RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
List<Person> personList;
Context context;
FragmentManager fragmentManager;
int count=0;

public RecyclerAdapter(Context context, List<Person> peopleList, 
 FragmentManager supportFragmentManager) {
    this.personList = peopleList;
    this.context = context;
    this.fragmentManager = supportFragmentManager;
}


 @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout
    View view = inflater.inflate(R.layout.row_item, parent, false);

    // Return a new holder instance
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
 }

 @Override
 public void onBindViewHolder(ViewHolder holder, int position) {
    Person person = personList.get(position);

    // Set item views based on your views and data model
    holder.id.setText(String.valueOf(person.getId()));
    holder.testFragment = new TestFragment();
 }

 @Override
 public int getItemCount() {
    return personList.size();
 }

 public class ViewHolder extends RecyclerView.ViewHolder{

    TextView id;
    LinearLayout linearLayout;
    FrameLayout frameLayout;
    TestFragment testFragment;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public ViewHolder(View itemView) {
        super(itemView);
        id = (TextView)itemView.findViewById(R.id.txt_id);
        linearLayout = (LinearLayout)itemView.findViewById(R.id.linear);
        frameLayout = (FrameLayout)itemView.findViewById(R.id.test_frame);

        if(testFragment==null){

            fragmentManager.findFragmentByTag(TestFragment.TAG);
           // TestFragment.newInstance(fragmentManager);
        }
    }
 }

Upvotes: 1

Views: 6454

Answers (3)

Eduard  Fomin
Eduard Fomin

Reputation: 31

Starting from API 17, you can use view.generateViewId () to get the dynamic id of a fragment without possible intersection errors with existing id.

public class ViewHolderInds extends RecyclerView.ViewHolder {
...
    public final int frameId; //Unique identifier for ValueFrame
...
    public ViewHolderInds(View view) {
        super(view);
...
        frameId = view.generateViewId();
        ((FrameLayout) view.findViewById(R.id.valueFrame)).setId(frameId);
...
    }
}

Then, in the RecyclerView.Adapter, we use a frameId from a ViewHolderInds.

    @Override
    public void onBindViewHolder(@NonNull ViewHolderInds holder, int position) {
        getActivity().getSupportFragmentManager().
                beginTransaction().
                replace(holder.frameId, EditFragment.newInstance(...)).
                commit();
...

Upvotes: 3

Muhammed Saneef
Muhammed Saneef

Reputation: 72

You will have to create a layout with a fragment container(Viewgroup) and Create your fragment from the onCreateViewHolder() and add fragment to the layout. In R.layout.item_fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

And in your recycler view adapter, you could do like this,

In Constructor,

public RecyclerViewConstructor(Activity hostActivity) {
    super();
    this.hostActivity = hostActivity;
    lastClickTime = 0;
}

Activity host
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View rootView = LayoutInflater.from(mContext).inflate(R.layout.item_fragment, null);
    return new ViewHolder(rootView);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    YourFragment yourFragment = YourFragment.newInstance();
    FragmentManager fm = hostActivity.getSupportFragmentManager();
    fm.addOnBackStackChangedListener(this);
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.fragment_container, fragment, tag);
}

Upvotes: 0

Sandun Isuru Niraj
Sandun Isuru Niraj

Reputation: 461

I think You can't use fragment as Recycler View.

Refer Here. Reference

Upvotes: 0

Related Questions