当Internet连接恢复时,如何刷新片段

时间:2018-03-15 15:46:06

标签: android fragment internet-connection

我有一个片段,首先我检查互联网连接的可用性。如果它可用,那么,我想通过单击重试按钮刷新页面。出现的问题是,我无法刷新片段 这是我的片段代码。

public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.homework, container, false);

            listView = rootView.findViewById(R.id.home_list);

            adapter = new HomeWorkAdapter(getContext(), R.layout.home_single, arrayList);

            listView.setAdapter(adapter);

            cal = rootView.findViewById(R.id.date_select);



        boolean x = handleNetworkConnection();
        if (x == true) {
            methodListener();
            SharedPreferences preferences = getActivity().getSharedPreferences("user_login", Context.MODE_PRIVATE);

            HashMap<String, String> map = new HashMap<>();
            map.put("school_id", preferences.getString("school_id", ""));
            map.put("class", preferences.getString("classes", ""));
            map.put("sec", "homeera");

            defaultfetch(map);

        }
        else
        {
             final LinearLayout ll = new LinearLayout(getActivity());
            TextView textView = new TextView(getActivity());
            Button button = new Button(getActivity());

            ImageView image = new ImageView(getActivity());
            ll.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
            ll.setBackgroundColor(Color.parseColor("#ecf0f1"));
            image.setImageResource(R.drawable.errorsq);
            button.setBackgroundColor(Color.parseColor("#02b48a"));

            button.setText("Try Again");
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
/*
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.detach(getActivity().).attach(this).commit();
*/

                }
            });
            ll.addView(textView);
            ll.addView(image);
            ll.addView(button);
            ll.setOrientation(LinearLayout.VERTICAL);




            View rootview = ll;

            return rootview;
        }

            return rootView;
        }

在这段代码中,我已经应用了一种刷新片段的方法,但问题是刷新后的UI变为空白。屏幕上没有任何内容。

1 个答案:

答案 0 :(得分:0)

在获得互联网连接时,不应刷新片段,而应刷新列表

由于您的方法handleNetworkConnection()同步返回,您可以按如下方式组织代码:

public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.homework, container, false);
        listView = rootView.findViewById(R.id.home_list);
        adapter = new HomeWorkAdapter(getContext(), R.layout.home_single, arrayList);
        listView.setAdapter(adapter);
        cal = rootView.findViewById(R.id.date_select);

        prepareList();
 }

 void prepareList() {
     boolean x = handleNetworkConnection();
    if (x == true) {
       // same code as now, load the list content and display it
    } else {
       // same code as now except the click listener:
           public void onClick(View view) {
                prepareList();
            }
       //
    }
 }