Add fragment into listview item

THERE IS A SOLUTION” for this.

The issue is, you cannot add fragment directly to the container(FrameLayout) with same “id” in listview as you have done in the above code.

The trick is, create listview(Recyclerview) of “LinearLayout”. Then dynamically create FrameLayout in adapter and assign different id’s for each. Inflate Fragment to FrameLayout and this FrameLayout to LinearLayout.

@Override
protected void onBindBasicItemView(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof VideoHomeViewHolder) {
        VideoHomeViewHolder videoHomeViewHolder = (VideoHomeViewHolder) holder;
        FrameLayout frameLayout = new FrameLayout(mContext);
        frameLayout.setId(position+1); //since id cannot be zero
        FragmentHelper.popBackStackAndReplace(mFragmentManager, frameLayout.getId(),
                new ShowLatestVideosFragment(mShowLatestVideosItems.get(position)));
        videoHomeViewHolder.linearLayout.addView(frameLayout);
    }
}

Leave a Comment