简体   繁体   English

可点击的卡片视图简单,无需回收者视图

[英]clickable card view simple without recycler view

I have a fragment and have linear layout that contains 2 simple card views. 我有一个片段,并具有包含2个简单卡片视图的线性布局。

How can I make card view clickable cardviews? 如何使卡片视图可点击的卡片视图?

I have searched, but all topics are about cardviews in recycler view ... But I have a simple clickable cardview. 我已经搜索过,但是所有主题都与回收者视图中的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;
    }
}

You can simply assign id in your layout xml to each card view. 您只需在布局xml中为每个卡片视图分配ID。 then in oncreateView() of the fragment bind the view and use mycardView.setOnclickListener.... . 然后在片段的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;
        }
    }

do not forget to assign the corresponding ids in your layout.xml 不要忘记在您的layout.xml中分配相应的ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM