1

我有一個recyclerview,其縱向和橫向的佈局不同。當活動開始時,回收站視圖會填充數據而與方向無關。但是,如果方向改變,則不顯示數據。並沒有數據顯示隨之而來的方向變化。Recyclerview未在方向更改後填充數據

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    Log.d("CCC", "OnCreate View called"); 
    View view = inflater.inflate(R.layout.fragment_coach_list, container, false); 
    new NetworkConnector(getActivity(), coachListURL, method, null, new OnRequestComplete()); 
    Log.d("CCC", "Network connector called"); 
    return view; 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    Log.d("CCC", "On View created called"); 
    //if rv is declared in oncreateview then NPE during setadapter 
    coachRecyclerView = (RecyclerView) getActivity().findViewById(R.id.fcoachlist_rv); 
    final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
    Log.d("CCC", "orientation " + layoutManager.getOrientation()); 
    coachRecyclerView.setLayoutManager(layoutManager); 
} 

private class OnRequestComplete implements RequestCompleteListener<String> { 

    public OnRequestComplete() { 
    } 

    @Override 
    public void onRequestExecuted(String responseType, String result) { 
     if (!responseType.equals("error")) { 
      try { 
       JSONObject jsonResponse = new JSONObject(result); 
       Log.d("CCC", "Result: " + result); 
       String coachResult = jsonResponse.getString("success"); 
       switch (coachResult) { 
        case "1": 
         ArrayList<CoachList> coachListData = parseJSONResponse(jsonResponse); 
         coachListAdapter = new CoachListAdapter(getActivity()); 
         coachListAdapter.setCoachList(coachListData); 
         coachRecyclerView.setAdapter(coachListAdapter); 
         if(coachRecyclerView.getAdapter()== null){ 
          Log.d("CCC", "Adapter is null"); 
         } else if (coachRecyclerView.getAdapter()== coachListAdapter){ 
          Log.d("CCC", "Adapter is coachlistAdapter"); 
         } else { 
          Log.d("CCC", "This is odd!!"); 
         } 
         Log.d("CCC", "Size " + coachListAdapter.getItemCount()); 
         break; 
        case "0": 
         Toast.makeText(getActivity(), "No trainers available currently", Toast.LENGTH_LONG).show(); 
         break; 
        case "-1": 
         Toast.makeText(getActivity(), "Please try again later", Toast.LENGTH_LONG).show(); 
         break; 
       } 
      } catch (JSONException e) { 
       Log.d("CCC", "CoachList JSONException"); 
      } 
     } 
    } 
} 

NetworkConnector是一個執行抽氣stringrequest的類。

這是活動開始時方向未改變時的logcat。

03-18 07:06:57.702 30392-30392/in.jiyofit.the_app D/CCC: OnCreate View called 
03-18 07:06:57.722 30392-30392/in.jiyofit.the_app D/CCC: Network connector called 
03-18 07:06:57.722 30392-30392/in.jiyofit.the_app D/CCC: On View created called 
03-18 07:06:57.722 30392-30392/in.jiyofit.the_app D/CCC: orientation 1 
03-18 07:06:57.774 30392-30392/in.jiyofit.the_app W/EGL_genymotion: eglSurfaceAttrib not implemented 
03-18 07:06:57.774 30392-30392/in.jiyofit.the_app E/RecyclerView: No adapter attached; skipping layout 
03-18 07:06:57.778 30392-30392/in.jiyofit.the_app D/CCC: Result: {"success":"1","Coaches":[{"CoachID":"1","Coach_Name":"Tyler //lots of json 
03-18 07:06:57.778 30392-30392/in.jiyofit.the_app D/CCC: Adapter is coachlistAdapter 
03-18 07:06:57.778 30392-30392/in.jiyofit.the_app D/CCC: Size 4 
03-18 07:06:57.946 30392-30488/in.jiyofit.the_app D/dalvikvm: GC_FOR_ALLOC freed 414K, 6% free 8585K/9076K, paused 8ms, total 8ms 
03-18 07:06:59.426 30392-30392/in.jiyofit.the_app D/CCC: Result: {"success":"1","Coaches":[{"CoachID":"1","Coach_Name":"Tyler ...//lots of json 
03-18 07:06:59.426 30392-30392/in.jiyofit.the_app D/CCC: Adapter is coachlistAdapter 
03-18 07:06:59.426 30392-30392/in.jiyofit.the_app D/CCC: Size 4 

這是方向改變時的logcat。

03-18 07:13:53.792 30392-30392/in.jiyofit.the_app D/CCC: OnCreate View called 
03-18 07:13:53.796 30392-30392/in.jiyofit.the_app D/CCC: Network connector called 
03-18 07:13:53.796 30392-30392/in.jiyofit.the_app D/CCC: On View created called 
03-18 07:13:53.796 30392-30392/in.jiyofit.the_app D/CCC: orientation 1 
03-18 07:13:53.796 30392-30392/in.jiyofit.the_app D/CCC: OnCreate View called 
03-18 07:13:53.796 30392-30392/in.jiyofit.the_app D/CCC: Network connector called 
03-18 07:13:53.796 30392-30392/in.jiyofit.the_app D/CCC: On View created called 
03-18 07:13:53.796 30392-30392/in.jiyofit.the_app D/CCC: orientation 1 
03-18 07:13:53.856 30392-30392/in.jiyofit.the_app W/EGL_genymotion: eglSurfaceAttrib not implemented 
03-18 07:13:53.856 30392-30392/in.jiyofit.the_app E/RecyclerView: No adapter attached; skipping layout 
03-18 07:13:53.856 30392-30392/in.jiyofit.the_app E/RecyclerView: No adapter attached; skipping layout 
03-18 07:13:53.872 30392-30392/in.jiyofit.the_app D/dalvikvm: GC_FOR_ALLOC freed 554K, 7% free 9823K/10452K, paused 4ms, total 4ms 
03-18 07:13:53.888 30392-30392/in.jiyofit.the_app D/CCC: Result: {"success":"1","Coaches":[{"CoachID":"1","Coach_Name":"Tyler //lots of json 
03-18 07:13:53.888 30392-30392/in.jiyofit.the_app D/CCC: Adapter is coachlistAdapter 
03-18 07:13:53.888 30392-30392/in.jiyofit.the_app D/CCC: Size 4 
03-18 07:13:53.996 30392-30392/in.jiyofit.the_app E/RecyclerView: No adapter attached; skipping layout 
03-18 07:13:54.676 30392-30392/in.jiyofit.the_app D/CCC: Result: {"success":"1","Coaches":[{"CoachID":"1","Coach_Name":"Tyler //lots of json 
03-18 07:13:54.680 30392-30392/in.jiyofit.the_app D/CCC: Adapter is coachlistAdapter 
03-18 07:13:54.680 30392-30392/in.jiyofit.the_app D/CCC: Size 4 
03-18 07:13:54.736 30392-30392/in.jiyofit.the_app D/CCC: Result: {"success":"1","Coaches":[{"CoachID":"1","Coach_Name":"Tyler //lots of json 
03-18 07:13:54.736 30392-30392/in.jiyofit.the_app D/CCC: Adapter is coachlistAdapter 
03-18 07:13:54.736 30392-30392/in.jiyofit.the_app D/CCC: Size 4 

可以看出,代碼的某些部分被重複調用。我不知道爲什麼。在方向更改後調用onViewCreated之後,再次調用OnCreateView。可能這是造成這個問題的原因。任何幫助,將不勝感激。謝謝。

+0

你確定你的代碼沒有添加該片段的多個實例嗎? – Luksprog

+0

只調用片段的單個實例 – suku

+0

如果您只是想讓屏幕旋轉,但沒有具體取決於佈局,那麼我相信您必須重寫活動中的onConfigurationChanged方法。還將其添加到Manifest文件中。 更多解釋請看這裏: http://stackoverflow.com/a/2418763/1750013 –

回答

3

這是通過在活動中保存片段的實例來解決的。將佈局管理器的狀態保存在片段中並在旋轉時恢復它並不能解決此問題。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //this activity extends baseactivity 
    //inflating activity layout in a framelayout inside baseactivity 
    getLayoutInflater().inflate(R.layout.activity_hire_coach, contentFrameLayout); 
    if (savedInstanceState != null) { 
     //Restore the fragment's instance 
     coachListFragment = (CoachListFragment) getFragmentManager().getFragment(savedInstanceState, "coachListFrag"); 
    } else { 
     coachListFragment = new CoachListFragment(); 
     getFragmentManager().beginTransaction().add(R.id.ahirecoach_layout, coachListFragment, "coachListFragment").commit(); 
    } 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    //Save the fragment's instance 
    getFragmentManager().putFragment(outState, "coachListFrag", coachListFragment); 
} 

參考:https://stackoverflow.com/a/17135346/5512020

0
protected void onSaveInstanceState(Bundle state) { 
super.onSaveInstanceState(state); 
// Save list state 
mListState = mLayoutManager.onSaveInstanceState(); 
state.putParcelable(LIST_STATE_KEY, mListState); 
} 
Restore state in the onRestoreInstanceState(): 

protected void onRestoreInstanceState(Bundle state) { 
super.onRestoreInstanceState(state); 

// Retrieve list state and list/item positions 
if(state != null) 
mListState = state.getParcelable("myState"); 
} 
Then update the LayoutManager (I do in onResume()): 

@Override 
protected void onResume() { 
super.onResume(); 

if (mListState != null) { 
mLayoutManager.onRestoreInstanceState(mListState); 
} 
} 
+0

我試過了。它沒有工作。問題不在於丟失數據。即使再次檢索數據,也不會顯示。回收站視圖保持空白。適配器也被設置。 – suku

1

在一個片段,一個簡單的方法在旋轉時保持數據是創建適配器的onCreate()也有設置的片段,以保持其狀態setRetainInstance(true)onResume()將適配器應用於recyclerview,還調用adapter.notifyDataSetChanged()來填充您的recyclerView。這就是它爲我工作的方式。