使用的Andr​​oid意图从活动打开一个片段 [英] Android using intent to open a fragment from an activity

查看:139
本文介绍了使用的Andr​​oid意图从活动打开一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现它在一个活动并为包含在全屏图像的每个图像一个片段图像的GridView控件的应用程序。当我点击任何图像的网格,它应该打开相应的片段。但是,我们不能使用意图做到这一点。
这里是我的code

I am implementing an app which has a gridview of images in one activity and one fragment for each image which contains the image in full screen. When i click on any of the images in the grid, it should open up the corresponding fragment. However, we cannot use intent to do this. here is my code

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub
            if(position==0)
            {
                Intent i=new Intent(Gallery.this,ImageFrag1.class);
                startActivity(i);
            }

和片段是

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ImageFrag1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.imagefrag1, container, false);
    }
}

这片段绑定到一个活动ImagesSwipe。那么,如何实现网格视图项及其相应片段之间的过渡。
谢谢

This fragment is bound to an activity ImagesSwipe. SO how do i achieve the transition between grid view item and its corresponding fragment. Thanks

推荐答案

您不需要一个片段一个图像。只需使用一个片段与ImageView的,在它的布局每一个形象。

You don't need one Fragment for one Image. Just reuse one Fragment with an ImageView in it's layout for every Image.

片段不是通过一个Intent调用类的活动。他们只能存在部分作为关活动,这是他们的设计。想想他们作为一个可重用的UI-MODUL的一个活动。要将片段添加到活动,你必须使用 FragmentManager FragmentTransaction 类,提供碎片的所有交互。

Fragments aren't invoked like Activities through an Intent. They can only exist as part off an Activity, that is what they are designed for. Think about them as a reusable UI-Modul for an Activity. To add a Fragment to an Activity you have to use the FragmentManager and the FragmentTransaction classes, which provide all interactions with Fragments.

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(YourFragment.newInstance(), null);
    ft.commit();

看一看从谷歌文档这个指南在这里介绍有关GridView的基本的东西。此外,你应该了解片段的。这里是一个教程了解你的方法。

Have a look at this guide from the Google Docs where the basic things about GridViews are described. In Addition you should read about Fragments. And here is a Tutorial about your approach.

这篇关于使用的Andr​​oid意图从活动打开一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆