How to move from one fragment to another fragment

2019-01-15 06:39发布

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

Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);

How can I do this? Can anyone explain to me step by step?

My codes are as follows:

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
            //how to go to tasks fragment from here???
            }
        });
        return view;

    } 
}

tasks.class

public class tasks extends Fragment {
    public tasks() { 
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_layout_one, container,
            false);

        return view;
    }
}

3条回答
闹够了就滚
2楼-- · 2019-01-15 07:08
purple.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Fragment fragment = new tasks();
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
});

you write the above code...there we are replacing R.id.content_frame with our fragment. hope this helps you

查看更多
够拽才男人
3楼-- · 2019-01-15 07:16

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

查看更多
▲ chillily
4楼-- · 2019-01-15 07:26

you can move to another fragment by using the FragmentManager transactions. Fragment can not be called like activities,. Fragments exists on the existance of activities.

You can call another fragment by writing the code below:

        FragmentTransaction t = this.getFragmentManager().beginTransaction();
        Fragment mFrag = new MyFragment();
        t.replace(R.id.content_frame, mFrag);
        t.commit();

here "R.id.content_frame" is the id of the layout on which you want to replace the fragment.

you can also add the other fragment incase of replace.

查看更多
登录 后发表回答