没有数据时无法在SwipeRefresh布局中滑动

时间:2017-06-18 08:53:27

标签: android android-recyclerview

我在我的应用中使用了RecyclerView和SwipeRefreshLayout。问题是当我的RecyclerView有数据时,我可以刷布局。但是当RecyclerView中没有数据时,我无法刷卡。

任何人都可以解决这个问题吗?

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.student_fragment_tutorial_upcoming, container, false);

    tvNoData = (TextView) view.findViewById(R.id.tvNoData);

    bdn = getArguments();
    year = bdn.getString(TutorialStudentFragment.YEARNAME);

    Toast.makeText(getContext(), year, Toast.LENGTH_SHORT).show();

    networkUtil = new NetworkUtil();
    conStatus = networkUtil.getConnectivityStatusString(getContext());

    mdbHelper = new DatabaseHelper(getContext());
    copyData = new CopyDatabase(getContext());

    recyclerTutorial = (RecyclerView) view.findViewById(R.id.recyclerTutorial);

    swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipelayoutTutorial);
    swipeRefreshLayout.setColorSchemeResources(R.color.blue, R.color.orange, R.color.green);

    if (conStatus.equals("wifi") || conStatus.equals("mobile_data")) {
        onRefresh();

    } else {
        //swipeRefreshLayout.setRefreshing(true);
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                if (mdbHelper.getAllTutorialInfo(year, tutorialType).size() == 0) {
                    tvNoData.setText("There is no upcoming tutorial for now");
                    tvNoData.setVisibility(View.VISIBLE);
                } else {
                    tvNoData.setVisibility(View.INVISIBLE);
                    loadTutorialCard(mdbHelper.getAllTutorialInfo(year, tutorialType));
                }
                //swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getContext(), 1);
    recyclerTutorial.setLayoutManager(mLayoutManager);
    recyclerTutorial.addItemDecoration(new GridSpacingItemDecoration(1, dpToPx(10), true));
    recyclerTutorial.setItemAnimator(new DefaultItemAnimator());
    Animation fadeIn = AnimationUtils.loadAnimation(getContext(), R.anim.fade_in);
    Animation fadeOut = AnimationUtils.loadAnimation(getContext(), R.anim.fade_out);
    recyclerTutorial.setAnimation(fadeIn);

    recyclerTutorial.addOnItemTouchListener(new RecyclerTouchListener(getContext(), recyclerTutorial, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {
           /* tutorialArrayList = mdbHelper.getAllTutorialInfo(year);
            Tutorial tutorial = tutorialArrayList.get(position);
            Bundle bdn = new Bundle();*/
            Toast.makeText(getContext(), position + " clicked", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

    swipeRefreshLayout.setOnRefreshListener(this);


    return view;
}



 public void getTutorialData() {
    swipeRefreshLayout.setRefreshing(true);
    networkUtil = new NetworkUtil();
    conStatus = networkUtil.getConnectivityStatusString(getContext());
    if (conStatus.equals("wifi") || conStatus.equals("mobile_data")) {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, select_tutorial_url,
                new Response.Listener<String>() {
                    ArrayList<Tutorial> tutorialArrayList = new ArrayList<>();

                    @Override
                    public void onResponse(String response) {
                        int count = 0;
                        if (response.length() > 0) {
                            while (count < response.length()) {
                                try {
                                    JSONArray jsonArray = new JSONArray(response);
                                    JSONObject jsonObject = jsonArray.getJSONObject(count);
                                    int id = Integer.parseInt(jsonObject.getString("id"));
                                    // String codeStatus =jsonObject.getString("code");
                                    Tutorial tutorial = new Tutorial(id, jsonObject.getString("class_name"), jsonObject.getString("subject"), jsonObject.getString("lesson"), jsonObject.getString("exam_date"), jsonObject.getString("exam_time"), jsonObject.getString("published"));
                                    if (checkTutorialListWithDate(tutorial)) {
                                        tutorialArrayList.add(tutorial);
                                    }

                                    Log.i("Class :", tutorial.getsClass());
                                    Log.i("Subject :", tutorial.getSubject());
                                    Log.i("Lesson :", tutorial.getLesson());

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                count++;
                            }
                        } else {
                            Toast.makeText(getContext(), String.valueOf(response.length()), Toast.LENGTH_SHORT).show();
                        }
                        swipeRefreshLayout.setRefreshing(false);

                        loadTutorialCard(tutorialArrayList);
                        if (tutorialArrayList.size() == 0) {
                            tvNoData.setVisibility(View.VISIBLE);
                            tvNoData.setText("There is no upcoming tutorial for now");
                        } else {
                            tvNoData.setVisibility(View.INVISIBLE);
                        }

                        workingWithLocalDatabase(tutorialArrayList, mdbHelper.getAllTutorialInfo(year, tutorialType));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                swipeRefreshLayout.setRefreshing(false);
                NetworkUtil.checkInternetConnection(error, getContext(), swipeRefreshLayout);
            }
        }
        ) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("studentYear", year);
                return params;
            }
        };
        Mysingleton.getInstance(getContext()).addToRequestque(stringRequest);
    } else {
        swipeRefreshLayout.setRefreshing(false);
        networkUtil.showNetError(getContext(), swipeRefreshLayout);
    }
}

public void onRefresh() {
    getTutorialData();
}

0 个答案:

没有答案