簡體   English   中英

Internet連接恢復時如何刷新片段

[英]How to Refresh the fragment, when Internet Connection is Resume

我有一個片段,首先檢查互聯網連接的可用性。 如果可以,請點擊重試按鈕以刷新頁面。 出現的問題是,我無法刷新片段這是我的片段代碼。

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后空白。 屏幕上沒有任何內容。

建立互聯網連接后,您不應刷新片段,而應刷新列表

由於方法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();
            }
       //
    }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM