我试图在我的arraylist适配器中添加一个onClick侦听器,当单击它时将打开一个片段。当我单击按钮时,原始片段消失了,但是新片段没有显示。
viewHolder.commentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AppCompatActivity activity = (AppCompatActivity) view.getContext();
CommentFragment commentFragment = new CommentFragment();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.viewPager , commentFragment).addToBackStack(null).commit();
}
});
我的新片段
public class CommentFragment extends Fragment {
View v;
ImageView backArrow;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
v = inflater.inflate(R.layout.comment_fragment, container,false);
backArrow = v.findViewById(R.id.backarrow);
Log.i("inflated","Should be inflated");
backArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getActivity().getSupportFragmentManager().popBackStack();
}
});
return v;
}
public CommentFragment() {
}
}