public class YellowFragment extends Fragment implements FlowerAdapter.FlowerClickListener{


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

        View view = inflater.inflate(R.layout.fragment_yellow, container, false);

        swipeContainer_yellow = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer_yellow);

        setRetainInstance(true);

        mReferSharedPreference = new ReferSharedPreference(getContext());
        mlat = mReferSharedPreference.getValue("Lat", "None");
        mlon = mReferSharedPreference.getValue("Lon", "None");

        mCoordinatesTextLinear = (TextView) view.findViewById(R.id.tv_coordinates_linear);

        if(!mlat.equals("None")){
            getLinearPosts();
        }
        else {
            Toast.makeText(getContext(), "Choose your location", Toast.LENGTH_LONG).show();
        }

        swipeContainer_yellow.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(!mlat.equals("None")){
                    getLinearPosts();
                }
                else {
                    Toast.makeText(getContext(), "Choose your location", Toast.LENGTH_LONG).show();
                }
            }
        });
        configViews(view);
        return view;
    }

    private void getLinearPosts() {
        ReferSharedPreference preferenceCoordinates = new ReferSharedPreference(getContext());
        String lat = preferenceCoordinates.getValue("Lat", "None");
        String lon = preferenceCoordinates.getValue("Lon", "None");
        mCoordinatesTextLinear.setText("Your Location :" + lat + "  , " + lon);

        mRestManager = new RestManager();
        Call<List<Flower>> listCall = mRestManager.getmFlowerApiService(getActivity()).getAllFlowers(lat, lon);
        listCall.enqueue(new Callback<List<Flower>>() {
            @Override
            public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) {

                if (response.isSuccessful()) {
                    mFlowerAdapter.clear();

                    List<Flower> flowerList = response.body();
                    for(int i =0; i<flowerList.size(); i++) {
                        Flower flower = flowerList.get(i);
                        mFlowerAdapter.addFlower(flower);
                    }
                    swipeContainer_yellow.setRefreshing(false);
                }
            }
            @Override
            public void onFailure(Call<List<Flower>> call, Throwable t) {
            }
        });
    }


在此代码中,当我滑动时,如果mlat是None,则存在滑动更新图标,并且它不会消失。

我想要的是,如果mlat具有某些价值,请执行getLinearPosts();,但是如果mlat具有None,请向Toast显示“选择您的位置”。

但是引用图标不会消失

像这样

android - 如何删除此更新图标? Android Swiperefersh-LMLPHP

问题:当mlat为None时,如何删除此更新或刷新图标?

最佳答案

任务完成后,只需调用swipeContainer_yellow.setRefreshing(false),它将隐藏该进度条

如我所见,您已经使用过它,但是问题是您假设您的响应将一直是完美的。因此,如果有条件,请将其保留在if (response.isSuccessful())内,而不是将其保留在onFailure()中,并将其放置在中

listCall.enqueue(new Callback<List<Flower>>() {
    @Override
    public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) {
        swipeContainer_yellow.setRefreshing(false);
        if (response.isSuccessful()) {
            mFlowerAdapter.clear();

            List<Flower> flowerList = response.body();
            for(int i =0; i<flowerList.size(); i++) {
                Flower flower = flowerList.get(i);
                mFlowerAdapter.addFlower(flower);
            }

        }
    }
    @Override
    public void onFailure(Call<List<Flower>> call, Throwable t) {
        swipeContainer_yellow.setRefreshing(false);
    }
});

08-05 00:47