gpt4 book ai didi

android - 单击 Gridview 后如何打开 fragment

转载 作者:行者123 更新时间:2023-11-29 21:03:55 25 4
gpt4 key购买 nike

我在我的应用程序中为一组剧院创建了一个 GridView。我的 Theater 类扩展了 Fragment 并从 JSON 中获取数据。我想要的是在单击 gridview 中的项目时打开另一个名为电影详细信息的 fragment 。例如,如果我从我的 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;
}

}

最佳答案

试试这个

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

谢谢。

关于android - 单击 Gridview 后如何打开 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25114233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com