如何使recyclerview不可见?

9jyewag0  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(324)

有一个 RecyclerView 它从服务器获取数据,然后将其存储在手机的缓存中,然后将其显示在设备上。问题是我有两个相同的 RecyclerViews 立即显示,从internet获取数据的和已将数据保存到缓存并显示的。你能不能设法使 RecyclerView 从互联网上看不到的数据?仅显示保存在缓存中的数据。
或者使缓存中存储的数据仅在没有寄宿学校时显示?我想会更好。 Fragment 一切都发生在哪里

public class FragmentRink extends Fragment {
    List<ItemSkatingRinkSchedule_2> mSkatingRinkScheduleList;

    RecyclerView recyclerView;
    RecyclerView.LayoutManager recyclerViewlayoutManager;
    RecyclerView.Adapter recyclerViewadapter;
    String URL = "";
    String GET_JSON_FROM_SERVER_NUMBER = "session_number";
    String GET_JSON_FROM_SERVER_START = "session_start";
    String GET_JSON_FROM_SERVER_END = "session_end";
    String GET_JSON_FROM_SERVER_WEEKDAYS = "weekdays";
    String GET_JSON_FROM_SERVER_WEEKDAYS_AND_HOLIDAYS = "weekends_and_holidays";
    RequestQueue requestQueue;

    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        OnBackPressedCallback callback = new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                Intent homeIntent = new Intent(Intent.ACTION_MAIN);
                homeIntent.addCategory(Intent.CATEGORY_HOME);
                homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(homeIntent);
            }
        };

        requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_rink, container, false);
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {

        recyclerView = requireView().findViewById(R.id.recy_srs);

        mSkatingRinkScheduleList = new ArrayList<>();
        recyclerViewadapter = new AdapterSkatingRinkSchedule_2((ArrayList<ItemSkatingRinkSchedule_2>) mSkatingRinkScheduleList);

        recyclerViewlayoutManager = new LinearLayoutManager(getContext());

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(recyclerViewlayoutManager);
        recyclerView.setAdapter(recyclerViewadapter);
        JSON_DATA_WEB_CALL();

        mSwipeRefreshLayout = requireView().findViewById(R.id.ref);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.color_8);
        mSwipeRefreshLayout.setOnRefreshListener(() -> {
            mSkatingRinkScheduleList.clear();
            JSON_DATA_WEB_CALL();
            mSwipeRefreshLayout.setRefreshing(false);
        });
    }

    public void JSON_DATA_WEB_CALL() {
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL, response ->
        {
            for (int i = 0; i < response.length(); i++) {
                try {
                    ItemSkatingRinkSchedule_2 item = new ItemSkatingRinkSchedule_2();
                    JSONObject json;
                    json = response.getJSONObject(i);
                    item.set_session_number(json.getString(GET_JSON_FROM_SERVER_NUMBER));
                    item.set_session_start(json.getString(GET_JSON_FROM_SERVER_START));
                    item.set_session_end(json.getString(GET_JSON_FROM_SERVER_END));
                    item.set_weekdays(json.getString(GET_JSON_FROM_SERVER_WEEKDAYS));
                    item.set_weekends_and_holidays(json.getString(GET_JSON_FROM_SERVER_WEEKDAYS_AND_HOLIDAYS));

                    mSkatingRinkScheduleList.add(item);
                }
                catch (JSONException e) {
                    e.printStackTrace();

                }
            }

            recyclerViewadapter.notifyDataSetChanged();
        }, error ->
                Log.e("Volley", error.toString())) {
            @Override
            protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
                try {
                    Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
                    if (cacheEntry == null) {
                        cacheEntry = new Cache.Entry();
                    }
                    final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
                    long now = System.currentTimeMillis();
                    final long ttl = now + cacheExpired;
                    cacheEntry.data = response.data;
                    cacheEntry.softTtl = now;
                    cacheEntry.ttl = ttl;
                    String headerValue;
                    headerValue = response.headers.get("Date");
                    if (headerValue != null) {
                        cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }
                    headerValue = response.headers.get("Last-Modified");
                    if (headerValue != null) {
                        cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }
                    cacheEntry.responseHeaders = response.headers;
                    final String jsonString = new String(response.data,
                            HttpHeaderParser.parseCharset(response.headers));
                    return Response.success(new JSONArray(jsonString), cacheEntry);
                } catch (UnsupportedEncodingException | JSONException e) {
                    return Response.error(new ParseError(e));
                }
            }

            @Override
            protected void deliverResponse(JSONArray response) {
                super.deliverResponse(response);
            }

            @Override
            public void deliverError(VolleyError error) {
                super.deliverError(error);
            }

            @Override
            protected VolleyError parseNetworkError(VolleyError volleyError) {
                return super.parseNetworkError(volleyError);
            }
        };
        requestQueue = Volley.newRequestQueue(requireContext());
        requestQueue.add(jsonArrayRequest);
    }
}
kt06eoxx

kt06eoxx1#

不要用两个 RecyclerView ,但更新 RecyclerView.Adapter 并通知。

相关问题