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");
}
});
这在Recyclerview Adapter内部被调用。单击一个项目时,我想创建一个片段的新实例,并在创建的Fragment中隐藏一些视图。
答案 0 :(得分:0)
您可以在片段参数上发送标志,并在片段类上检查该标志
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();
}
});
现在您可以在片段类中检查此标志
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;
}
}
希望我的回答对您有帮助。