在我的应用程序中我有Gridview和TabActivity。 我的Rice.class有gridview, 在MainActivity.class中有tabActivity, myChild选项卡是FragmentA,FragmentB,FragmnetC。 我想把我的gridview索引传递给FragmnetA.how我可以解决这个问题。
Rice.class
public class Rice extends ActionBarActivity {
FragmentA fa;
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rice);
gridView=(GridView) findViewById(R.id.gridViewR);
gridView.setAdapter(new MyRAdapter(this));
Intent intent = getIntent();
//Breakfast bf = (Breakfast) getFragmentManager().findFragmentById(R.id.fragment2);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.putExtra("index",i);
startActivity(intent);
}
});
}
}
class MyRAdapter extends BaseAdapter {
private Context mContext;
public MyRAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
//imageView.setLayoutParams(new GridView.LayoutParams(450, 200));
//imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.back_1, R.drawable.back_2,
R.drawable.back_3, R.drawable.back_4,
R.drawable.back_5, R.drawable.back_6,
R.drawable.back_11,
};
}
MainActivity.class
public class MainActivity extends FragmentActivity {
ViewPager viewPager=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyFrAdapter(fragmentManager));
Intent intent = getIntent();
int index = intent.getIntExtra("index",0);
}
}
class MyFrAdapter extends FragmentPagerAdapter{
public MyFrAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
if(i==0){
fragment = new FragmentA();
}
if(i==1){
fragment = new FragmentB();
}
if(i==2){
fragment = new FragmentC();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0){
return "Ingredient";
}
if(position == 1){
return "Recipe";
}
if(position == 2){
return "Summary";
}
return null;
}
}
FragmentA.class
public class FragmentA extends Fragment {
TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a,container,false);
textView = (TextView) view.findViewById(R.id.textView);
return view;
}
}
答案 0 :(得分:0)
public class MainActivity extends FragmentActivity {
int mIndex;
ViewPager viewPager=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyFrAdapter(fragmentManager));
Intent intent = getIntent();
mIndex = intent.getIntExtra("index",0);
}
class MyFrAdapter extends FragmentPagerAdapter{
public MyFrAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
if(i==0){
fragment = new FragmentA().newInstance(mIndex);
}
if(i==1){
fragment = new FragmentB();
}
if(i==2){
fragment = new FragmentC();
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0){
return "Ingredient";
}
if(position == 1){
return "Recipe";
}
if(position == 2){
return "Summary";
}
return null;
}
}
}
//...
public class FragmentA extends Fragment {
TextView textView;
int FragmentA_index;
public static final String EXTRA_INDEX = "mypackage.index";
public static FragmentA newInstance(int index) {
Bundle args = new Bundle();
args.putInt(EXTRA_INDEX, index);
FragmentA fragment = new FragmentA();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentA_index = getArguments().getInt(EXTRA_INDEX);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a,container,false);
textView = (TextView) view.findViewById(R.id.textView);
return view;
}
}