2017-04-11 79 views
0

當我運行我的應用程序時,一切正常工作,直到我需要片段被替換爲另一個片段。當用戶點擊列表視圖中的一個項目時,它應該打開新的佈局(新片段)。但現在,什麼都沒有發生,當我點擊了項目Android - 佈局留在當前片段

這是我的onCreate方法

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 


     JsonArrayRequest tournamentReq = new JsonArrayRequest(url, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 

         // Parsing json 
         for (int i = 0; i < response.length(); i++) { 
          try { 

           JSONObject obj = response.getJSONObject(i); 
           Tournament tournament = new Tournament(); 
           tournament.setTitle(obj.getString("title")); 
           tournament.setThumbnailUrl(obj.getString("image")); 
           tournament.setDate(((Number) obj.get("tdate")) 
             .doubleValue()); 
           // Genre is json array 
           JSONArray categoryArry = obj.getJSONArray("category"); 
           ArrayList<String> category = new ArrayList<String>(); 
           for (int j = 0; j < categoryArry.length(); j++) { 
            category.add((String) categoryArry.get(j)); 
           } 
           tournament.setCategory(category); 

           // adding tournament to tournament array 
           TournamentList.add(tournament); 

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 

         } 
         adapter.notifyDataSetChanged(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
      } 
     }); 

    AppController.getInstance().addToRequestQueue(tournamentReq); 
     } 

這是我onCreateView和

TournamentFragment

 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_tournament, container, false); 
     listView = (ListView) rootView.findViewById(android.R.id.list); 

     if (rootView.findViewById(R.id.frag_tour) != null){ 

     if (savedInstanceState != null) { 
      // Create an Instance of Fragment 
      TournamentFragment tFragment = new TournamentFragment(); 
      tFragment.setArguments(getActivity().getIntent().getExtras()); 
      getFragmentManager().beginTransaction() 
        .add(R.id.frag_tour, tFragment) 
        .commit(); 
     }}return rootView; 
} 

    public void OnSelectionChanged(int versionNameIndex) { 

    DescriptionFragment descriptionFragment = new DescriptionFragment(); 
    descriptionFragment.setArguments(getActivity().getIntent().getExtras()); 

    if (descriptionFragment != null){ 
     descriptionFragment.setDescription(versionNameIndex); 
    } else { 
     TournamentFragment tFragment = new TournamentFragment(); 

     fragmentTransaction.replace(R.id.fragment_container, tFragment); 
     fragmentTransaction.commit(); 
    } 
} 
OnSelectionChanged方法

fragment_description.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/fragment_container"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="This is where each individual description will appear" 
    android:id="@+id/version_description" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginLeft="@dimen/activity_horizontal_margin" 
    android:layout_marginRight="@dimen/activity_horizontal_margin" 
    android:gravity="center"/> 

</RelativeLayout> 

到目前爲止,我已經嘗試了不同類型的解決方案回答了在這裏計算器,但還是解決不了我的問題。 有人可以幫我理解這一點嗎?下面的代碼

getSupportFragmentManager() 
       .beginTransaction() 
       .detach(contentFragment) // detach the current fragment 
       .attach(contentFragment) // attach fragment that you want. 
       .commit(); 
+0

調用'R.id.drawer_layout' ......你確定這是'activity_main2.xml'的一部分? –

+0

@ cricket_007是 – nao

+0

爲什麼在'onCreate'中使用'replace',不應該只使用'add',因爲沒有Fragment呢? –

回答

0

使用通過

getActivity().getSupportFragmentManager() 
      .beginTransaction() 
      .replace(R.id.fragment_container, tFragment) 
      .fragmentTransaction 
      .commit(); 
+0

是它的onCreate片段類裏面的ID替換? – nao

+0

@nao'getSupportFragmentManager()'只能從'FragmentActivity'子類調用,而不是片段 –

+0

你可以檢查我的新編輯過的文章嗎? – nao