2016-08-03 8 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; 

} 

} 
関連する問題