我有一個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。可能這是造成這個問題的原因。任何幫助,將不勝感激。謝謝。
你確定你的代碼沒有添加該片段的多個實例嗎? – Luksprog
只調用片段的單個實例 – suku
如果您只是想讓屏幕旋轉,但沒有具體取決於佈局,那麼我相信您必須重寫活動中的onConfigurationChanged方法。還將其添加到Manifest文件中。 更多解釋請看這裏: http://stackoverflow.com/a/2418763/1750013 –