gpt4 book ai didi

android - 从另一个 fragment 类重定向到一个 fragment 类

转载 作者:行者123 更新时间:2023-11-29 17:47:56 30 4
gpt4 key购买 nike

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

第一个 fragment 类(使用哪个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();
}

}

}

第二个 fragment 类(这是我想在点击 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;
}


}

加载 fragment 类:

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

Fragemnt Class1 with Button OnclickListener :

        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;

}


}

2 类 fragment :

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;

}
}

主要布局 fragment :

  <FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

关于android - 从另一个 fragment 类重定向到一个 fragment 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25116254/

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