[英]clickable card view simple without recycler view
我有一個片段,並具有包含2個簡單卡片視圖的線性布局。
如何使卡片視圖可點擊的卡片視圖?
我已經搜索過,但是所有主題都與回收者視圖中的cardview有關。但是我有一個簡單的可點擊cardview。
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;
}
}
您只需在布局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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.