2016-08-03 88 views
0
public class Fragment_01 extends ListFragment { 

String string[]={"Contact 1","Contact 2","Contact 3","Contact 4","Contact 5"}; 



    public View onCreateView(LayoutInflater inflater, ViewGroup container ,Bundle saveInstanceState) { 

     View myview = inflater.inflate(R.layout.fragment_fragment_01, container, false); 
     ListView listView= (ListView) myview.findViewById(android.R.id.list); 
     ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,string); 
     listView.setAdapter(array); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 


      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      FragmentManager fm = getFragmentManager(); 
      FragmentTransaction ft = fm.beginTransaction(); 
      Fragment_03 f3 = new Fragment_03(); 
      ft.replace(R.id.linearLayout2, f3); 
      ft.commit(); 




      } 

     }); 
     return myview; 
    } 


     } 
+4

什麼是你的問題? – Bobby

回答

0

不要在另一個片段內更改片段。在活動中進行框架佈局。點擊列表視圖中的項目,通過接口將事件發送到活動,並在活動中替換片段。這是替換碎片的正確方法。

0
Fragment_03 f3 = new Fragment_03(); 
FragmentManager fragmentManager = ((FragmentActivity) getContext()).getSupportFragmentManager(); 
fragmentManager.beginTransaction() 
    .replace(R.id.content_frame, new Fragment_03()) 
    .commit(); 
+0

仍然無法正常工作 –

0
//I am using this code without listView and this is working fine then why the other is not working ?? 

public class Fragment_01 extends Fragment { 



    public View onCreateView(LayoutInflater inflater, ViewGroup container ,Bundle saveInstanceState){ 

    View myview=inflater.inflate(R.layout.activity_fragment_01,container,false); 
     Button btn = (Button) myview.findViewById(R.id.button); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 


       FragmentManager fm =getFragmentManager(); 
       FragmentTransaction ft =fm.beginTransaction(); 
       Fragment_03 f3 = new Fragment_03(); 
       ft.replace(R.id.linearLayout2,f3); 
       ft.commit(); 
      } 
     }); 
    return myview; 

} 

}