Android 方向更改后,Recyclerview未填充数据
我有一个recyclerview,有不同的纵向和横向布局。当活动开始时,recyclerview将填充数据,而与方向无关。但如果方向改变,则不会显示任何数据。并且没有显示后续方向变化的数据Android 方向更改后,Recyclerview未填充数据,android,android-recyclerview,screen-orientation,orientation-changes,Android,Android Recyclerview,Screen Orientation,Orientation Changes,我有一个recyclerview,有不同的纵向和横向布局。当活动开始时,recyclerview将填充数据,而与方向无关。但如果方向改变,则不会显示任何数据。并且没有显示后续方向变化的数据 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("CCC", "OnCreate View called"); View vie
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");
}
}
}
}
这是方向改变时的日志
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
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。这可能是问题的根源。任何帮助都将不胜感激。谢谢。通过在活动中保存片段实例解决了此问题。在片段中保存layoutmanager的状态并在旋转时恢复它并不能解决此问题
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);
}
}
@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);
}
参考:在片段中,在旋转时保留数据的一种简单方法是创建适配器onCreate(),并将片段设置为保留其状态
setRetainInstance(true)
onResume()
将适配器应用于您的recyclerview,同时调用adapter.notifyDataSetChanged()来填充您的recyclerview。这就是我的工作原理。你确定你的代码没有添加该片段的多个实例吗?只调用片段的一个实例。如果你只想允许屏幕旋转,但不想根据布局进行任何特定操作,那么我相信你必须覆盖活动中的onConfigurationChanged方法。还要将其添加到清单文件中。更多解释请看这里:我得到了解决它的答案。下面提到了这一点,但我还不能将其标记为正确答案。但是你的回答不起作用,因为我需要再次调用oncreate。我尝试了这个。它不起作用。问题不在于丢失数据。即使再次检索数据,也不会显示。recyclerview仍为空。适配器也已设置。