0
    holder.parentLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ProfileFragment myFragment = new ProfileFragment();
            ProfileFragment.profileFragment.getFragmentManager().beginTransaction().replace(R.id.RelLayout1, myFragment).addToBackStack(null).commit();
            Log.d(TAG, "onClick: rr");
        }
    });

This is called inside a Recyclerview Adapter.On clicking an item ,I want to create a new instance of the fragment and hide some of the views inside the created Fragment.

Sidharth V
  • 117
  • 8

1 Answers1

1

You can send flag on fragment arguments and check this flag on the fragment class

holder.parentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ProfileFragment myFragment = new ProfileFragment();
                Bundle fragmentBundle = new Bundle();
                fragmentBundle.putBoolean(ProfileFragment.MY_FLAG, true);
                myFragment.setArguments(fragmentBundle);
                ProfileFragment.profileFragment.getFragmentManager().beginTransaction().replace(R.id.RelLayout1, myFragment).addToBackStack(null).commit();

            }
        });

And now you can check this flag in your fragment class

public class ProfileFragment extends Fragment{
    public static final String MY_FLAG = "MY_FLAG";
    private boolean hideShowFlag = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //check if arguments not null and contains the desired key
        //if all true set value to a variable to be used later
        if (getArguments() != null && getArguments().containsKey(MY_FLAG) {
            this.hideShowFlag = getArguments().getBoolean(MY_FLAG);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(getRootView(), container, false);
        if (this.hideShowFlag) {
            //write your logic for show and hide here
        }
        return mView;
    }

}

hope my answer will help you.

Shalan93
  • 774
  • 1
  • 7
  • 20