我在我的应用程序中为一组影院创建了一个GridView。我的Theater类扩展了Fragment,它从JSON获取数据。我想要的是在点击gridview中的项目时打开另一个称为电影细节的片段。 例如,如果我从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();
}
}
}
电影详细信息课程(这是我应该点击gridview中的项目的课程)
package com.fortuna.cinemalk;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MovieDetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shortfilm, container,
false);
return view;
}
}
答案 0 :(得分:1)
您可以在点击事件中使用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();
注意:
您的片段缺少空构造函数。
答案 1 :(得分:1)
试试这个
android.support.v4.app.Fragment detail = new MovieDetailFragment();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
感谢。