【发布时间】:2016-07-05 03:05:32
【问题描述】:
我有一个纵向和横向布局不同的回收站视图。当活动开始时,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 是一个执行 volley stringrequest 的类。
这是 Activity 启动且方向未更改时的 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。可能这是导致问题的原因。任何帮助,将不胜感激。谢谢。
【问题讨论】:
-
您确定您的代码没有添加该片段的多个实例吗?
-
只调用片段的单个实例
-
如果您只想允许屏幕旋转但没有具体取决于布局,那么我相信您必须覆盖活动中的 onConfigurationChanged 方法。也将其添加到清单文件中。更多解释请看这里:stackoverflow.com/a/2418763/1750013
-
我得到了解决它的答案。它在下面提到,但我还不能将其标记为正确答案。但你的回答不会奏效,因为无论如何我都需要再次调用 oncreate。
标签: android android-recyclerview screen-orientation orientation-changes