2014-08-29 41 views
0

我有一个主Activity.inside主要活动我有片段与gridview,我想通过gridview点击更改我的片段。如何用新的片段替换旧的片段另一个片段的gridview点击

任何帮助......

这是我的MainActivity类别

public class MainActivity extends Activity implements Fragment_name_list.Communicator  { 
     Fragment_name_list f1; 
     Fragment_tests f2; 
     FragmentManager manager; 

     Fragment_addStudentDetails f3; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 

    manager=getFragmentManager(); 
    f1 = (Fragment_name_list) manager.findFragmentById(R.id.fragment); 
    f1.setCommunicator(this); 
    //ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,result); 

} 

@Override 
public void respond(int index) { 
    f2 = (Fragment_tests) manager.findFragmentById(R.id.fragment2); 
    f3 = (Fragment_addStudentDetails) manager.findFragmentById(R.id.fragment3); 
    switch (index){ 
     case 1: 
      ///Intent i = new Intent(this,Group_AddStudent_details.class); 
      ///i.putExtra("index", index); 
      //startActivity(i); 
      f2.changeData(index); 
      break; 
     case 2: 

      break; 
     case 3: 

      break; 
     case 4: 
      break; 
    } 

} 

}

这是我的片段测试类

 public class Fragment_tests extends Fragment { 
TextView text; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_test,container,false); 
    text = (TextView) view.findViewById(R.id.textViewp); 
    return view; 
} 

public void changeData(int index){ 
    String[] result= getResources().getStringArray(R.array.result); 
    text.setText(result[index]); 
} 

}

这是我Fragmnet增加学生类

 public class Fragment_addStudentDetails extends Fragment { 
TextView text; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_add_student_details,container,false); 
    text = (TextView) view.findViewById(R.id.textViewp); 
    return view; 
} 

}

我想fragmnet_add_student_details来代替我的fragmnet_test。 plzz help

回答

1

首先,不要把你的碎片放在主Activity的布局xml中。我不明白你是否确实从你的代码中这样做了,但是我怀疑你正在做的是因为你要保留的字段指向你正在使用的两个碎片。

你想要做的是有一个ViewGroup的某种(FrameLayout将工作),你把你的片段进入编程。使用FragmentTransaction上的replace将现有片段替换为另一片段。

getFragmentManager().beginTransaction() // 
    .replace(R.id.fragmentContainer, new BlahFragment()) // 
    .commit(); 

另外,不要在碎片上使用变异方法。如果数据发生变化,只是换了片段与上述代码片段,并通过记录的模式让自己一个新的片断:

BlahFragment.newInstance(String[] data); 

我把从文档的例子:

public static class DetailsFragment extends Fragment { 
    /** 
    * Create a new instance of DetailsFragment, initialized to 
    * show the text at 'index'. 
    */ 
    public static DetailsFragment newInstance(int index) { 
     DetailsFragment f = new DetailsFragment(); 

     // Supply index input as an argument. 
     Bundle args = new Bundle(); 
     args.putInt("index", index); 
     f.setArguments(args); 

     return f; 
    } 
} 
+0

感谢帮助... – kosala 2014-08-29 07:58:27

相关问题