单击Gridview中的项时,我想重定向到片段类。此gridview也位于片段类中。所以我的问题是如何从另一个片段类重定向到片段类

第一个片段类(使用gridview)

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





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





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

    }

    }


第二个片段类(单击gridview的某个项目时,我要去的类)

package com.fortuna.cinemalk;

import java.util.ArrayList;

import com.fortuna.cinemalk.adapter.PageAdapter;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.service.CommonVariable;
import com.fortuna.cinemalk.service.JSONServices;

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

public class MovieFragment extends Fragment {

    private ViewPager viewPager;
    private PageAdapter pageAdapter;
    private ArrayList<BaseElement> filmCategory;
    private Fragment fragment;
    private Activity activity;
    private CommonVariable commonVariable;

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

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

        fragment = this;

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        viewPager = (ViewPager) view.findViewById(R.id.news_page_viewpager);

        new BackGround().execute();

        return view;
    }

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

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

            filmCategory = JSONServices.getCategory();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            commonVariable.setCategory(filmCategory);

            pageAdapter = new PageAdapter(fragment.getChildFragmentManager(),
                    filmCategory, activity);

            viewPager.setAdapter(pageAdapter);

            super.onPostExecute(result);
        }

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

    }

}

最佳答案

尝试以下示例代码:

加载片段类:

        package com.example.fragmentexample;

        import android.app.Activity;
        import android.os.Bundle;
        import android.view.Menu;

        public class MainActivity extends Activity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                LoadFragment loadFragmentObj = new LoadFragment(getFragmentManager());
                loadFragmentObj.initializeFragment(new FragmentClass1());
            }


            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }


        }

使用按钮OnclickListener片段类1:
    package com.example.fragmentexample;

    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;

    public class LoadFragment {
        FragmentManager fragmentManager;
        public LoadFragment(FragmentManager fragmentManager)
        {
            this.fragmentManager = fragmentManager;
        }

          protected void initializeFragment(Fragment fragment) {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.frame_container, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
    }

片段类别2:
        package com.example.fragmentexample;

        import android.app.Fragment;
        import android.os.Bundle;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.ViewGroup;
        import android.widget.Button;

        public class FragmentClass1 extends Fragment {

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

                View rootView = inflater.inflate(R.layout.fragment1_layout, container, false);
                loadFragmentObj = new LoadFragment(getFragmentManager());
                Button loadSecondFragment = (Button)rootView.findViewById(R.id.button1);
                loadSecondFragment.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                         loadFragmentObj.initializeFragment(new FragmentClass2());
                    }
                });
                return rootView;

            }


        }

主要版式摘要:
package com.example.fragmentexample;

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

    public class FragmentClass2 extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);

            return rootView;

        }
    }

07-24 09:47