Android 单击Gridview后如何打开片段

Android 单击Gridview后如何打开片段,android,gridview,android-fragments,Android,Gridview,Android Fragments,我已经在我的应用程序中为一组剧院创建了一个GridView。我的Theater类扩展了片段,它从JSON获取数据。我想要的是在点击gridview中的一个项目时打开另一个名为movie details的片段。 例如,如果我从gridview中单击一个剧院,那么moviedetails类应该打开并显示它的布局。我怎样才能做到这一点 我的戏剧课 package com.fortuna.cinemalk; import java.util.ArrayList; import android.app

我已经在我的应用程序中为一组剧院创建了一个GridView。我的Theater类扩展了片段,它从JSON获取数据。我想要的是在点击gridview中的一个项目时打开另一个名为movie details的片段。 例如,如果我从gridview中单击一个剧院,那么moviedetails类应该打开并显示它的布局。我怎样才能做到这一点

我的戏剧课

package com.fortuna.cinemalk;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.content.Intent;

    import android.widget.AdapterView;


    import com.fortuna.cinemalk.adapter.LazyAdapter;
    import com.fortuna.cinemalk.model.BaseElement;
    import com.fortuna.cinemalk.service.CommonVariable;
    import com.fortuna.cinemalk.service.JSONServices;
    import com.fortuna.cinemalk.util.Element;

    public class TheaterFragment extends Fragment {

        private GridView gridView;

        private ArrayList<BaseElement> filmTheater;
        private LazyAdapter adapter;
        private Activity activity;
        private CommonVariable commonVariable;

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

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

            activity = this.getActivity();

            commonVariable = (CommonVariable) activity.getApplication();

            gridView = (GridView) view.findViewById(R.id.gridView1);






                    new BackGround().execute();

            return view;
        }



        public class BackGround extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground(Void... params) {

                filmTheater = JSONServices.getTheater();
                return null;
            }

            @Override
            /* check again */
            protected void onPostExecute(Void result) {

                commonVariable.setTheater(filmTheater);

                adapter = new LazyAdapter(filmTheater, activity,
                        Element.THEATER_LIST.getType());

                gridView.setAdapter(adapter);

                super.onPostExecute(result);
            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
            }

        }

        } 

您可以在单击事件中使用FragmentTransaction:

FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
Fragment profileFragment = new MovieDetailFragment();//the fragment you want to show
profileFragment.setArguments(bundle);
fragmentTransaction
    .replace(R.id.content_frame, profileFragment);//R.id.content_frame is the layout you want to replace
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
注:

你的片段缺少一个空构造函数。

试试这个

    android.support.v4.app.Fragment detail = new MovieDetailFragment(); 
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();

谢谢。

我在getSupportFragmentManager()中遇到错误,当我替换内容框架中的布局时,请将此代码放入gridview的OnItemClick中。打开MovieDetailFragment。目前我没有gridview的McClick。你能帮我吗?因为我是androidFor gridview示例的超级初学者,请参考以下链接:你能告诉我如何通过单击gridview上的不同项目来添加多个片段吗?使用grid view制作剧院布局的最佳方法是什么?我需要建议。
    android.support.v4.app.Fragment detail = new MovieDetailFragment(); 
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();