How to move from one fragment to another fragment on click of an ImageView in Android?

后端 未结 6 1305
不知归路
不知归路 2020-12-03 10:16

I have an ImageView. I want to move from one fragment to another fragment on a click of an Imageview, the same way like we can move from one activity to another using

<
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 11:02

    inside your onClickListener.onClick, put

    getFragmentManager().beginTransaction().replace(R.id.container, new tasks()).commit();
    

    In another word, in your mycontacts.class

    public class mycontacts extends Fragment {
    
        public mycontacts() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            final View v = super.getView(position, convertView, parent);
            ImageView purple = (ImageView) v.findViewById(R.id.imageView1);
            purple.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    getFragmentManager()
                            .beginTransaction()
                            .replace(R.id.container, new tasks())
                            .commit();
                }
            });
            return view;
    
        }
    }
    

    now, remember R.id.container is the container (FrameLayout or other layouts) for the activity that calls the fragment

提交回复
热议问题