时从一个片段移动到另一个片段

时从一个片段移动到另一个片段

本文介绍了如何在 Android 中单击 ImageView 时从一个片段移动到另一个片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ImageView.我想通过单击 Imageview 从一个片段移动到另一个片段,就像我们可以使用

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?

我的代码如下:

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;
    }
}

推荐答案

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();
    }
});

你写了上面的代码......我们正在用我们的片段替换 R.id.content_frame.

You write the above code...there we are replacing R.id.content_frame with our fragment.

这篇关于如何在 Android 中单击 ImageView 时从一个片段移动到另一个片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:51