Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中点击ImageView从一个片段移动到另一个片段?_Android_Android Fragments_Fragment - Fatal编程技术网

如何在Android中点击ImageView从一个片段移动到另一个片段?

如何在Android中点击ImageView从一个片段移动到另一个片段?,android,android-fragments,fragment,Android,Android Fragments,Fragment,我有一个图像视图。我想通过单击Imageview从一个片段移动到另一个片段,就像我们可以使用 Intent i=new Intent(MainActivity.this,SecondActivity.class); startActivity(i); 我该怎么做?谁能一步一步地给我解释一下吗 我的代码如下: mycontacts.class public class mycontacts extends Fragment { public mycontacts() { }

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

Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
我该怎么做?谁能一步一步地给我解释一下吗

我的代码如下:

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;

    } 
}
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;
    }
}
任务。类

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;

    } 
}
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;
    }
}
您编写了上面的代码…在这里,我们将用片段替换R.id.content\u框架。
希望这对您有所帮助

您可以使用FragmentManager事务转移到另一个片段。不能像活动一样调用片段,。碎片存在于活动的存在上

您可以通过编写以下代码来调用另一个片段:

        FragmentTransaction t = this.getFragmentManager().beginTransaction();
        Fragment mFrag = new MyFragment();
        t.replace(R.id.content_frame, mFrag);
        t.commit();
这里的“R.id.content\u frame”是要替换片段的布局的id


您还可以添加另一个片段以备替换。

在onClickListener.onClick中,放置

getFragmentManager().beginTransaction().replace(R.id.container, new tasks()).commit();
换句话说,在你的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;

    }
}

现在,请记住
R.id.container
是调用片段的活动的容器(框架布局或其他布局)

将此代码添加到要单击并加载片段的位置。我希望这对你有用


Fragment fragment = new yourfragment();
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();


当您在活动中并且需要转到下面的片段时,请使用

getFragmentManager().beginTransaction().replace(R.id.*TO\u BE\u REPLACED\u LAYOUT\u id*,new tasks()).commit()

但是,当您在一个片段中,需要转到一个片段时,只需在前面添加一个
getActivity()。
,它就会变成

getActivity().getFragmentManager().beginTransaction().replace(R.id.*TO\u BE\u REPLACED\u LAYOUT\u id*,new tasks()).commit()

就这么简单


*待替换\u布局\u ID*
可以是活动的整个页面,也可以是活动的一部分,只需确保为待替换的布局添加ID即可。一般做法是将可替换布局放在
FrameLayout

中。如果要查找上述代码的Kotlin版本,可以用这种方式执行,并且可以在单击Listener或任何需要的地方调用replaceFragment(RequiredFragment())

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_profile, container, false);
    notification = (ImageView)v.findViewById(R.id.notification);

    notification.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentTransaction fr = getFragmentManager().beginTransaction();
            fr.replace(R.id.container,new NotificationFragment());
            fr.commit();
        }
    });

    return v;
}
private fun replaceFragment(fragment: Fragment) {
val transaction = activity!!.supportFragmentManager.beginTransaction()
transaction.replace(R.id.frame, fragment)
transaction.commit()
}

我猜你是新来的碎片..?我说的对吗..?碎片碎片=新任务();我收到错误
无法将片段转换为任务
。怎么办?任务扩展了fragment…所以任务只是fragment的子类,请确保您的导入语句导入android.support.v4.app.fragment;导入android.support.v4.app.FragmentManager;导入android.support.v4.app.FragmentTransaction;如果仍然有错误,那么发布日志消息STASKS片段的布局id为
r.id.task
。我的当前片段具有布局id
r.id.contact
。那么我应该写什么来代替
r.id.content\u frame
?好的..你现在在MyContacts fragment na中..你如何开始…?检查..你可以通过替换视图从某处调用MyContacts fragment..所以这里你也必须提到相同的视图。。希望这对你有帮助。哦,我寻找答案已经多久了!非常感谢。不过,一开始,变量任务的名称(?!)令人困惑。然后,他漫不经心地说,他称自己为碎片任务Fragment=新任务();我收到错误消息,无法将片段转换为任务。怎么办请检查导入,使用supportedFragment或Fragmentsasks fragment具有布局id r.id.task。我的当前片段具有布局id r.id.contact。那么我应该在r.id.content\u框架中写什么呢?