我有一个连接到碎片的侧面导航抽屉。在一个片段中,我有三个CardView需要带我到其他片段。我见过的所有示例都使用带有ViewHolder的RecyclerView来实现这一点,但我不打算在这个上使用RecyclerView。我已经为其中一个CardView尝试了以下操作,但它似乎不起作用:
public class Dashboard extends Fragment implements View.OnClickListener {
View view;
CardView cardview1;
@Nullable
@Override
public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.dashboard_fragment, container, false);
cardview1 = (CardView) view.findViewById(R.id.cardview_a);
cardview1.setOnClickListener(this);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Title");
}
@Override
public void onClick(View v) {
Fragment fragment;
switch (v.getId()) {
case R.id.cardview_a:
fragment FragmentA();
break;
}
}
}
答案 0 :(得分:0)
@Override
public void onClick(View v) {
Fragment fragment;
switch (v.getId()) {
case R.id.cardview_a:
fragment = new FragmentA();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container,fragment);
transaction.commit();
break;
}
}