当前位置: 首页 > 知识库问答 >
问题:

RecolyerView在方向更改后未填充数据

范瀚昂
2023-03-14

我有一个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");
            }
        }
    }
}
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

共有1个答案

柴宏阔
2023-03-14

这是通过在活动中保存片段的实例来解决的。在片段中保存layoutmanager的状态并在循环时还原它并不能解决此问题。

@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

 类似资料:
  • 请注意,此问题不是以下问题的重复: https://stackoverflow.com/questions/19006776/onactivityresult-not-working-with-fragments onActivityResult不处理片段 此外,之前也有人问过类似的问题,但这并没有提到方向的变化(目前尚未解决)。 如果不切换方向,则会调用片段中的onActivityResult方

  • 我正试图为我的Android应用建立一个基于美国用户的Firebase受众群。从我收集到的信息来看,用户的国家是一个属性,通过与Firebase SDK集成就可以直接收集到。我在analytics dashboard中构建了一个受众(标准是用户属性->Country->完全匹配“我们”),但我仍然没有看到受众中有任何用户。自从我创建观众以来已经超过3天了,因为我从其他几个问题中读到,观众不是追溯填

  • 我正在比较填充整数列表所需的时间与整数向量。 每个矢量 令我惊讶的是,填充列表比填充整数向量快100倍。我希望填充整数的向量要快得多,因为向量在内存中是连续的,插入要快得多。 填充列表怎么会比填充向量快100倍而不是10倍呢?我肯定我缺少一些导致这种情况的概念或想法。 这是我用来生成结果的代码 有人能给我解释一下为什么会这样吗???

  • 当我试图更改数据库中表的模式/数据时,我遇到了一个无法解决的问题。我正在用Rooms.CreateFromAsset方法预填充数据库。 当我使用以下代码构建数据库时: 和这个数据类:

  • 问题内容: 我对PostgreSQL相对较新,并且我知道如何在SQL Server中用左数零填充数字,但是我在PostgreSQL中努力解决这个问题。 我有一个数字列,其中最大位数为3,最小位数为1:如果是一位,它的左边有两个零,如果是两位,则有1,例如001、058、123。 在SQL Server中,我可以使用以下命令: 在PostgreSQL中不存在。任何帮助,将不胜感激。 问题答案: 您可

  • 我有一个非常简单的Java/Spring应用程序来演示KStream的功能,但不幸的是,我无法使KStream加载数据。想法是创建一个KStream对象,并使用controller GET方法简单地检索其内容。示例代码: 问题-主题中有消息,但foreach(...)中的KStream枚举没有从中检索任何结果。KStream对象状态为“RUNning”,日志中没有错误。 生成随机应用程序ID并将A