我有一个片段,并且线性布局包含2个简单的卡片视图。
如何制作卡片查看可点击的卡片视图?
我已搜索过,但所有主题都是关于回收商视图中的卡片视图...但我有一个简单的可点击卡片视图。
public class popFragment extends Fragment {
public popFragment()
{
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.popfragment,container,false);
return view;
}
}
答案 0 :(得分:0)
您只需将布局xml中的ID分配给每个卡片视图即可。然后在片段的oncreateView()中绑定视图并使用mycardView.setOnclickListener ....
public class popFragment extends Fragment {
private CardView cardView1;
private CardView cardView2;
public popFragment()
{
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.popfragment,container,false);
cardView1 = view.findViewById(R.id.my_card_view_1);
cardView2 = view.findViewById(R.id.my_card_view_2);
cardView1.setOnClickListener(v->{
//set on click functions here
});
cardView2.setOnClickListener(v->{
//set on click functions here
});
return view;
}
}
不要忘记在layout.xml中分配相应的ID